Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: memory leak in faidx when fetching sequences #455

Merged
merged 1 commit into from
Dec 2, 2024
Merged
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
30 changes: 16 additions & 14 deletions src/faidx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,27 @@ impl Reader {
/// * `name` - the name of the template sequence (e.g., "chr1")
/// * `begin` - the offset within the template sequence (starting with 0)
/// * `end` - the end position to return (if smaller than `begin`, the behavior is undefined).
pub fn fetch_seq<N: AsRef<str>>(&self, name: N, begin: usize, end: usize) -> Result<&[u8]> {
pub fn fetch_seq<N: AsRef<str>>(&self, name: N, begin: usize, end: usize) -> Result<Vec<u8>> {
if begin > std::i64::MAX as usize {
return Err(Error::FaidxPositionTooLarge);
}
if end > std::i64::MAX as usize {
return Err(Error::FaidxPositionTooLarge);
}
let cname = ffi::CString::new(name.as_ref().as_bytes()).unwrap();
let len_out: i64 = 0;
let cseq = unsafe {
let ptr = htslib::faidx_fetch_seq64(
self.inner, //*const faidx_t,
cname.as_ptr(), // c_name
begin as htslib::hts_pos_t, // p_beg_i
end as htslib::hts_pos_t, // p_end_i
&mut (len_out as htslib::hts_pos_t), //len
);
ffi::CStr::from_ptr(ptr)
let mut len_out: htslib::hts_pos_t = 0;
let ptr = unsafe {
htslib::faidx_fetch_seq64(
self.inner, //*const faidx_t,
cname.as_ptr(), // c_name
begin as htslib::hts_pos_t, // p_beg_i
end as htslib::hts_pos_t, // p_end_i
&mut len_out, //len
)
};

Ok(cseq.to_bytes())
let vec =
unsafe { Vec::from_raw_parts(ptr as *mut u8, len_out as usize, len_out as usize) };
Ok(vec)
}

/// Fetches the sequence and returns it as string.
Expand All @@ -122,7 +122,7 @@ impl Reader {
end: usize,
) -> Result<String> {
let bytes = self.fetch_seq(name, begin, end)?;
Ok(std::str::from_utf8(bytes).unwrap().to_owned())
Ok(std::str::from_utf8(&bytes).unwrap().to_owned())
}

/// Fetches the number of sequences in the fai index
Expand Down Expand Up @@ -226,13 +226,15 @@ mod tests {
fn faidx_read_chr_start() {
let r = open_reader();

//for _i in 0..100_000_000 { // loop to check for memory leaks
let bseq = r.fetch_seq("chr1", 0, 9).unwrap();
assert_eq!(bseq.len(), 10);
assert_eq!(bseq, b"GGGCACAGCC");

let seq = r.fetch_seq_string("chr1", 0, 9).unwrap();
assert_eq!(seq.len(), 10);
assert_eq!(seq, "GGGCACAGCC");
//}
}

#[test]
Expand Down
Loading