Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions nifti2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<TARGET_FILE:${NIFTI_PACKAGE_PREFIX}nifti2_from_ascii_test> )



# Do all regression tests
Expand Down
19 changes: 19 additions & 0 deletions nifti2/nifti2_from_ascii_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* sscanf matches nothing here; run under memcheck to catch an uninitialized read */
#include <stdio.h>
#include <nifti2_io.h>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ought to be "" not <>. But seems the codebase is inconsistent about this. I'll make a followup PR.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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;
}
17 changes: 11 additions & 6 deletions nifti2/nifti2_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,"<nifti_image") != 0 ) return NULL ;
ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ;
if( ii != 1 ) return NULL ; /* nothing scanned: lhs and nn are unset */
Comment thread
hjmjohnson marked this conversation as resolved.
Resolved
spos += nn ;
if( strcmp(lhs,"<nifti_image") != 0 ) return NULL ;

/* create empty image struct */

Expand Down Expand Up @@ -8782,8 +8784,10 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read )

/* get lhs string */

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 */

Expand All @@ -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 */

Expand Down
17 changes: 11 additions & 6 deletions niftilib/nifti1_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,"<nifti_image") != 0 ) return NULL ;
ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ;
if( ii != 1 ) return NULL ; /* nothing scanned: lhs and nn are unset */
spos += nn ;
if( strcmp(lhs,"<nifti_image") != 0 ) return NULL ;

/* create empty image struct */

Expand Down Expand Up @@ -6666,8 +6668,10 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read )

/* get lhs string */

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 */

Expand All @@ -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 */

Expand Down
8 changes: 8 additions & 0 deletions niftilib/nifti_tester001.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
Loading