Skip to content

Commit

Permalink
Fix incorrect l_extranul calculation.
Browse files Browse the repository at this point in the history
Commit 6eb1051 changed how the sam parser pads the query name to
be a multiple of four bytes.  An error in an if statement caused
it to use four bytes instead of zero on names that did not need
any extra padding.  This also caused l_qname to wrap around when
the name was exactly 252 characters long, leading to problems like
an out-of-bounds memory access in sam_format1().

Replace the calculation with a corrected version that also gets
rid of the if statement.
  • Loading branch information
daviesrob committed Sep 24, 2018
1 parent 864c5b7 commit 0a8cdc6
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ int sam_parse1(kstring_t *s, bam_hdr_t *h, bam1_t *b)
if ((p-q)+4 > SIZE_MAX - s->l || ks_resize(&str, str.l+(p-q)+4) < 0) goto err_ret;
memcpy(str.s+str.l, q, p-q); str.l += p-q;

c->l_extranul = 4-(str.l % 4); if (c->l_extranul == 0) c->l_extranul = 0;
c->l_extranul = (4 - (str.l & 3)) & 3;
memcpy(str.s+str.l, "\0\0\0\0", c->l_extranul); str.l += c->l_extranul;

c->l_qname = p - q + c->l_extranul;
Expand Down

0 comments on commit 0a8cdc6

Please sign in to comment.