Skip to content
Draft
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
10 changes: 10 additions & 0 deletions cifti/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,14 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS)
-input ${CMAKE_CURRENT_LIST_DIR}/testdata/deep_nesting.xml )
set_tests_properties( ${TEST_PREFIX}_deep_nesting
PROPERTIES PASS_REGULAR_EXPRESSION "BrainModel" )

# A -output path that cannot be opened must be reported and refused, not
# written to anyway.
add_test( NAME ${TEST_PREFIX}_write_failure
COMMAND ${CMAKE_COMMAND}
-DTOOL=$<TARGET_FILE:${NIFTI_PACKAGE_PREFIX}cifti_tool>
-DINPUT=${CMAKE_CURRENT_LIST_DIR}/testdata/cext_unterminated.nii
-DBADOUT=${CMAKE_CURRENT_BINARY_DIR}/no_such_dir/out.txt
-DGOODOUT=${CMAKE_CURRENT_BINARY_DIR}/write_failure_ok.txt
-P ${CMAKE_CURRENT_LIST_DIR}/test_write_failure.cmake )
endif()
10 changes: 7 additions & 3 deletions cifti/cifti_tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ int process(opts_t * opts)
{
nifti_image * nim = NULL;
afni_xml_t * ax;
int rv = 0;

if( !opts->fin ){ fprintf(stderr, "** missing option '-input'\n"); return 1;}

Expand All @@ -190,13 +191,13 @@ int process(opts_t * opts)
return 1;
}

if( opts->disp_cext ) disp_cifti_extension(nim, opts);
if( opts->eval_cext ) eval_cifti_extension(ax, opts);
if( opts->disp_cext ) rv |= disp_cifti_extension(nim, opts);
if( opts->eval_cext ) rv |= eval_cifti_extension(ax, opts);

axml_free_xml_t(ax);
nifti_image_free(nim);

return 0;
return rv ? 1 : 0;
}

int disp_cifti_extension(nifti_image * nim, opts_t * opts)
Expand All @@ -220,6 +221,7 @@ int disp_cifti_extension(nifti_image * nim, opts_t * opts)
}

fp = open_write_stream(opts->fout);
if( !fp ) return 1;

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.

[Google Carbon] Move the if (!ext) check (lines 224–227) above fp = open_write_stream(opts->fout) and write that diagnostic to stderr instead of fp — otherwise -disp_cext -output existing.xml clobbers existing.xml with "** no CIFTI extension in ..." on failure!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That branch is unreachable from cifti_tool: process() returns at 'failed to read CIFTI ext' before disp_cifti_extension when there is no CIFTI extension, so -output is never opened. I confirmed it with an image that has no extension, so I left the order as is.

if( !ext ) {
fprintf(fp, "** no CIFTI extension in %s\n",nim->fname?nim->fname:"NULL");
close_stream(fp);
Expand All @@ -243,6 +245,7 @@ int eval_cifti_extension(afni_xml_t * ax, opts_t * opts)
opts->fout ? opts->fout : "DEFAULT" );

fp = open_write_stream(opts->fout);
if( !fp ) return 1;
Comment thread
hjmjohnson marked this conversation as resolved.
Resolved
axml_set_wstream(fp);

if( opts->verb > 1 ) fprintf(stderr, "-- recursive eval from %s\n",
Expand Down Expand Up @@ -270,6 +273,7 @@ int eval_cifti_extension(afni_xml_t * ax, opts_t * opts)
axml_recur(ax_show_names, ax);

/* possibly close file */
axml_set_wstream(NULL);
close_stream(fp);

return 0;
Expand Down
42 changes: 42 additions & 0 deletions cifti/test_write_failure.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Drives cifti_tool at an unopenable -output path. A plain add_test cannot
# express this: PASS_REGULAR_EXPRESSION ignores the exit status, so a crash
# that printed the diagnostic first would still pass.

if(NOT TOOL OR NOT INPUT OR NOT BADOUT OR NOT GOODOUT)
message(FATAL_ERROR "TOOL, INPUT, BADOUT and GOODOUT are all required")
endif()

function(run_tool expect_result out_var)
execute_process(COMMAND ${TOOL} ${ARGN}
RESULT_VARIABLE result
OUTPUT_VARIABLE out
ERROR_VARIABLE err)
set(${out_var} "${out}${err}" PARENT_SCOPE)
if(NOT result STREQUAL "${expect_result}")
message(FATAL_ERROR
"expected result ${expect_result}, got '${result}'\n${out}${err}")
endif()
endfunction()

run_tool(1 bad_disp -input ${INPUT} -disp_cext -output ${BADOUT})
if(NOT bad_disp MATCHES "failed to open")
message(FATAL_ERROR "missing the open-failure diagnostic:\n${bad_disp}")
endif()

run_tool(1 bad_eval -input ${INPUT} -eval_cext -eval_type show_summary
-output ${BADOUT})
if(NOT bad_eval MATCHES "failed to open")
message(FATAL_ERROR "missing the open-failure diagnostic:\n${bad_eval}")
endif()

# An openable -output must still be written, so that refusing every write
# would not pass.
file(REMOVE ${GOODOUT})
run_tool(0 good_disp -input ${INPUT} -disp_cext -output ${GOODOUT})
if(NOT EXISTS ${GOODOUT})
message(FATAL_ERROR "nothing written to ${GOODOUT}")
endif()
file(READ ${GOODOUT} written)
if(NOT written MATCHES "MapName")
message(FATAL_ERROR "extension not displayed:\n${written}")
endif()