Fix samtools merge crash on BAM files with malformed headers#2128
Conversation
When the binary and text reference names disagree in a BAM header, it's possible for the number of references apparently present to increase when the header text is parsed. This caused a crash when samtools merge built its reference translation table because the trans_tbl_t::tid_trans array was allocated in trans_tbl_init() with the smaller size, and then trans_tbl_add_sq() used the bigger one when trying to put data in it. Fixed by moving the allocation to trans_tbl_add_sq(), so the space allocated will always match the number of items iterated over. Thanks to @frostb1ten for the bug report. Fixes samtools#2127 (Heap Buffer Overflow in samtools merge within trans_tbl_add_sq function)
|
Do you think we should be updating htslib too to warn when there are disparities between binary and text SAM headers? It's legal (maybe?) but it's also unusual and probably indicative of something going wrong. I'm also wondering how many other places have a similar issue. (This fix does work though.) |
|
I couldn't find any other obvious uses of |
|
I did a search for other uses of |
When the binary and text reference names disagree in a BAM header, it's possible for the number of references apparently present to increase when the header text is parsed. This caused a crash when samtools merge built its reference translation table because the
trans_tbl_t::tid_transarray was allocated intrans_tbl_init()with the smaller size, and thentrans_tbl_add_sq()used the bigger one when trying to put data in it.Fixed by moving the allocation to
trans_tbl_add_sq(), so the space allocated will always match the number of items iterated over.Thanks to @frostb1ten for the bug report.
Fixes #2127 (Heap Buffer Overflow in samtools merge within trans_tbl_add_sq function)