Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/bam/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ CCCCCCCCCCCCCCCCCCC"[..],
rec.set(names[0], Some(&cigars[0]), seqs[0], quals[0]);
// note: this segfaults if you push_aux() before set()
// because set() obliterates aux
rec.push_aux(b"NM", &Aux::Integer(15)).unwrap();
rec.push_aux(b"NM", &Aux::Integer(15));

assert_eq!(rec.qname(), names[0]);
assert_eq!(*rec.cigar(), cigars[0]);
Expand All @@ -1241,7 +1241,7 @@ CCCCCCCCCCCCCCCCCCC"[..],
for i in 0..names.len() {
let mut rec = record::Record::new();
rec.set(names[i], Some(&cigars[i]), seqs[i], quals[i]);
rec.push_aux(b"NM", &Aux::Integer(15)).unwrap();
rec.push_aux(b"NM", &Aux::Integer(15));

assert_eq!(rec.qname(), names[i]);
assert_eq!(*rec.cigar(), cigars[i]);
Expand Down Expand Up @@ -1354,7 +1354,7 @@ CCCCCCCCCCCCCCCCCCC"[..],
for i in 0..names.len() {
let mut rec = record::Record::new();
rec.set(names[i], Some(&cigars[i]), seqs[i], quals[i]);
rec.push_aux(b"NM", &Aux::Integer(15)).unwrap();
rec.push_aux(b"NM", &Aux::Integer(15));

bam.write(&mut rec).ok().expect("Failed to write record.");
}
Expand Down Expand Up @@ -1406,7 +1406,7 @@ CCCCCCCCCCCCCCCCCCC"[..],
let mut rec = record::Record::new();
let idx = i % names.len();
rec.set(names[idx], Some(&cigars[idx]), seqs[idx], quals[idx]);
rec.push_aux(b"NM", &Aux::Integer(15)).unwrap();
rec.push_aux(b"NM", &Aux::Integer(15));
rec.set_pos(i as i32);

bam.write(&mut rec).ok().expect("Failed to write record.");
Expand Down
30 changes: 19 additions & 11 deletions src/bam/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,25 @@ impl Record {
}

fn realloc_var_data(&mut self, new_len: usize) {
self.inner_mut().m_data = new_len as u32;
// Pad
self.inner_mut().m_data += 32 - self.inner().m_data % 32;
unsafe {
self.inner_mut().data = ::libc::realloc(
// pad request
let new_len = new_len as u32;
let new_request = new_len + 32 - (new_len % 32);

let ptr = unsafe {
::libc::realloc(
self.inner().data as *mut ::libc::c_void,
self.inner().m_data as usize,
) as *mut u8;
new_request as usize,
) as *mut u8
};

if ptr.is_null() {
panic!("ran out of memory in rust_htslib trying to realloc");
}

// don't update m_data until we know we have
// a successful allocation.
self.inner_mut().m_data = new_request;
self.inner_mut().data = ptr;
}

pub fn cigar_len(&self) -> usize {
Expand Down Expand Up @@ -513,7 +523,7 @@ impl Record {

/// Add auxiliary data.
/// push_aux() should never be called before set().
pub fn push_aux(&mut self, tag: &[u8], value: &Aux<'_>) -> Result<(), AuxWriteError> {
pub fn push_aux(&mut self, tag: &[u8], value: &Aux<'_>) {
let ctag = tag.as_ptr() as *mut i8;
let ret = unsafe {
match *value {
Expand Down Expand Up @@ -549,9 +559,7 @@ impl Record {
};

if ret < 0 {
Err(AuxWriteError::Some)
} else {
Ok(())
panic!("htslib ran out of memory in push_aux");
}
}

Expand Down