Skip to content

Commit 2641dbb

Browse files
committed
Fix some minor warnings/issues.
Also exposed some string utils for exporting spectra to HTML/JS, to the fuzzing routine.
1 parent 5e69466 commit 2641dbb

8 files changed

+123
-45
lines changed

SpecUtils/D3SpectrumExport.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424
#ifndef D3SpectrumExport_h
2525
#define D3SpectrumExport_h
2626

27+
#include "SpecUtils_config.h"
28+
2729
#include <map>
2830
#include <vector>
2931
#include <string>
3032
#include <utility>
3133
#include <ostream>
3234

33-
#include "SpecUtils_config.h"
35+
#if( SpecUtils_BUILD_FUZZING_TESTS )
36+
#include <sstream>
37+
#endif
3438

3539

3640
namespace SpecUtils
@@ -48,7 +52,7 @@ namespace D3SpectrumExport
4852
struct D3SpectrumChartOptions;
4953

5054
//Writes the spectrum component of the js that SpectrumChartD3 expects.
51-
// An anotatated example of what would be written is:
55+
// An annotated example of what would be written is:
5256
// { "title": "Line Title", "peaks":"[...]", "liveTime": 300, "realTime": 320, "neutrons": 0,
5357
// "lineColor": "black", "x": [0, 2.93, 5.86, ..., 3000], "y": [0,1,10,...0], "yScaleFactor": 1.0 }
5458
bool write_spectrum_data_js( std::ostream &ostr,
@@ -188,5 +192,11 @@ namespace D3SpectrumExport
188192

189193
std::map<std::string,std::string> m_reference_lines_json; //map from nuclide to their JSON
190194
};//struct D3SpectrumChartOptions
195+
196+
#if( SpecUtils_BUILD_FUZZING_TESTS )
197+
std::string escape_text_test( const std::string &input );
198+
void copy_check_utf8_test( const char * &src, char * &dest );
199+
void sanitize_unicode_test( std::stringstream &sout, const std::string& text );
200+
#endif
191201
}//namespace D3SpectrumExport
192202
#endif //D3SpectrumExport

SpecUtils/StringAlgo.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ namespace SpecUtils
314314
bool split_to_floats( const char *input, const size_t length,
315315
std::vector<float> &results );
316316

317-
/* \brief A convienience function. */
317+
/* \brief A convenience function. */
318318
bool split_to_floats( const std::string &input, std::vector<float> &results );
319319

320320

fuzz_test/fuzz_str_utils.cpp

+21-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@
2727

2828
#include <random>
2929

30+
#include "SpecUtils/DateTime.h"
3031
#include "SpecUtils/StringAlgo.h"
3132
#include "SpecUtils/ParseUtils.h"
3233
#include "SpecUtils/Filesystem.h"
33-
#include "SpecUtils/DateTime.h"
34-
34+
#if( SpecUtils_ENABLE_D3_CHART )
35+
#include "SpecUtils/D3SpectrumExport.h"
36+
#else
37+
static_assert( 0, "You should enable SpecUtils_ENABLE_D3_CHART" );
38+
#endif
3539

3640
using namespace std;
3741

@@ -692,6 +696,21 @@ extern "C" int LLVMFuzzerTestOneInput( const uint8_t *data, size_t size )
692696
//std::istream &read_binary_data( std::istream &input, T &val );
693697
//size_t write_binary_data( std::ostream &input, const T &val );
694698

699+
#if( SpecUtils_ENABLE_D3_CHART )
700+
{
701+
D3SpectrumExport::escape_text_test( datastr );
702+
703+
char buf[4];
704+
for( const char *c = datastr.c_str(); *c; )
705+
{
706+
char *b = buf;
707+
D3SpectrumExport::copy_check_utf8_test(c, b);
708+
}
709+
710+
std::stringstream outstrm;
711+
D3SpectrumExport::sanitize_unicode_test( outstrm, datastr );
712+
}
713+
#endif
695714

696715
return 0;
697716
}//LLVMFuzzerTestOneInput

src/D3SpectrumExport.cpp

+75-27
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include "SpecUtils_config.h"
2121

22+
#include <iostream>
23+
2224
#include <cmath>
2325
#include <stack>
2426
#include <limits>
@@ -45,8 +47,8 @@ using namespace std;
4547

