Bug Summary

File:programs/Utilities/hddm_merge_files/hddm_merge_files.cc
Location:line 53, column 26
Description:Dereference of null pointer

Annotated Source Code

1// $Id$
2//
3// Created Dec 22, 2007 David Lawrence
4
5#include <iostream>
6#include <iomanip>
7#include <vector>
8using namespace std;
9
10#include <signal.h>
11#include <time.h>
12
13#include "HDDM/hddm_s.h"
14
15void Smear(s_HDDM_t *hddm_s);
16void ParseCommandLineArguments(int narg, char* argv[]);
17void Usage(void);
18void ctrlCHandle(int x);
19
20vector<char*> INFILENAMES;
21char *OUTFILENAME = NULL__null;
22int QUIT = 0;
23
24
25#define _DBG_cout<<"hddm_merge_files.cc"<<":"<<25<<
" "
cout<<__FILE__"hddm_merge_files.cc"<<":"<<__LINE__25<<" "
26#define _DBG__cout<<"hddm_merge_files.cc"<<":"<<26<<
endl
cout<<__FILE__"hddm_merge_files.cc"<<":"<<__LINE__26<<endl
27
28
29//-----------
30// main
31//-----------
32int main(int narg,char* argv[])
33{
34 // Set up to catch SIGINTs for graceful exits
35 signal(SIGINT2,ctrlCHandle);
36
37 ParseCommandLineArguments(narg, argv);
38
39
40 // Output file
41 cout<<" output file: "<<OUTFILENAME<<endl;
42 s_iostream_t *fout = init_s_HDDM(OUTFILENAME);
43 if(!fout){
1
Taking false branch
44 cout<<" Error opening output file \""<<OUTFILENAME<<"\"!"<<endl;
45 exit(-1);
46 }
47
48 // Loop over input files
49 int NEvents = 0;
50 int NEvents_read = 0;
51 time_t last_time = time(NULL__null);
52 for(unsigned int i=0; i<INFILENAMES.size(); i++){
2
Loop condition is true. Entering loop body
53 cout<<" input file: "<<INFILENAMES[i]<<endl;
3
Dereference of null pointer
54 s_iostream_t *fin = open_s_HDDM(INFILENAMES[i]);
55 if(!fin){
56 cout<<" Error opening input file \""<<INFILENAMES[i]<<"\"!"<<endl;
57 exit(-1);
58 }
59
60 // Loop over all events in input
61 while(true){
62 s_HDDM_t *hddm_s = read_s_HDDM(fin);
63 if(!hddm_s)break;
64 NEvents_read++;
65
66 // Write this output event to file and free its memory
67 flush_s_HDDM(hddm_s, fout);
68 NEvents++;
69
70 // Update ticker
71 time_t now = time(NULL__null);
72 if(now != last_time){
73 cout<<" "<<NEvents_read<<" events read ("<<NEvents<<" event written) \r";cout.flush();
74 last_time = now;
75 }
76
77 if(QUIT)break;
78 }
79
80 // Close input file
81 close_s_HDDM(fin);
82 }
83
84 // Close output file
85 close_s_HDDM(fout);
86
87 cout<<" "<<NEvents<<" events read"<<endl;
88
89 return 0;
90}
91
92//-----------
93// ParseCommandLineArguments
94//-----------
95void ParseCommandLineArguments(int narg, char* argv[])
96{
97 INFILENAMES.clear();
98
99 for(int i=1; i<narg; i++){
100 char *ptr = argv[i];
101
102 if(ptr[0] == '-'){
103 switch(ptr[1]){
104 case 'h': Usage(); break;
105 case 'o': OUTFILENAME=&ptr[2]; break;
106 }
107 }else{
108 INFILENAMES.push_back(argv[i]);
109 }
110 }
111
112 if(INFILENAMES.size()==0){
113 cout<<endl<<"You must enter a filename!"<<endl<<endl;
114 Usage();
115 }
116
117 if(OUTFILENAME==NULL__null){
118 char *default_outfile = (char*)malloc(20);
119 OUTFILENAME = strcpy(default_outfile,"merged_files.hddm");
120 }
121}
122
123
124//-----------
125// Usage
126//-----------
127void Usage(void)
128{
129 cout<<endl<<"Usage:"<<endl;
130 cout<<" hddm_merge_files [-oOutputfile] file1.hddm file2.hddm ..."<<endl;
131 cout<<endl;
132 cout<<"options:"<<endl;
133 cout<<" -oOutputfile Set output filename (def. merged_files.hddm)"<<endl;
134 cout<<endl;
135 cout<<" This will merge 1 or more HDDM files into a single HDDM file."<<endl;
136 cout<<" "<<endl;
137 cout<<" "<<endl;
138 cout<<endl;
139
140 exit(0);
141}
142
143//-----------------------------------------------------------------
144// ctrlCHandle
145//-----------------------------------------------------------------
146void ctrlCHandle(int x)
147{
148 QUIT++;
149 cerr<<endl<<"SIGINT received ("<<QUIT<<")....."<<endl;
150}