From e4e639aab9cd7b9573dc36211a5e32522b6704a9 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 18 Sep 2026 22:11:47 -0400 Subject: [PATCH 1/3] BUG: Do not write to the stream that failed to open open_write_stream() returns NULL when its fopen() fails, and reports it. Both callers then used the result without testing it. $ cifti_tool -input c.nii -disp_cext -output /nonexistent-dir/x.txt ** failed to open '/nonexistent-dir/x.txt' for writing Segmentation fault (core dumped) disp_cifti_extension() passes it to fprintf(). eval_cifti_extension() hands it to axml_set_wstream(), so the NULL becomes the xml library's write stream and every later write follows it. Both now return 1; the message has already been printed. This file is behind USE_CIFTI_CODE, which defaults to OFF, so the standard workflows do not compile it. Verified with -DUSE_CIFTI_CODE=ON. (cherry picked from commit f368af9a57106c46ecd7c738cf07817e9a7ef170) --- cifti/cifti_tool.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cifti/cifti_tool.c b/cifti/cifti_tool.c index 3743921f..f5680e80 100644 --- a/cifti/cifti_tool.c +++ b/cifti/cifti_tool.c @@ -220,6 +220,7 @@ int disp_cifti_extension(nifti_image * nim, opts_t * opts) } fp = open_write_stream(opts->fout); + if( !fp ) return 1; if( !ext ) { fprintf(fp, "** no CIFTI extension in %s\n",nim->fname?nim->fname:"NULL"); close_stream(fp); @@ -243,6 +244,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; axml_set_wstream(fp); if( opts->verb > 1 ) fprintf(stderr, "-- recursive eval from %s\n", @@ -270,6 +272,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; From 39ca5eecf5ef23adacafc85bedabf22356e37e14 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 08:53:21 -0500 Subject: [PATCH 2/3] ENH: Cover the unopenable -output path in cifti_tool Driven through a cmake -P script rather than add_test, because PASS_REGULAR_EXPRESSION ignores the exit status and a crash that had already printed the diagnostic would pass. The script pins that the tool exits normally and reports the failure, and that an openable -output is still written with the extension in it, so refusing every write would not pass. (cherry picked from commit 5c08eb39ce357756f14e9d0ea46a813af325db24) --- cifti/CMakeLists.txt | 10 ++++++++ cifti/test_write_failure.cmake | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 cifti/test_write_failure.cmake diff --git a/cifti/CMakeLists.txt b/cifti/CMakeLists.txt index b233a58f..99fa1756 100644 --- a/cifti/CMakeLists.txt +++ b/cifti/CMakeLists.txt @@ -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=$ + -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() diff --git a/cifti/test_write_failure.cmake b/cifti/test_write_failure.cmake new file mode 100644 index 00000000..ea264050 --- /dev/null +++ b/cifti/test_write_failure.cmake @@ -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(0 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(0 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() From dcb70d63da40bc6482bf8998a75c0962da628ed2 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 3 Nov 2026 06:05:06 -0600 Subject: [PATCH 3/3] BUG: Exit non-zero when cifti_tool cannot display or evaluate process() discarded the return values of disp_cifti_extension() and eval_cifti_extension(), so the tool exited 0 after failing to open its -output file, finding no CIFTI extension, or having no image to display. A script chaining on the exit status went on with output that was never written. Those failures now exit 1, as a missing -input or an unreadable CIFTI file already did. Co-Authored-By: Sean McBride --- cifti/cifti_tool.c | 7 ++++--- cifti/test_write_failure.cmake | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cifti/cifti_tool.c b/cifti/cifti_tool.c index f5680e80..e2b0c9f2 100644 --- a/cifti/cifti_tool.c +++ b/cifti/cifti_tool.c @@ -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;} @@ -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) diff --git a/cifti/test_write_failure.cmake b/cifti/test_write_failure.cmake index ea264050..b476430a 100644 --- a/cifti/test_write_failure.cmake +++ b/cifti/test_write_failure.cmake @@ -18,12 +18,12 @@ function(run_tool expect_result out_var) endif() endfunction() -run_tool(0 bad_disp -input ${INPUT} -disp_cext -output ${BADOUT}) +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(0 bad_eval -input ${INPUT} -eval_cext -eval_type show_summary +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}")