Skip to content

Commit 6fac488

Browse files
committed
Small fixes from fuzzing and regression tests.
1 parent 9d15094 commit 6fac488

8 files changed

+45
-16
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ set_target_properties( SpecUtils PROPERTIES PREFIX "lib" OUTPUT_NAME "SpecUtils"
231231
if( SpecUtils_USE_FROM_CHARS )
232232
if( NOT (MSVC AND (MSVC_VERSION GREATER_EQUAL 1920)) )
233233
# MSVC >=2019 supports floating point from_chars, while Xcode 14 still doesnt (havent checked Android Studio)
234-
message("Please dopuble check your compiler does actually support std::from_chars for parsing floats")
234+
message("Please double check your compiler does actually support std::from_chars for parsing floats")
235235
endif()
236236

237237
set_target_properties( SpecUtils PROPERTIES

SpecUtils/ParseUtils.h

+4
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ namespace SpecUtils
125125
ex: "Title: FA-SG-LANL-0-0-8{dx=-155.6579,dy=-262.5} @235cm H=262.5cm V=221.1404cm/s : Det=Ba2"
126126
127127
TODO: does not currently take into account units (e.g., cm)
128+
129+
Throws exception if not successful.
128130
*/
129131
float dx_from_remark( std::string remark );
130132

@@ -133,6 +135,8 @@ namespace SpecUtils
133135
ex: "Title: FA-SG-LANL-0-0-8{dx=-155.6579,dy=-262.5} @235cm H=262.5cm V=221.1404cm/s : Det=Ba2"
134136
135137
TODO: does not currently take into account units (e.g., cm)
138+
139+
Throws exception if not successful.
136140
*/
137141
float dy_from_remark( std::string remark );
138142

fuzz_test/CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ cmake_policy(SET CMP0079 NEW) #allow altering SpecUtils linking from this file
55

66
add_executable( file_parse_fuzz file_parse_fuzz.cpp fuzz_interface.h fuzz_interface.cpp )
77

8+
# spirit float parser can assert with inputs like
9+
# ".000000000000000000000000000000000000000000000000000000000000000000000000000"
10+
# when we compile as RelWithDebInfo; so we'll disable this asserting for fuzzing
11+
target_compile_definitions( SpecUtils PUBLIC BOOST_DISABLE_ASSERTS )
12+
813
target_compile_options( file_parse_fuzz PRIVATE $<$<CXX_COMPILER_ID:Clang>:-g -O1 -fsanitize=fuzzer,address,undefined -fprofile-instr-generate -fcoverage-mapping> )
914
target_compile_options( SpecUtils PRIVATE $<$<CXX_COMPILER_ID:Clang>:-g -O1 -fsanitize=fuzzer,address,undefined -fprofile-instr-generate -fcoverage-mapping> )
1015
target_link_libraries( SpecUtils PUBLIC $<$<CXX_COMPILER_ID:Clang>:-fsanitize=fuzzer,address,undefined -fprofile-instr-generate -fcoverage-mapping > )
1116
target_link_libraries( file_parse_fuzz PRIVATE SpecUtils $<$<CXX_COMPILER_ID:Clang>:-fsanitize=fuzzer,address,undefined> )
1217
set_target_properties( file_parse_fuzz PROPERTIES CXX_STANDARD 14 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO )
1318

14-
1519
#add_executable( run_one_file_parse run_one_file_parse.cpp fuzz_interface.h fuzz_interface.cpp )
1620
#target_link_libraries( run_one_file_parse PRIVATE SpecUtils )
1721
#set_target_properties( run_one_file_parse PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO )
1822

1923
add_executable( fuzz_str_utils fuzz_str_utils.cpp )
20-
target_link_libraries( fuzz_str_utils PRIVATE SpecUtils $<$<CXX_COMPILER_ID:Clang>:-fsanitize=fuzzer,address,undefined> )
24+
target_link_libraries( fuzz_str_utils PRIVATE SpecUtils $<$<CXX_COMPILER_ID:Clang>:-fsanitize=fuzzer,address,undefined -fprofile-instr-generate -fcoverage-mapping> )
2125
set_target_properties( fuzz_str_utils PROPERTIES CXX_STANDARD 14 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO )

fuzz_test/fuzz_interface.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,20 @@ int run_file_parse_fuzz( const uint8_t *data, size_t size )
275275
test_write_output( spec );
276276
}
277277

278+
{
279+
SpecUtils::SpecFile spec;
280+
stringstream strm( datastr, ios_base::in );
281+
if( spec.load_from_json( strm ) )
282+
test_write_output( spec );
283+
}
284+
285+
{
286+
SpecUtils::SpecFile spec;
287+
stringstream strm( datastr, ios_base::in );
288+
if( spec.load_from_caen_gxml( strm ) )
289+
test_write_output( spec );
290+
}
291+
278292

279293
return 0;
280294
}

