From 1dcfe72ceecafbd9d54e334383bbef39e986211e Mon Sep 17 00:00:00 2001 From: thomas Date: Mon, 9 Oct 2017 15:33:20 +0200 Subject: [PATCH 1/3] main.cpp: Defeat impact of possible buffer overflow 'valgrind --tool=exp-sgcheck' detected an invalid write at former line 491 (sprintf...). Using snprintf avoids this condition, and allows data loss detection (Q: why not use the safer c++ strings, A:???). This fix has some observable impact on bsmap's output, although I've only seen a change in the amount of whitespace. I also assume that this might be the cause for the crashes at the end of bsmap execution. --- main.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 7bab7d4..761df2d 100644 --- a/main.cpp +++ b/main.cpp @@ -20,6 +20,8 @@ #include #endif +#define LINEBUFFSIZE 1024 + using namespace std; //global variables @@ -397,7 +399,8 @@ int check_ifile_format(string &filename, int gz_flag) { } void RunProcess(void) { - char _ch[256]; string _str="@HD\tVN:1.0\n"; + char _ch[LINEBUFFSIZE]; + string _str="@HD\tVN:1.0\n"; if(out_align_file.size()>4){ if(out_align_file.compare(out_align_file.size()-4,4,".sam")==0) param.out_sam=1; else if (out_align_file.compare(out_align_file.size()-4,4,".bam")==0) param.out_sam=2; @@ -483,11 +486,19 @@ void RunProcess(void) { } if(param.out_sam&¶m.sam_header) { + int ret_len; for(bit32_t i=0;i=LINEBUFFSIZE) { + cerr<<"Buffer error, output was truncated (increase LINEBUFFSIZE in main.cpp and recompile)."<=LINEBUFFSIZE) { + cerr<<"Buffer error, output was truncated (increase LINEBUFFSIZE in main.cpp and recompile)."< Date: Mon, 9 Oct 2017 15:39:33 +0200 Subject: [PATCH 2/3] main.cpp: Remove some whitespace clutter so that the next commit will look cute :) --- main.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/main.cpp b/main.cpp index 761df2d..0f0b22d 100644 --- a/main.cpp +++ b/main.cpp @@ -562,18 +562,18 @@ void RunProcess(void) { } else fout.open(out_align_file.c_str()); } - + if(param.out_sam&¶m.sam_header) { - char _ch[1000]; - for(bit32_t i=0;i Date: Mon, 9 Oct 2017 15:55:00 +0200 Subject: [PATCH 3/3] main.cpp: Add snprintf code in similar code section Introduce snprintf & LINEBUFFSIZE in similar section for 'single-read alignments'. This should make the code a bit cleaner. --- main.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 0f0b22d..9c252a2 100644 --- a/main.cpp +++ b/main.cpp @@ -564,12 +564,20 @@ void RunProcess(void) { } if(param.out_sam&¶m.sam_header) { - char _ch[1000]; + char _ch[LINEBUFFSIZE]; + int ret_len; for(bit32_t i=0;i=LINEBUFFSIZE) { + cerr<<"Buffer error, output was truncated (increase LINEBUFFSIZE in main.cpp and recompile)."<=LINEBUFFSIZE) { + cerr<<"Buffer error, output was truncated (increase LINEBUFFSIZE in main.cpp and recompile)."<