Bug Summary

File:/home/sdobbs/work/clang/halld_recon/src/programs/Utilities/hdevio_scan/hdevio_scan.cc
Warning:line 146, 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_scan.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_scan -I programs/Utilities/hdevio_scan -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++0x -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_scan/hdevio_scan.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 "DMapEVIOWords.h"
17
18#include <DAQ/HDEVIO.h>
19
20
21void Usage(string mess);
22void ParseCommandLineArguments(int narg, char *argv[]);
23void PrintSummary(void);
24void MapEVIOWords(void);
25
26
27vector<string> filenames;
28bool PRINT_SUMMARY = true;
29bool SAVE_FILE_MAP = false;
30bool SKIP_EVENT_MAPPING = false;
31bool MAP_WORDS = false;
32bool GENERATE_ERROR_REPORT = false;
33string ROOT_FILENAME = "hdevio_scan.root";
34string MAP_FILENAME = "";
35uint64_t MAX_EVIO_EVENTS = 20000;
36uint64_t SKIP_EVIO_EVENTS = 0;
37uint32_t BLOCK_SIZE = 20; // used for daq_block_size histogram
38uint32_t MAX_HISTORY_BUFF_SIZE = 400;
39int32_t RUNNUMBER = (1<<30);
40
41//----------------
42// main
43//----------------
44int main(int narg, char *argv[])
45{
46
47 ParseCommandLineArguments(narg, argv);
48
49 if(PRINT_SUMMARY) PrintSummary();
1
Assuming 'PRINT_SUMMARY' is true
2
Taking true branch
3
Calling 'PrintSummary'
50
51 if(MAP_WORDS ) MapEVIOWords();
52
53 return 0;
54}
55
56//----------------
57// Usage
58//----------------
59void Usage(string mess="")
60{
61 cout << endl;
62 cout << "Usage:" << endl;
63 cout << endl;
64 cout <<" hdevio [options] file.evio [file2.evio ...]" << endl;
65 cout << endl;
66 cout << "options:" << endl;
67 cout << " -h, --help Print this usage statement" << endl;
68 cout << " -w Make histogram of population by word type" << endl;
69 cout << " -r file.root Set name of ROOT file to save histo to. " << endl;
70 cout << " (implies -w)" << endl;
71 cout << " -m max_events Max. EVIO events (not physics events) to process." << endl;
72 cout << " -i ignore_events Num. EVIO events (not physics events) to ignore at start." << endl;
73 cout << " -b block_size EVIO events to add for daq_block_size histo." << endl;
74 cout << " -e Write details of bad event tag location to file" << endl;
75 cout << " -n max_buff Max. events to keep timing info for (only valid)" << endl;
76 cout << " with -w option)" << endl;
77 cout << " -s Save file block/event map" << endl;
78 cout << " -blocksonly Save only block map not events. (Only use with -s)" << endl;
79 cout << " -f file.map Set name of file to save block/event to. " << endl;
80 cout << " (implies -s)" << endl;
81 cout << " -R RUNNUMBER Set the run number used to access the TTAB in the CCDB" << endl;
82 cout << endl;
83 cout << "n.b. When using the -i (ignore) flag, the total number of events" << endl;
84 cout << " read in will be the sum of how many are ignored and the \"max\"" << endl;
85 cout << " events with only the last max_events being processed." << endl;
86 cout << endl;
87
88 if(mess != "") cout << endl << mess << endl << endl;
89
90 exit(0);
91}
92
93//----------------
94// ParseCommandLineArguments
95//----------------
96void ParseCommandLineArguments(int narg, char *argv[])
97{
98
99 if(narg<2) Usage("You must supply a filename!");
100
101 for(int i=1; i<narg; i++){
102 string arg = argv[i];
103 string next = (i+1)<narg ? argv[i+1]:"";
104
105 if(arg == "-h" || arg == "--help") Usage();
106 else if(arg == "-w"){ MAP_WORDS = true; PRINT_SUMMARY = false; }
107 else if(arg == "-r"){ MAP_WORDS = true; PRINT_SUMMARY = false; ROOT_FILENAME = next; i++;}
108 else if(arg == "-i"){ SKIP_EVIO_EVENTS = atoi(next.c_str()); i++;}
109 else if(arg == "-m"){ MAX_EVIO_EVENTS = atoi(next.c_str()); i++;}
110 else if(arg == "-b"){ BLOCK_SIZE = atoi(next.c_str()); i++;}
111 else if(arg == "-e"){ GENERATE_ERROR_REPORT = true; }
112 else if(arg == "-n"){ MAX_HISTORY_BUFF_SIZE = atoi(next.c_str()); i++;}
113 else if(arg == "-s"){ SAVE_FILE_MAP = true;}
114 else if(arg == "-f"){ SAVE_FILE_MAP = true; MAP_FILENAME = next; i++;}
115 else if(arg == "-R"){ RUNNUMBER = atoi(next.c_str()); i++;}
116 else if(arg == "-blocksonly") { SKIP_EVENT_MAPPING = true;}
117 else if(arg[0] == '-') {cout << "Unknown option \""<<arg<<"\" !" << endl; exit(-1);}
118 else filenames.push_back(arg);
119 }
120
121 // If user did not set run number, try extracting it from file
122 if(RUNNUMBER >= (1<<30)){
123 if(!filenames.empty()){
124 cout << "No run number given, trying to extract from filename: " << filenames[0] << endl;
125 auto pos = filenames[0].find("hd_rawdata_");
126 if(pos != string::npos ){
127 auto runstr = filenames[0].substr( pos+11, 6);
128 RUNNUMBER = atoi( runstr.c_str() );
129 }
130 }
131 }
132}
133
134//----------------
135// PrintSummary
136//----------------
137void PrintSummary(void)
138{
139 // Loop over input files
140 for(uint32_t i=0; i<filenames.size(); i++){
4
Assuming the condition is true
5
Loop condition is true. Entering loop body
141 string &filename = filenames[i];
142 cout << "Processing file " << (i+1) << "/" << filenames.size() << " : " << filename << endl;
143
144 HDEVIO *hdevio = new HDEVIO(filename);
6
Memory is allocated
145 if(!hdevio->is_open){
7
Assuming field 'is_open' is false
8
Taking true branch
146 cout << hdevio->err_mess.str() << endl;
9
Potential leak of memory pointed to by 'hdevio'
147 continue;
148 }
149
150 if(SKIP_EVENT_MAPPING) hdevio->SKIP_EVENT_MAPPING = true;
151
152 time_t start_time = time(NULL__null);
153 hdevio->PrintFileSummary();
154 time_t end_time = time(NULL__null);
155
156 if(GENERATE_ERROR_REPORT){
157 ofstream ofs("hdevio_scan.err");
158 ofs << "#";
159 ofs << "# hdevio_scan report for " << filename << endl;
160 ofs << "#";
161 ofs << "# The following list is for each EVIO event that contained an" << endl;
162 ofs << "# unknown top-level bank tag. Since any corruption may have" << endl;
163 ofs << "# started in the previous event, locations for that are also" << endl;
164 ofs << "# provided (when available)." <<endl;
165 ofs << "# columns are:" << endl;
166 ofs << "# 1 block number in file (starting from 0)" << endl;
167 ofs << "# 2 block offset in file (hex)" <<endl;
168 ofs << "# 3 block number of previous event" << endl;
169 ofs << "# 4 block offset of previous event" <<endl;
170 ofs << "# 5 event number in block (starting from 1)" << endl;
171 ofs << "# 6 number of events in block" << endl;
172 ofs << "# 7 event offset in file (hex)" <<endl;
173 ofs << "# 8 event length (inclusive)" <<endl;
174 ofs << "# 9 event header (hex) <-- This value is what indicates a corrupt event" <<endl;
175 ofs << "# 10 event number of previous event" << endl;
176 ofs << "# 11 event offset of previous event" <<endl;
177 ofs << "# 12 event length of previous event" <<endl;
178 ofs << "# 13 event header of previous event" <<endl;
179 ofs << "#" << endl;
180 ofs << "# 1 2 3 4 5 6 7 8 9 10 11 12 13" <<endl;
181
182 vector<HDEVIO::EVIOBlockRecord> brs = hdevio->GetEVIOBlockRecords();
183
184 HDEVIO::EVIOBlockRecord *br_prev = NULL__null;
185 HDEVIO::EVIOEventRecord *er_prev = NULL__null;
186 int32_t ibr = 0;
187 for(HDEVIO::EVIOBlockRecord &br : brs){
188 int32_t ier = 1;
189 for(HDEVIO::EVIOEventRecord &er : br.evio_events){
190 if(er.event_type == HDEVIO::kBT_UNKNOWN){
191 char str[512];
192 // 1 2 3 4 5 6 7 8 9 10 11 12 13
193 sprintf(str, "%04u 0x%08x %04d 0x%08x %3u/%3u 0x%08x %10u 0x%08x %03d 0x%08x %10u 0x%08x"
194 /* 1 */ , ibr
195 /* 2 */ , (unsigned int)br.pos
196 /* 3 */ , ibr-1
197 /* 4 */ , br_prev!=NULL__null ? (unsigned int)br_prev->pos:0
198 /* 5 */ , ier
199 /* 6 */ , (unsigned int)br.evio_events.size()
200 /* 7 */ , (unsigned int)er.pos
201 /* 8 */ , (unsigned int)er.event_len
202 /* 9 */ , (unsigned int)er.event_header
203 /* 10 */ , ier-1
204 /* 11 */ , er_prev!=NULL__null ? (unsigned int)er_prev->pos:0
205 /* 12 */ , er_prev!=NULL__null ? (unsigned int)er_prev->event_len:0
206 /* 13 */ , er_prev!=NULL__null ? (unsigned int)er_prev->event_header:0);
207 ofs << str << endl;
208 }
209
210 er_prev = &er;
211 ier++;
212 }
213
214 br_prev = &br;
215 ibr++;
216 }
217
218 ofs.close();
219 }
220
221 if(SAVE_FILE_MAP) hdevio->SaveFileMap(MAP_FILENAME);
222
223 delete hdevio;
224
225 cout << (end_time - start_time) << " sec " << endl;
226 }
227}
228
229//----------------
230// MapEVIOWords
231//----------------
232void MapEVIOWords(void)
233{
234 cout << endl;
235 cout << "Mapping will be limited to first " << MAX_EVIO_EVENTS << " events per input file" << endl;
236
237 // Open ROOT file
238 TFile *rootfile = new TFile(ROOT_FILENAME.c_str(), "RECREATE");
239
240 DMapEVIOWords mapevio;
241 mapevio.max_history_buff_size = MAX_HISTORY_BUFF_SIZE;
242
243 // Loop over input files
244 uint64_t Nevents = 0;
245 for(uint32_t i=0; i<filenames.size(); i++){
246
247 // Open EVIO file
248 string &filename = filenames[i];
249 cout << "Processing file " << (i+1) << "/" << filenames.size() << " : " << filename << endl;
250 HDEVIO *hdevio = new HDEVIO(filename);
251 if(!hdevio->is_open){
252 cout << hdevio->err_mess.str() << endl;
253 continue;
254 }
255
256 // Read all events in file
257 uint32_t buff_len = 1000;
258 uint32_t *buff = new uint32_t[buff_len];
259 bool done = false;
260 while(!done){
261 hdevio->readNoFileBuff(buff, buff_len);
262 switch(hdevio->err_code){
263 case HDEVIO::HDEVIO_OK:
264 if( Nevents>= SKIP_EVIO_EVENTS ) mapevio.ParseEvent(buff);
265 break;
266 case HDEVIO::HDEVIO_USER_BUFFER_TOO_SMALL:
267 buff_len = hdevio->last_event_len;
268 delete[] buff;
269 buff = new uint32_t[buff_len];
270 break;
271 case HDEVIO::HDEVIO_EOF:
272 cout << endl << " end of file" << endl;
273 done = true;
274 break;
275 default:
276 cout << endl;
277 cout << hdevio->err_mess.str() << endl;
278 done = true;
279 break;
280
281 }
282
283 if((++Nevents % 1000) == 0) {
284 int percent_done = (100*Nevents)/MAX_EVIO_EVENTS;
285 cout << " " << Nevents << "/" << MAX_EVIO_EVENTS << " (" << percent_done << "%) processed \r"; cout.flush();
286 }
287 if( Nevents > (SKIP_EVIO_EVENTS+MAX_EVIO_EVENTS) ) break;
288 }
289 cout << endl;
290
291 // Close EVIO file
292 delete hdevio;
293 }
294
295 // Allow some histograms to be scaled.
296 mapevio.Finish();
297
298 // Flush and close ROOT file
299 rootfile->Write();
300 rootfile->Close();
301 delete rootfile;
302}
303
304
305