Skip to content
Open
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
20 changes: 20 additions & 0 deletions cifti/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS)
PROPERTIES PASS_REGULAR_EXPRESSION "<MapName>demo</MapName>"
FAIL_REGULAR_EXPRESSION "no CIFTI extension" )

# A read failure must be reported, not taken for the end of the document.
# The input is a directory, which opens on POSIX and fails at the first
# read; Windows refuses the open instead, so there is no read to fail.
if( NOT WIN32 )
add_test( NAME ${TEST_PREFIX}_xml_read_error
COMMAND $<TARGET_FILE:${NIFTI_PACKAGE_PREFIX}afni_xml_tool>
-input ${CMAKE_CURRENT_LIST_DIR}/testdata )
set_tests_properties( ${TEST_PREFIX}_xml_read_error
PROPERTIES PASS_REGULAR_EXPRESSION "failed to read XML file" )
endif()

# A readable document must still parse, and must not report a read error.
add_test( NAME ${TEST_PREFIX}_xml_read_ok
COMMAND $<TARGET_FILE:${NIFTI_PACKAGE_PREFIX}afni_xml_tool>
-input ${CMAKE_CURRENT_LIST_DIR}/testdata/mim_known_child.xml
-verb 2 )
set_tests_properties( ${TEST_PREFIX}_xml_read_ok
PROPERTIES PASS_REGULAR_EXPRESSION "xlist read"
FAIL_REGULAR_EXPRESSION "failed to read XML file" )

# An extension payload that fills esize-8 with no NUL must not be read
# past its end; the regex pins that the payload is still parsed.
add_test( NAME ${TEST_PREFIX}_tool_unterminated_cext
Expand Down
8 changes: 7 additions & 1 deletion cifti/afni_xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ afni_xml_list axml_read_file(const char * fname, int read_data)
if( reset_xml_buf(xd, &buf, &bsize) ) break;

blen = (unsigned)fread(buf, 1, (size_t)bsize, fp);
if( ferror(fp) ) {
fprintf(stderr,"** failed to read XML file '%s'\n", fname);
break;
}

/* check for early termination */
bshort = loc_strnlen(buf, blen);
Expand All @@ -207,7 +211,9 @@ afni_xml_list axml_read_file(const char * fname, int read_data)
blen = (unsigned)bshort;
}

done = blen < (unsigned) bsize;
/* feof() also stops a file ending exactly on a buffer boundary,
sparing a final read that can only return zero */
done = blen < (unsigned) bsize || feof(fp);

if(xd->verb > 4) fprintf(stderr,"-- XML_Parse # %d\n", pcount);
pcount++;
Expand Down