From c03d5fbb27d5df180207c17e9cbbddde933957ff Mon Sep 17 00:00:00 2001 From: Giles Hall Date: Thu, 12 Feb 2026 17:20:26 -0500 Subject: [PATCH] Fix silent truncation in IteratorRowAllRefs on I/O errors IteratorRowAllRefs.__next__() only checked `retval > 0` to decide whether a record was available. All non-positive return values, including I/O errors (retval < -1) and truncated files (retval == -2), were silently treated as end-of-reference and the iterator advanced to the next tid. This caused `samfile.fetch()` without a region to silently yield truncated results on read errors with no exception. Add explicit error checks matching the pattern already used by IteratorRowRegion: raise IOError on retval < -1, only treat retval == -1 as normal end-of-reference. --- pysam/libcalignmentfile.pyx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pysam/libcalignmentfile.pyx b/pysam/libcalignmentfile.pyx index d65d06b4c..1379d4691 100644 --- a/pysam/libcalignmentfile.pyx +++ b/pysam/libcalignmentfile.pyx @@ -2256,9 +2256,16 @@ cdef class IteratorRowAllRefs(IteratorRow): self.rowiter.cnext() # If current iterator is not exhausted, return aligned read - if self.rowiter.retval > 0: + if self.rowiter.retval >= 0: return makeAlignedSegment(self.rowiter.b, self.header) + if self.rowiter.retval == -2: + raise IOError('truncated file') + elif self.rowiter.retval < -1: + raise IOError( + "error while reading file {}: {}".format( + self.samfile.filename, self.rowiter.retval)) + self.tid += 1 # Otherwise, proceed to next reference or stop