Skip to content

Commit 20dcd1a

Browse files
committed
Add some bounds checks for CNF files.
Also check for zero file size for SPE files.
1 parent 1c93509 commit 20dcd1a

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/SpecFile_cnf.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,23 @@ void SpecFile::load_cnf_using_reader( CAMInputOutput::CAMIO &reader )
8181
meas->title_ = sampleid;
8282
if( sampleid.size() )
8383
meas->remarks_.push_back( "Sample ID: " + sampleid );
84-
}catch( std::exception &e )
84+
}catch( std::exception & )
8585
{
8686
// Will get here if no sample title
8787
}
8888

89-
// get the times
90-
meas->start_time_ = reader.GetAquisitionTime();
91-
float real_time = reader.GetRealTime();
92-
meas->real_time_ = real_time;
93-
meas->live_time_ = reader.GetLiveTime();
89+
90+
try
91+
{
92+
// Get the times - they are all in the same datablock, so if one throws, they will all throw.
93+
meas->start_time_ = reader.GetAquisitionTime();
94+
meas->real_time_ = reader.GetRealTime();
95+
meas->live_time_ = reader.GetLiveTime();
96+
}catch( std::exception &e )
97+
{
98+
// Will get here if no time block
99+
}
100+
94101
meas->sample_number_ = 1;
95102

96103
// set the energy calibration
@@ -193,7 +200,7 @@ void SpecFile::load_cnf_using_reader( CAMInputOutput::CAMIO &reader )
193200
{
194201
result.activity_ = activity;
195202
result.nuclide_ = cam_results[i].Name;
196-
result.real_time_ = real_time;
203+
result.real_time_ = meas->real_time_;
197204
result.detector_ = det_name;
198205

199206
new_det_ana->results_.push_back(result);
@@ -241,7 +248,7 @@ bool SpecFile::load_from_cnf( std::istream &input )
241248
cleanup_after_load();
242249
}catch ( std::exception &e )
243250
{
244-
cerr << "Failed CNF: " << e.what() << endl;
251+
//cerr << "Failed CNF: " << e.what() << endl;
245252
input.clear();
246253
//input.seekg( orig_pos, ios::beg );
247254

src/SpecFile_spe.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,20 @@ bool SpecFile::load_from_iaea( std::istream& istr )
269269
// gracious and check if maybe this is the case.
270270

271271
istr.seekg( 0, ios::end );
272-
const istream::pos_type eof_pos = istr.tellg();
272+
const istream::pos_type eof_pos = istr.tellg(); //orig_pos may be -1
273+
//if( !istr || (eof_pos < 0) )
274+
// throw runtime_error( "Error reading file" );
275+
273276
istr.seekg( orig_pos, ios::beg );
274-
275-
const size_t filesize = static_cast<size_t>( 0 + eof_pos - orig_pos );
276-
277+
const size_t filesize = static_cast<size_t>( 0 + std::max(eof_pos,istream::pos_type(0)) - std::max(orig_pos,istream::pos_type(0)) );
278+
const size_t headerdata_size = std::min( size_t(2048), filesize + 1 );
279+
277280
string headerdata;
278-
headerdata.resize( std::min( size_t(2048), filesize + 1 ) );
281+
headerdata.resize( headerdata_size );
279282
istr.read( &(headerdata[0]), headerdata.size() - 1 );
280283
istr.seekg( 0, ios::beg );
281284
headerdata.back() = '\0'; //JIC
282-
285+
283286
is_ncf = ((headerdata.find("EXPTID:") != string::npos)
284287
&& (headerdata.find("SAMPID:") != string::npos)
285288
&& (headerdata.find("DATA:") != string::npos));

0 commit comments

Comments
 (0)