Skip to content

Commit de3c6ec

Browse files
Fix use-after-free in mappy (lh3#1345)
mm_map_aux() takes in |b._b| which can end up reallocating |km| at the end of mm_map_frag_core(). Since the address of |km| is cached before those calls it ends up pointing to freed memory. This can result in a crash as seen in lh3#1183, however it also happens to Just Work most of the time since the new allocation often lands at the same address as the old one. Preloading ASAN or a similar replacement allocator that doesn't have that behaviour results in a reliable crash.
1 parent e2542e6 commit de3c6ec

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

python/mappy.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ cdef class Aligner:
189189
if self._idx is NULL: return None
190190
if buf is None: b = ThreadBuffer()
191191
else: b = buf
192-
km = cmappy.mm_tbuf_get_km(b._b)
193192

194193
_seq = seq if isinstance(seq, bytes) else seq.encode()
195194
if name is not None:
@@ -216,6 +215,7 @@ cdef class Aligner:
216215
c = h.cigar32[k]
217216
cigar.append([c>>4, c&0xf])
218217
if cs or ds or MD: # generate the cs/ds and/or the MD tag, if requested
218+
km = cmappy.mm_tbuf_get_km(b._b)
219219
_cur_seq = _seq2 if h.seg_id > 0 and seq2 is not None else _seq
220220
if cs:
221221
l_cs_str = cmappy.mm_gen_cs(km, &cs_str, &m_cs_str, self._idx, &regs[i], _cur_seq, 1)

0 commit comments

Comments
 (0)