Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Fix segfault in VariantReader on non-existent file
Browse files Browse the repository at this point in the history
  • Loading branch information
droazen committed Oct 29, 2014
1 parent a2a79b6 commit 7fee5cc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gamgee/variant_reader.h
Expand Up @@ -14,6 +14,7 @@
#include <fstream>
#include <algorithm>
#include <memory>
#include <stdexcept>


namespace gamgee {
Expand Down Expand Up @@ -163,6 +164,9 @@ class VariantReader {
*/
void init_reader (const std::string& filename) {
auto* file_ptr = bcf_open(filename.empty() ? "-" : filename.c_str(), "r");
if ( file_ptr == nullptr ) {
throw std::invalid_argument{std::string{"Could not open file "} + filename};
}
m_variant_file_ptr = utils::make_shared_hts_file (file_ptr);
m_variant_header_ptr = utils::make_shared_variant_header (bcf_hdr_read (file_ptr));
}
Expand Down
5 changes: 5 additions & 0 deletions test/variant_reader_test.cpp
Expand Up @@ -14,6 +14,7 @@
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/iterator/zip_iterator.hpp>
#include <stdexcept>

using namespace std;
using namespace gamgee;
Expand Down Expand Up @@ -1340,3 +1341,7 @@ BOOST_AUTO_TEST_CASE( synced_variant_iterator_move_test ) {
BOOST_CHECK_EQUAL(record0[0].alignment_start(), moved_record[0].alignment_start());
}
}

BOOST_AUTO_TEST_CASE( variant_reader_nonexistent_file ) {
BOOST_CHECK_THROW(SingleVariantReader{"foo/bar/nonexistent.vcf"}, invalid_argument);
}

0 comments on commit 7fee5cc

Please sign in to comment.