diff --git a/nifti2/CMakeLists.txt b/nifti2/CMakeLists.txt index 6028291d..6477a8a1 100644 --- a/nifti2/CMakeLists.txt +++ b/nifti2/CMakeLists.txt @@ -46,6 +46,10 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) add_executable(${NIFTI_PACKAGE_PREFIX}clib_02_nifti2 clib_02_nifti2.c) target_link_libraries(${NIFTI_PACKAGE_PREFIX}clib_02_nifti2 PUBLIC ${NIFTI_NIFTILIB2_NAME}) + add_executable(${NIFTI_PACKAGE_PREFIX}nifti2_from_ascii_test nifti2_from_ascii_test.c) + target_link_libraries(${NIFTI_PACKAGE_PREFIX}nifti2_from_ascii_test PUBLIC ${NIFTI_NIFTILIB2_NAME}) + add_test( NAME ${NIFTI_PACKAGE_PREFIX}nifti2_from_ascii_test COMMAND $ ) + # Do all regression tests diff --git a/nifti2/nifti2_from_ascii_test.c b/nifti2/nifti2_from_ascii_test.c new file mode 100644 index 00000000..5450119d --- /dev/null +++ b/nifti2/nifti2_from_ascii_test.c @@ -0,0 +1,19 @@ +/* sscanf matches nothing here; run under memcheck to catch an uninitialized read */ +#include +#include + +int main(void) +{ + int bytes_read = 0; + int errors = 0; + nifti_image * nim = nifti_image_from_ascii(" \n\t ", &bytes_read); + + if( nim != NULL ) { + printf("ERROR: nifti_image_from_ascii accepted whitespace-only input\n"); + nifti_image_free(nim); + errors++; + } + + printf("TOTAL ERRORS=%d\n", errors); + return errors; +} diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index c8cc6b23..1b43350f 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -8752,8 +8752,10 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) /* scan for opening string */ spos = 0 ; - ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; spos += nn ; - if( ii == 0 || strcmp(lhs,"") == 0 ) break ; /* end of input? */ + ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; + if( ii != 1 ) break ; /* nothing scanned: lhs and nn are unset */ + spos += nn ; + if( strcmp(lhs,"/>") == 0 ) break ; /* end of input? */ /* skip whitespace and the '=' marker */ @@ -8800,8 +8804,9 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) memcpy(rhs,str+spos+1,nn) ; rhs[nn] = '\0' ; spos = (str[ii] == '\'') ? ii+1 : ii ; } else { - ii = sscanf( str+spos , "%1023s%n" , rhs , &nn ) ; spos += nn ; - if( ii == 0 ) break ; /* nothing found? */ + ii = sscanf( str+spos , "%1023s%n" , rhs , &nn ) ; + if( ii != 1 ) break ; /* nothing found: rhs and nn are unset */ + spos += nn ; } unescape_string(rhs) ; /* remove any XML escape sequences */ diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index eb8887f3..75870d81 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -6636,8 +6636,10 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) /* scan for opening string */ spos = 0 ; - ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; spos += nn ; - if( ii == 0 || strcmp(lhs,"") == 0 ) break ; /* end of input? */ + ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; + if( ii != 1 ) break ; /* nothing scanned: lhs and nn are unset */ + spos += nn ; + if( strcmp(lhs,"/>") == 0 ) break ; /* end of input? */ /* skip whitespace and the '=' marker */ @@ -6684,8 +6688,9 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) memcpy(rhs,str+spos+1,nn) ; rhs[nn] = '\0' ; spos = (str[ii] == '\'') ? ii+1 : ii ; } else { - ii = sscanf( str+spos , "%1023s%n" , rhs , &nn ) ; spos += nn ; - if( ii == 0 ) break ; /* nothing found? */ + ii = sscanf( str+spos , "%1023s%n" , rhs , &nn ) ; + if( ii != 1 ) break ; /* nothing found: rhs and nn are unset */ + spos += nn ; } unescape_string(rhs) ; /* remove any XML escape sequences */ diff --git a/niftilib/nifti_tester001.c b/niftilib/nifti_tester001.c index 3fbc0656..6aea6723 100644 --- a/niftilib/nifti_tester001.c +++ b/niftilib/nifti_tester001.c @@ -369,6 +369,14 @@ int main (int argc, const char *argv[]) nifti_image_free(reference_image); nifti_image_free(reloaded_image); } + { + /* sscanf matches nothing here; run under memcheck to catch an uninitialized read */ + int bytes_read = 0; + nifti_image * blank_image = nifti_image_from_ascii(" \n\t ", &bytes_read); + PrintTest("nifti_image_from_ascii whitespace only", + blank_image != NULL,false,&Errors); + nifti_image_free(blank_image); + } { enum { NUM_FILE_NAMES=8 };