Skip to content

Commit

Permalink
Fixed buffer overflow with inaccurate exon length
Browse files Browse the repository at this point in the history
closes #26
  • Loading branch information
agraubert committed Apr 17, 2019
1 parent 58d57ef commit fe0ded9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/GTF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "GTF.h"
#include <exception>
#include <stdexcept>
#include <unordered_set>
#include <boost/regex.hpp>

using std::ifstream;
Expand All @@ -27,6 +28,7 @@ namespace rnaseqc {

ifstream& operator>>(ifstream &in, Feature &out)
{
static std::unordered_set<std::string> geneIds, exonIds;
try{
string line;
while(getline(in, line))
Expand Down Expand Up @@ -76,6 +78,8 @@ namespace rnaseqc {
std::cout << "Bad fead feature range:" << out.start << " - " << out.end << std::endl;
if (out.type == FeatureType::Gene && attributes.find("gene_id") != attributes.end())
{
if (geneIds.count(out.feature_id)) throw gtfException(std::string("Detected non-unique Gene ID: "+out.feature_id));
geneIds.insert(out.feature_id);
//Parse gene attributes
out.feature_id = attributes["gene_id"];
geneLengths[out.feature_id] = out.end - out.start + 1;
Expand All @@ -85,6 +89,8 @@ namespace rnaseqc {
if (attributes.find("gene_id") != attributes.end()) out.gene_id = attributes["gene_id"];
if (out.type == FeatureType::Exon)
{
if (exonIds.count(out.feature_id)) throw gtfException(std::string("Detected non-unique Exon ID: "+out.feature_id));
exonIds.insert(out.feature_id);
//Parse exon attributes
if (attributes.find("exon_id") != attributes.end())
{
Expand All @@ -107,6 +113,10 @@ namespace rnaseqc {
}

}
catch(gtfException &e)
{
throw e;
}
catch(std::invalid_argument &e)
{
throw gtfException(std::string("GTF is in an invalid format: ") + e.what());
Expand Down
4 changes: 3 additions & 1 deletion src/Metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ namespace rnaseqc {

void add_range(std::vector<unsigned long> &coverage, coord offset, unsigned int length)
{
for (coord i = offset; i < offset + length; ++i) coverage[i] += 1ul;
const size_t size = coverage.size();
for (coord i = offset; i < offset + length && i < size; ++i) coverage[i] += 1ul;
if (offset + length > size) std::cerr << "Error: Attempted to write more coverage than present on exon. Coverage-based metrics may be inaccurate. This may be a sign of an invalid bam or gtf entry" << std::endl;
}

//Compute exon coverage metrics, then stich exons together and compute gene coverage metrics
Expand Down

0 comments on commit fe0ded9

Please sign in to comment.