fuzz_test/fuzz_str_utils.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,9 @@ extern "C" int LLVMFuzzerTestOneInput( const uint8_t *data, size_t size )
649649

650650
print_waypoint( 48 );
651651

652-
speed_from_remark( datastr );
653-
speed_from_remark( str_1 );
654-
speed_from_remark( str_2 );
652+
try{ speed_from_remark( datastr ); }catch(std::exception &){}
653+
try{ speed_from_remark( str_1 ); }catch(std::exception &){}
654+
try{ speed_from_remark( str_2 ); }catch(std::exception &){}
655655

656656
print_waypoint( 49 );
657657

@@ -661,19 +661,19 @@ extern "C" int LLVMFuzzerTestOneInput( const uint8_t *data, size_t size )
661661

662662
print_waypoint( 50 );
663663

664-
dx_from_remark( datastr );
665-
dx_from_remark( str_1 );
666-
dx_from_remark( str_2 );
664+
try{ dx_from_remark( datastr ); }catch(std::exception &){}
665+
try{ dx_from_remark( str_1 ); }catch(std::exception &){}
666+
try{ dx_from_remark( str_2 ); }catch(std::exception &){}
667667

668668
print_waypoint( 51 );
669669

670-
dy_from_remark( datastr );
671-
dy_from_remark( str_1 );
672-
dy_from_remark( str_2 );
670+
try{ dy_from_remark( datastr ); }catch(std::exception &){}
671+
try{ dy_from_remark( str_1 ); }catch(std::exception &){}
672+
try{ dy_from_remark( str_2 ); }catch(std::exception &){}
673673

674674
print_waypoint( 52 );
675675

676-
dose_units_usvPerH( (const char *)data, size );
676+
try{ dose_units_usvPerH( (const char *)data, size ); }catch(std::exception &){}
677677
//dose_units_usvPerH( datastr.c_str(), datastr.size() );
678678
//dose_units_usvPerH( str_1.c_str(), str_1.size() );
679679
//dose_units_usvPerH( str_2.c_str(), str_2.size() );

src/Filesystem.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,13 @@ std::string parent_path( const std::string &path )
543543
while( strcmp(bname,"..") == 0 )
544544
{
545545
char *parname = dirname( &(pathvec[0]) );
546+
if( !parname )
547+
{
548+
// I think we can get here for like invalid utf-8 characters or something, or maybe
549+
// just really long input, or maybe invalid paths; not sure, only hit this during fuzzing
550+
break;
551+
}
552+
546553
size_t newlen = strlen(parname);
547554
pathvec.resize( newlen + 1 );
548555
pathvec[newlen] = '\0';

src/SpecFile_json.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ bool SpecFile::load_from_json( std::istream &input )
306306
meas->parse_warnings_.insert(end(meas->parse_warnings_), begin(warnings), end(warnings));
307307

308308
measurements_.push_back(meas);
309-
instrument_type_ = "Gamma Handheld";
309+
instrument_type_ = "";
310310
manufacturer_ = "Bridgeport Instruments";
311311
instrument_model_ = "eMorpho";
312312
instrument_id_ = serial_number;
@@ -324,7 +324,7 @@ bool SpecFile::load_from_json( std::istream &input )
324324
}//try / catch
325325

326326
return false;
327-
}//bool load_from_aram( std::istream &input )
327+
}//bool load_from_json( std::istream &input )
328328
}//namespace SpecUtils
329329

330330

src/SpecFile_xml_other.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ bool SpecFile::load_from_caen_gxml(std::istream& input)
626626
remarks_ = remarks;
627627
manufacturer_ = "CAEN";
628628
instrument_model_ = "Hexagon";
629-
instrument_type_ = "Other";
629+
instrument_type_ = "";
630630
detector_type_ = DetectorType::Unknown;
631631

632632
cleanup_after_load();

0 commit comments

Comments
 (0)