Skip to content

v2.10.1: fix malloc crash on BAMs with malformed MD tags

Choose a tag to compare

@mtcicero26 mtcicero26 released this 23 Apr 15:39
· 116 commits to main since this release

Bug fix

fiberhmm-extract (and fiberhmm-call --mode daf on v2.10.0) crashed on BAMs where the MD tag's encoded reference length disagreed with the CIGAR's reference-consuming op total. Classic consensus-BAM bug: MD inherits stale state after a re-alignment updates CIGAR.

Symptom on v2.9.7:

malloc(): invalid size (unsorted)

Why

pysam.get_aligned_pairs(with_seq=True) raises AssertionError on MD/CIGAR mismatch — but before raising, pysam has already corrupted its internal malloc state. So v2.9.6 crashed immediately with the assertion; v2.9.7 swallowed the assertion but the heap was already poisoned, surfacing as a malloc(): invalid size on some unrelated allocation later in the worker.

Fix

Pre-validate the MD tag against the CIGAR in pure Python before asking pysam to parse it. If they disagree, skip the get_aligned_pairs(with_seq=True) call entirely. Never triggers pysam's AssertionError path, never corrupts malloc.

Patched both call sites:

  • fiberhmm/cli/extract_tags.py — _extract_deam path-3
  • fiberhmm/daf/encoder.py — get_daf_positions (v2.10.0's --mode daf MD fallback)

Workaround for affected BAMs (optional)

If you want --deam calls to populate for reads with broken MD, regenerate clean MD tags:

samtools calmd -b aligned.bam ref.fa > fixed.bam

Without this, v2.10.1 silently skips the broken reads for --deam extraction (every other track still works for those reads). With v2.10.0's --mode daf, you can also pass --reference ref.fa as a fallback.

Tests

292 passing. 3 new tests pin the MD parser, the validator, and the invariant that malformed MD bypasses the pysam call completely.

Install

pip install --upgrade fiberhmm