From 0a8cdc60e4c30e2b73138c0a65ad07025a01db1b Mon Sep 17 00:00:00 2001 From: Rob Davies Date: Mon, 24 Sep 2018 12:00:25 +0100 Subject: [PATCH] Fix incorrect l_extranul calculation. 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. --- sam.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sam.c b/sam.c index 047ad89e3..45ffac9f1 100644 --- a/sam.c +++ b/sam.c @@ -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;