Skip to content

Commit

Permalink
Permit BAM headers between 2GB and 4GB in size once more.
Browse files Browse the repository at this point in the history
This isn't permitted by the BAM specification, but was accepted by
earlier htslib release.  62f9909 added code to check the maximum
length.  This now has a warning at 2GB and the hard-failure at 4GB.

Fixes samtools#1420.  Fixes samtools/samtools#1613
  • Loading branch information
jkbonfield committed Apr 8, 2022
1 parent 52f10df commit f62563d
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,23 @@ int bam_hdr_write(BGZF *fp, const sam_hdr_t *h)

if (h->hrecs) {
if (sam_hrecs_rebuild_text(h->hrecs, &hdr_ks) != 0) return -1;
if (hdr_ks.l > INT32_MAX) {
if (hdr_ks.l > UINT32_MAX) {
hts_log_error("Header too long for BAM format");
free(hdr_ks.s);
return -1;
} else if (hdr_ks.l > INT32_MAX) {
hts_log_warning("Header too long for BAM specification (>2GB)");
hts_log_warning("Output file may not be portable");
}
text = hdr_ks.s;
l_text = hdr_ks.l;
} else {
if (h->l_text > INT32_MAX) {
if (h->l_text > UINT32_MAX) {
hts_log_error("Header too long for BAM format");
return -1;
} else if (h->l_text > INT32_MAX) {
hts_log_warning("Header too long for BAM specification (>2GB)");
hts_log_warning("Output file may not be portable");
}
text = h->text;
l_text = h->l_text;
Expand Down

0 comments on commit f62563d

Please sign in to comment.