Bug Summary

File:/home/sdobbs/work/clang/halld_recon/src/programs/Utilities/hdevio_sample/hdevio_sample.cc
Warning:line 55, column 4
Potential leak of memory pointed to by 'hdevio'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -main-file-name hdevio_sample.cc -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /w/halld-scifs17exp/home/sdobbs/clang/llvm-project/install/lib/clang/12.0.0 -D HAVE_EVIO -D HAVE_CCDB -D HAVE_RCDB -D HAVE_TMVA=1 -D RCDB_MYSQL=1 -D RCDB_SQLITE=1 -D SQLITE_USE_LEGACY_STRUCT=ON -I .Linux_CentOS7.7-x86_64-gcc4.8.5/programs/Utilities/hdevio_sample -I programs/Utilities/hdevio_sample -I . -I libraries -I libraries/include -I /w/halld-scifs17exp/home/sdobbs/clang/halld_recon/Linux_CentOS7.7-x86_64-gcc4.8.5/include -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/evio/evio-4.4.6/Linux-x86_64/include -I external/xstream/include -I /usr/include/tirpc -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/root/root-6.08.06/include -I /w/halld-scifs17exp/halld2/home/sdobbs/Software/jana/jana_0.8.2/Linux_CentOS7.7-x86_64-gcc4.8.5/include -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/ccdb/ccdb_1.06.06/include -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/rcdb/rcdb_0.06.00/cpp/include -I /usr/include/mysql -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/sqlitecpp/SQLiteCpp-2.2.0^bs130/include -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/sqlite/sqlite-3.13.0^bs130/include -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/hdds/hdds-4.9.0/Linux_CentOS7.7-x86_64-gcc4.8.5/src -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/xerces-c/xerces-c-3.1.4/include -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5 -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5/x86_64-redhat-linux -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5/backward -internal-isystem /usr/local/include -internal-isystem /w/halld-scifs17exp/home/sdobbs/clang/llvm-project/install/lib/clang/12.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /home/sdobbs/work/clang/halld_recon/src -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -o /tmp/scan-build-2021-01-21-110224-160369-1 -x c++ programs/Utilities/hdevio_sample/hdevio_sample.cc
1
2#include <unistd.h>
3#include <stdlib.h>
4#include <stdint.h>
5#include <time.h>
6
7#include <iostream>
8#include <string>
9#include <vector>
10#include <stack>
11#include <thread>
12using namespace std;
13
14#include <TFile.h>
15
16#include <DAQ/HDEVIO.h>
17
18
19void Usage(string mess);
20void ParseCommandLineArguments(int narg, char *argv[]);
21
22
23vector<string> filenames;
24
25bool KEEP_BOR = true;
26bool KEEP_EPICS = false;
27bool KEEP_CODA = false;
28string OUTPUT_FILENAME = "hdevio_pare.evio";
29 uint64_t PRESCALE = 100;
30
31//----------------
32// main
33//----------------
34int main(int narg, char *argv[])
35{
36
37 ParseCommandLineArguments(narg, argv);
38
39 // Open output file
40 ofstream ofs(OUTPUT_FILENAME);
41 if(!ofs.is_open()){
1
Assuming the condition is false
2
Taking false branch
42 _DBG_cout<<"programs/Utilities/hdevio_sample/hdevio_sample.cc"
<<":"<<42<<" "
<< "Unable to open " << OUTPUT_FILENAME << " for writing!" << endl;
43 exit(-1);
44 }
45
46 // Create buffer for reading blocks into.
47 uint32_t *buff = new uint32_t[1000000];
48
49 // Loop over input files
50 for(auto filename : filenames){
51
52 // Open input file
53 HDEVIO *hdevio = new HDEVIO(filename);
3
Memory is allocated
54 if(!hdevio->is_open){
4
Assuming field 'is_open' is false
5
Taking true branch
55 cout << hdevio->err_mess.str() << endl;
6
Potential leak of memory pointed to by 'hdevio'
56 continue;
57 }
58
59 // Get file map
60 vector<HDEVIO::EVIOBlockRecord> brs = hdevio->GetEVIOBlockRecords();
61
62 // Have HDEVIO close file and open it ourselves
63 delete hdevio;
64 ifstream ifs(filename);
65
66 // Loop over blocks
67 uint64_t idx = 0;
68 for(auto &br : brs){
69
70 bool write_block = false;
71 switch(br.block_type){
72 case HDEVIO::kBT_BOR : if(KEEP_BOR ) write_block = true; break;
73 case HDEVIO::kBT_EPICS: if(KEEP_EPICS) write_block = true; break;
74 case HDEVIO::kBT_PRESTART:
75 case HDEVIO::kBT_GO:
76 case HDEVIO::kBT_PAUSE:
77 case HDEVIO::kBT_END:
78 if(KEEP_CODA) write_block = true;
79 break;
80 case HDEVIO::kBT_PHYSICS:
81 default:
82 break;
83 }
84
85 // Any events we're not keeping, prescale
86 if(!write_block){
87 write_block = ((idx++)%PRESCALE) == 0;
88 }
89
90 if(write_block){
91 ifs.seekg(br.pos, ios::beg);
92 int nbytes = br.block_len*4;
93 ifs.read((char*)buff, nbytes);
94 if( ifs.gcount() != nbytes ){
95 _DBG_cout<<"programs/Utilities/hdevio_sample/hdevio_sample.cc"
<<":"<<95<<" "
<<"Unable to read entire block (is file truncated?)" << endl;
96 }
97
98 ofs.write((char*)buff, nbytes);
99 }
100 }
101
102 ifs.close();
103 }
104
105 ofs.close();
106 if(buff) delete[] buff;
107
108 return 0;
109}
110
111//----------------
112// Usage
113//----------------
114void Usage(string mess="")
115{
116 cout << endl;
117 cout << "Usage:" << endl;
118 cout << endl;
119 cout <<" hdevio_pare [options] file.evio [file2.evio ...]" << endl;
120 cout << endl;
121 cout << "options:" << endl;
122 cout << " -h, --help Print this usage statement" << endl;
123 cout << " -o file.evio Set name of EVIO ouput file" << endl;
124 cout << " -p prescale Prescale factor for EVIO events (not L1 trigger events)" << endl;
125 cout << " -bor Don't save BOR events" << endl;
126 cout << " -epics Don't save EPICS events" << endl;
127 cout << " -coda Don't save CODA control events" << endl;
128 cout << endl;
129
130 if(mess != "") cout << endl << mess << endl << endl;
131
132 exit(0);
133}
134
135//----------------
136// ParseCommandLineArguments
137//----------------
138void ParseCommandLineArguments(int narg, char *argv[])
139{
140
141 if(narg<2) Usage("You must supply a filename!");
142
143 for(int i=1; i<narg; i++){
144 string arg = argv[i];
145 string next = (i+1)<narg ? argv[i+1]:"";
146
147 if(arg == "-h" || arg == "--help") Usage();
148 else if(arg == "-o" ){ OUTPUT_FILENAME = next.c_str(); i++;}
149 else if(arg == "-p" ){ PRESCALE = atoi(next.c_str()); i++;}
150 else if(arg == "-bor" ){ KEEP_BOR = false;}
151 else if(arg == "-epics"){ KEEP_EPICS = false;}
152 else if(arg == "-coda" ){ KEEP_CODA = false;}
153 else if(arg[0] == '-') {cout << "Unknown option \""<<arg<<"\" !" << endl; exit(-1);}
154 else filenames.push_back(arg);
155 }
156}
157