4648
namespace
4749
{
48-
//anaonomous namespace for functions to help parse D3.js HTML files, that wont be
49-
// usefull outside of this file
50+
//anonymous namespace for functions to help parse D3.js HTML files, that wont be
51+
// useful outside of this file
5052
#if( SpecUtils_D3_SUPPORT_FILE_STATIC )
5153
const unsigned char * const ns_libJsFiles[] = {
5254
D3_MIN_JS // D3.js library
@@ -82,19 +84,31 @@ namespace
8284

8385
//Taken from the rapidxml.hpp that Wt uses
8486
template <class Ch>
85-
void copy_check_utf8(const Ch *& src, Ch *& dest)
87+
void copy_check_utf8( const Ch *& src, Ch *& dest )
8688
{
8789
// skip entire UTF-8 encoded characters at once,
8890
// checking their validity based on
8991
// http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/character-encoding.html (5.9.4 column 3)
9092

93+
assert( src );
94+
size_t src_len;
95+
for( src_len = 0; src[src_len] && src_len < 4; ++src_len )
96+
{
97+
}
98+
assert( src_len );
99+
if( !src_len )
100+
return;
101+
102+
91103
unsigned length = 1;
92104
bool legal = false;
93-
if ((unsigned char)src[0] <= 0x7F) {
105+
if ((unsigned char)src[0] <= 0x7F)
106+
{
94107
unsigned char c = src[0];
95108
if (c == 0x09 || c == 0x0A || c == 0x0D || c >= 0x20)
96109
legal = true;
97-
} else if ((unsigned char)src[0] >= 0xF0) {
110+
}else if( ((unsigned char)src[0] >= 0xF0) && (src_len >= 4) )
111+
{
98112
length = 4;
99113

100114
if ((
@@ -120,7 +134,8 @@ namespace
120134
))
121135
legal = true;
122136

123-
} else if ((unsigned char)src[0] >= 0xE0) {
137+
}else if( ((unsigned char)src[0] >= 0xE0) && (src_len >= 3) )
138+
{
124139
length = 3;
125140

126141
if ((
@@ -142,7 +157,8 @@ namespace
142157
))
143158
legal = true;
144159

145-
} else if ((unsigned char)src[0] >= 0xC0) {
160+
}else if( ((unsigned char)src[0] >= 0xC0) && (src_len >= 2) )
161+
{
146162
length = 2;
147163

148164
if (
@@ -155,9 +171,12 @@ namespace
155171
legal = true;
156172
}
157173

158-
if (legal) {
159-
if (dest) {
160-
if (length == 3) {
174+
if( legal )
175+
{
176+
if( dest )
177+
{
178+
if( length == 3 )
179+
{
161180
/*
162181
U+2028 and U+2029 may cause problems, they are line
163182
separators that mess up JavaScript string literals.
@@ -172,44 +191,56 @@ namespace
172191
} else
173192
for (unsigned i = 0; i < length; ++i)
174193
*dest++ = *src++;
175-
} else
176-
for (unsigned i = 0; i < length; ++i)
194+
}else
195+
{
196+
for( unsigned i = 0; i < length; ++i )
177197
*dest++ = *src++;
178-
} else
198+
}
199+
}else
200+
{
179201
src += length;
180-
} else {
181-
if (dest)
182-
if (length >= 3) {
202+
}
203+
}else //if( legal )
204+
{
205+
if( dest )
206+
{
207+
if( length >= 3 )
208+
{
183209
/* insert U+FFFD, the replacement character */
184210
*dest++ = (Ch)0xef;
185211
*dest++ = (Ch)0xbf;
186212
*dest++ = (Ch)0xbd;
187213
src += length;
188-
} else
189-
for (unsigned i = 0; i < length; ++i) {
214+
}else
215+
{
216+
for (unsigned i = 0; i < length; ++i)
217+
{
190218
*dest++ = '?';
191219
src++;
192220
}
193-
else {
194-
//const Ch *problem_src = src;
195-
src += length;
196-
throw runtime_error( "Invalid UTF-8 sequence" /* + const_cast<Ch *>(problem_src)*/ );
197221
}
198-
}
199-
}
200-
201-
222+
}else
223+
{
224+
//const Ch *problem_src = src;
225+
src += length;
226+
throw runtime_error( "Invalid UTF-8 sequence" /* + const_cast<Ch *>(problem_src)*/ );
227+
}//if( dest )
228+
}//if( legal ) / else
229+
}//void copy_check_utf8
202230

203231
void sanitize_unicode( stringstream &sout, const std::string& text )
204232
{
205233
char buf[4];
206234

207235
for (const char *c = text.c_str(); *c;) {
236+
assert( c <= (text.c_str() + text.size()) );
208237
char *b = buf;
209238
// but copy_check_utf8() does not declare the following ranges illegal:
210239
// U+D800-U+DFFF
211240
// U+FFFE-U+FFFF
212241
copy_check_utf8<char>(c, b);
242+
assert( c <= (text.c_str() + text.size()) );
243+
assert( (b - buf) <= 4 );
213244
for (char *i = buf; i < b; ++i)
214245
sout << *i;
215246
}
@@ -264,7 +295,24 @@ namespace
264295

265296
namespace D3SpectrumExport
266297
{
267-
298+
#if( SpecUtils_BUILD_FUZZING_TESTS )
299+
std::string escape_text_test( const std::string &input )
300+
{
301+
return escape_text( input );
302+
}
303+
304+
void copy_check_utf8_test( const char *& src, char *& dest )
305+
{
306+
copy_check_utf8( src, dest );
307+
}
308+
309+
void sanitize_unicode_test( std::stringstream &sout, const std::string& text )
310+
{
311+
sanitize_unicode( sout, text );
312+
}
313+
#endif
314+
315+
268316
#if( SpecUtils_D3_SUPPORT_FILE_STATIC )
269317
const unsigned char *d3_js(){ return D3_MIN_JS; }
270318
const unsigned char *spectrum_chart_d3_js(){ return SPECTRUM_CHART_D3_JS; }

src/SpecFile_csv.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ bool SpecFile::load_from_D3S_raw( std::istream &input )
409409

410410
// This next call will check that energies are monotonically increasing, and if not throw
411411
// an exception.
412-
energy_cal->set_lower_channel_energy( channel_energies.size(), move(channel_energies) );
412+
energy_cal->set_lower_channel_energy( channel_energies.size(), std::move(channel_energies) );
413413
}catch(std::exception &e)
414414
{
415415
energy_cal.reset();

src/SpecFile_gr135.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ bool SpecFile::write_binary_exploranium_gr135v2( std::ostream &output ) const
10201020

10211021
memcpy( buffer + 19, &noutchannel, sizeof(noutchannel) );
10221022

1023-
double rt_f = std::round( 1000 * meas.real_time_ );
1023+
double rt_f = std::round( 1000 * std::max(meas.real_time_, 0.0f) );
10241024
rt_f = std::min( rt_f, static_cast<double>(std::numeric_limits<uint32_t>::max()) );
10251025

10261026
const uint32_t real_time_thousanths = static_cast<uint32_t>( rt_f );

src/SpecFile_n42.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ void add_spectra_to_measurement_node_in_2012_N42_xml( ::rapidxml::xml_node<char>
11631163

11641164
string channeldata;
11651165
if( !zerocompressed )
1166-
channeldata.reserve( 3*m->gamma_counts()->size() ); //3 has not been verified to be reasonalbe
1166+
channeldata.reserve( 3*m->gamma_counts()->size() ); //3 has not been verified to be reasonable
11671167

11681168
const size_t nchannel = data.size();
11691169

src/SpecFile_spe.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,13 @@ bool SpecFile::load_from_iaea( std::istream& istr )
364364
vector<string> channelstrs;
365365
split( channelstrs, line, " \t," );
366366

367-
unsigned int firstchannel = 0, lastchannel = 0;
367+
int firstchannel = 0, lastchannel = 0;
368368
if( channelstrs.size() == 2 )
369369
{
370-
try
371-
{
372-
firstchannel = atol( channelstrs[0].c_str() );
373-
lastchannel = atol( channelstrs[1].c_str() );
374-
}catch(...)
370+
if( !parse_int(channelstrs[0].c_str(), channelstrs[0].size(), firstchannel)
371+
|| !parse_int(channelstrs[1].c_str(), channelstrs[1].size(), lastchannel) )
375372
{
373+
firstchannel = lastchannel = 0;
376374
}
377375
}else
378376
{
@@ -382,11 +380,14 @@ bool SpecFile::load_from_iaea( std::istream& istr )
382380

383381
double sum = 0.0;
384382
std::shared_ptr< vector<float> > channel_data( new vector<float>() );
385-
if( firstchannel < lastchannel )
386-
channel_data->reserve( lastchannel - firstchannel + 1 );
383+
if( (firstchannel < lastchannel)
384+
&& (firstchannel >= 0)
385+
&& ((lastchannel - firstchannel) < (65536 + 2)) )
386+
{
387+
channel_data->reserve( static_cast<size_t>(lastchannel - firstchannel) + 1 );
388+
}
387389

388-
//XXX - for some reason I think this next test condition is a little
389-
// fragile...
390+
//TODO: for some reason I think this next test condition is a little fragile...
390391
int num_cd_error = 0, num_cd_error_current = 0;
391392
while( SpecUtils::safe_get_line( istr, line ) )
392393
{

0 commit comments

Comments
 (0)