Skip to content

Commit 9a4b660

Browse files
committed
Stricter limit on POS/MPOS/TLEN in sam_parse1()
Help avoid overflow on arithmetic involving POS, MPOS and TLEN by limiting values in the SAM parser to fit in 62 bits (or 63 for TLEN as it's signed). The new limit is still massively bigger than any known reference so it should not cause any problems in practice. Credit to OSS-Fuzz Fixes oss-fuzz 68750
1 parent fbe5ff6 commit 9a4b660

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

sam.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -2947,7 +2947,7 @@ int sam_parse1(kstring_t *s, sam_hdr_t *h, bam1_t *b)
29472947
} else c->tid = -1;
29482948

29492949
// pos
2950-
c->pos = hts_str2uint(p, &p, 63, &overflow) - 1;
2950+
c->pos = hts_str2uint(p, &p, 62, &overflow) - 1;
29512951
if (*p++ != '\t') goto err_ret;
29522952
if (c->pos < 0 && c->tid >= 0) {
29532953
_parse_warn(1, "mapped query cannot have zero coordinate; treated as unmapped");
@@ -2990,15 +2990,16 @@ int sam_parse1(kstring_t *s, sam_hdr_t *h, bam1_t *b)
29902990
_parse_warn(c->mtid < 0, "unrecognized mate reference name %s; treated as unmapped", hts_strprint(logbuf, sizeof logbuf, '"', q, SIZE_MAX));
29912991
}
29922992
// mpos
2993-
c->mpos = hts_str2uint(p, &p, 63, &overflow) - 1;
2993+
c->mpos = hts_str2uint(p, &p, 62, &overflow) - 1;
29942994
if (*p++ != '\t') goto err_ret;
29952995
if (c->mpos < 0 && c->mtid >= 0) {
29962996
_parse_warn(1, "mapped mate cannot have zero coordinate; treated as unmapped");
29972997
c->mtid = -1;
29982998
}
29992999
// tlen
3000-
c->isize = hts_str2int(p, &p, 64, &overflow);
3000+
c->isize = hts_str2int(p, &p, 63, &overflow);
30013001
if (*p++ != '\t') goto err_ret;
3002+
_parse_err(overflow, "number outside allowed range");
30023003
// seq
30033004
q = _read_token(p);
30043005
if (strcmp(q, "*")) {

test/sam.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* test/sam.c -- SAM/BAM/CRAM API test cases.
22
3-
Copyright (C) 2014-2020, 2022-2023 Genome Research Ltd.
3+
Copyright (C) 2014-2020, 2022-2024 Genome Research Ltd.
44
55
Author: John Marshall <[email protected]>
66
@@ -1408,16 +1408,16 @@ static void check_big_ref(int parse_header)
14081408
"@HD\tVN:1.4\n"
14091409
"@SQ\tSN:large#1\tLN:5000000000\n"
14101410
"@SQ\tSN:small#1\tLN:100\n"
1411-
"@SQ\tSN:large#2\tLN:9223372034707292158\n"
1411+
"@SQ\tSN:large#2\tLN:4611686018427387904\n"
14121412
"@SQ\tSN:small#2\tLN:1\n"
14131413
"r1\t0\tlarge#1\t4999999000\t50\t8M\t*\t0\t0\tACGTACGT\tabcdefgh\n"
14141414
"r2\t0\tsmall#1\t1\t50\t8M\t*\t0\t0\tACGTACGT\tabcdefgh\n"
1415-
"r3\t0\tlarge#2\t9223372034707292000\t50\t8M\t*\t0\t0\tACGTACGT\tabcdefgh\n"
1416-
"p1\t99\tlarge#2\t1\t50\t8M\t=\t9223372034707292150\t9223372034707292158\tACGTACGT\tabcdefgh\n"
1417-
"p1\t147\tlarge#2\t9223372034707292150\t50\t8M\t=\t1\t-9223372034707292158\tACGTACGT\tabcdefgh\n"
1415+
"r3\t0\tlarge#2\t4611686018427387000\t50\t8M\t*\t0\t0\tACGTACGT\tabcdefgh\n"
1416+
"p1\t99\tlarge#2\t1\t50\t8M\t=\t4611686018427387895\t4611686018427387903\tACGTACGT\tabcdefgh\n"
1417+
"p1\t147\tlarge#2\t4611686018427387895\t50\t8M\t=\t1\t-4611686018427387903\tACGTACGT\tabcdefgh\n"
14181418
"r4\t0\tsmall#2\t2\t50\t8M\t*\t0\t0\tACGTACGT\tabcdefgh\n";
14191419
const hts_pos_t expected_lengths[] = {
1420-
5000000000LL, 100LL, 9223372034707292158LL, 1LL
1420+
5000000000LL, 100LL, 4611686018427387904LL, 1LL
14211421
};
14221422
const int expected_tids[] = {
14231423
0, 1, 2, 2, 2, 3
@@ -1426,11 +1426,11 @@ static void check_big_ref(int parse_header)
14261426
-1, -1, -1, 2, 2, -1
14271427
};
14281428
const hts_pos_t expected_positions[] = {
1429-
4999999000LL - 1, 1LL - 1, 9223372034707292000LL - 1, 1LL - 1,
1430-
9223372034707292150LL - 1, 2LL - 1
1429+
4999999000LL - 1, 1LL - 1, 4611686018427387000LL - 1, 1LL - 1,
1430+
4611686018427387895LL - 1, 2LL - 1
14311431
};
14321432
const hts_pos_t expected_mpos[] = {
1433-
-1, -1, -1, 9223372034707292150LL - 1, 1LL - 1, -1
1433+
-1, -1, -1, 4611686018427387895LL - 1, 1LL - 1, -1
14341434
};
14351435
samFile *in = NULL, *out = NULL;
14361436
sam_hdr_t *header = NULL, *dup_header = NULL;

0 commit comments

Comments
 (0)