Skip to content

Commit

Permalink
fix leaks in getFORMAT with vector<string>
Browse files Browse the repository at this point in the history
  • Loading branch information
Zilong-Li committed Apr 8, 2024
1 parent a62802a commit 4149953
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CXX = g++
CXXFLAGS = -std=c++11 -Wall -fsanitize=address
# CXXFLAGS = -std=c++11 -Wall -g
LDFLAGS = -L$(HTSLIB) -Wl,-rpath,$(HTSLIB)
LIBS = -lhts
LIBS = -lhts -lz -lm -lbz2 -llzma -lcurl -lpthread
INC = -I. -I.. -I${HTSINC}

OBJS = test-main.o bcf-variant.o bcf-reader.o bcf-writer.o example.o
Expand Down
20 changes: 20 additions & 0 deletions test/bcf-reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,23 @@ TEST_CASE("parse vcf with multialleles - vector<int>", "[bcf-reader]")
var.getGenotypes(gt);
for(auto g : gt) cout << g << endl;
}

TEST_CASE("parse EV in vcf - vector<string>", "[bcf-reader]")
{
string vcffile{"test-GL.vcf.gz"};
BcfWriter bw(vcffile, "VCF4.2");
bw.header.addContig("chr20");
bw.header.addINFO("AF", "A", "Float", "Estimated allele frequency in the range (0,1)");
bw.header.addFORMAT("GT", "1", "String", "Genotype");
bw.header.addFORMAT("EV", "G", "String", "Classes of evidence supporting final genotype");
for(auto & s : {"id01", "id02"}) bw.header.addSample(s); // add 3 samples
bw.writeLine("chr20\t2006060\trs146931526\tG\tA\t100\tPASS\tAF=0.02\tGT:EV\t0/1:RD\t1/1:SR,PE");
bw.close();
BcfReader br(vcffile);
BcfRecord var(br.header);
vector<string> ev;
REQUIRE(br.getNextVariant(var)==true);
var.getFORMAT("EV",ev);
REQUIRE(ev[0]=="RD");
REQUIRE(ev[1]=="SR,PE");
}
6 changes: 3 additions & 3 deletions vcfpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file https://github.com/Zilong-Li/vcfpp/vcfpp.h
* @author Zilong Li
* @email [email protected]
* @version v0.3.7
* @version v0.3.8
* @breif a single C++ file for manipulating VCF
* Copyright (C) 2022-2023.The use of this code is governed by the LICENSE file.
******************************************************************************/
Expand Down Expand Up @@ -648,12 +648,12 @@ class BcfRecord
// Ugly: GT field is considered to be a string by the VCF header but BCF represents it as INT.
v.emplace_back(dst[i]);
};
free(dst);
free(dst[0]); free(dst);
return true;
}
else
{
free(dst);
free(dst[0]); free(dst);
throw std::runtime_error("couldn't parse the " + tag + " format of this variant.\n");
}
}
Expand Down

0 comments on commit 4149953

Please sign in to comment.