From 321c791f13214efb0fbe0095eee4a69e1412c508 Mon Sep 17 00:00:00 2001 From: minorllama Date: Mon, 13 Jul 2026 14:20:11 -0400 Subject: [PATCH 1/3] add null check to tidname on HeaderView --- src/bam/mod.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/bam/mod.rs b/src/bam/mod.rs index d13852f90..cd1d5a6be 100644 --- a/src/bam/mod.rs +++ b/src/bam/mod.rs @@ -1439,7 +1439,13 @@ impl HeaderView { } pub fn tid2name(&self, tid: u32) -> &[u8] { - unsafe { ffi::CStr::from_ptr(htslib::sam_hdr_tid2name(self.inner, tid as i32)).to_bytes() } + // unsafe { ffi::CStr::from_ptr(htslib::sam_hdr_tid2name(self.inner, tid as i32)).to_bytes() } + let ptr = unsafe { htslib::sam_hdr_tid2name(self.inner, tid as i32) }; + if ptr.is_null() { + b"" + } else { + unsafe { ffi::CStr::from_ptr(ptr).to_bytes() } + } } pub fn target_count(&self) -> u32 { @@ -3182,6 +3188,13 @@ CCCCCCCCCCCCCCCCCCC"[..], let actual = reader.index_stats().unwrap(); assert_eq!(expected, actual); } + + #[test] + fn test_nonexistent_tidname() { + let header = Header::new(); + let header_view = HeaderView::from_header(&header); + assert_eq!(b"", header_view.tid2name(0)); + } // #[test] // fn test_number_mapped_and_unmapped_cram() { From 2c30fbc7be9990c945386d4c25fb4e5b4cca33db Mon Sep 17 00:00:00 2001 From: minorllama <80299702+minorllama@users.noreply.github.com> Date: Tue, 14 Jul 2026 14:54:31 -0400 Subject: [PATCH 2/3] remove commented out old code mod.rs --- src/bam/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/bam/mod.rs b/src/bam/mod.rs index cd1d5a6be..10f2cb25d 100644 --- a/src/bam/mod.rs +++ b/src/bam/mod.rs @@ -1439,7 +1439,6 @@ impl HeaderView { } pub fn tid2name(&self, tid: u32) -> &[u8] { - // unsafe { ffi::CStr::from_ptr(htslib::sam_hdr_tid2name(self.inner, tid as i32)).to_bytes() } let ptr = unsafe { htslib::sam_hdr_tid2name(self.inner, tid as i32) }; if ptr.is_null() { b"" From fd54af14b246238a02f69108e636f969125986f7 Mon Sep 17 00:00:00 2001 From: minorllama Date: Wed, 15 Jul 2026 14:21:46 -0400 Subject: [PATCH 3/3] linting:cargo fmt + clippy --- src/bam/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bam/mod.rs b/src/bam/mod.rs index 10f2cb25d..85a15340a 100644 --- a/src/bam/mod.rs +++ b/src/bam/mod.rs @@ -1441,7 +1441,7 @@ impl HeaderView { pub fn tid2name(&self, tid: u32) -> &[u8] { let ptr = unsafe { htslib::sam_hdr_tid2name(self.inner, tid as i32) }; if ptr.is_null() { - b"" + b"" } else { unsafe { ffi::CStr::from_ptr(ptr).to_bytes() } } @@ -3187,7 +3187,7 @@ CCCCCCCCCCCCCCCCCCC"[..], let actual = reader.index_stats().unwrap(); assert_eq!(expected, actual); } - + #[test] fn test_nonexistent_tidname() { let header = Header::new();