rust-bio / rust-htslib

This library provides HTSlib bindings and a high level Rust API for reading and writing BAM files.
MIT License
309 stars 80 forks source link

SIGABRT when dropping record after reading string format field and genotypes in VCF #161

Open Adaminius opened 5 years ago

Adaminius commented 5 years ago

Hello!

I am receiving errors like malloc: Incorrect checksum for freed object 0x7fdf6c4066b8: probably modified after being freed in my code, and I think it might be due to a bug in rust-htslib's use of bcf_get_format_values() in htslib for strings or genotypes. Here is the smallest example I could cut my code down to and still reproduce the issue:

use rust_htslib::bcf;
use rust_htslib::bcf::Read;

fn main() {
    let mut vcf = bcf::Reader::from_path("src/test_data/tmp.vcf")
        .expect(&format!("Failed to open file"));

    for (rec_index, rec) in vcf.records().enumerate() {
        let mut record = rec.unwrap();
        let sample_filters = record.format(b"FT").string().unwrap();
        let genotypes = record.genotypes().unwrap();
    }
}

I have the above code ready to run with an example VCF in this repo.

I have reproduced the issue on macOS 10.14 and Ubuntu 16.04.

I am new to Rust and relatively inexperienced with other low-level languages, so I am having difficulty debugging this myself, so I would really appreciate it if one of the authors could take a look into this.

Thank you!

Adaminius commented 5 years ago

Oddly, I don't seem to get this issue when I access genotypes/other integer/float fields before accessing string fields.

johanneskoester commented 5 years ago

Thanks for reporting. I cannot see anything wrong in our way to call the function. Of course, any help would be welcome. It could be something related to automatic unpacking of the record though.