From e3ebc01154f8e26e2302a7da4edd19ea70aa78df Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Sat, 15 Aug 2026 01:18:16 -0400 Subject: [PATCH 1/2] BUG: Fix cifti_tool's CIFTI extension search, which never advanced disp_cifti_extension() searched for the CIFTI extension with ext = nim->ext_list; for( ind = 0; ind < nim->num_ext; ind++ ) if( ext->ecode == NIFTI_ECODE_CIFTI ) break; ext is never advanced, so this tests the first extension num_ext times. cifti_tool could only ever find a CIFTI extension that happened to be first in the list; with any other extension ahead of it the tool reported 'no CIFTI extension' for a file that has one. It now indexes ext_list[ind] and leaves ext NULL when there is no match, which also avoids the read past the end of the list that a bare ext++ would have introduced. The same function opened its output stream before the 'no CIFTI extension' check and returned without closing it; the early return now closes the stream like the normal path does. --- cifti/CMakeLists.txt | 9 +++++++++ cifti/cifti_tool.c | 15 +++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/cifti/CMakeLists.txt b/cifti/CMakeLists.txt index ec8a4307..db297bbf 100644 --- a/cifti/CMakeLists.txt +++ b/cifti/CMakeLists.txt @@ -42,6 +42,15 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) set_tests_properties( ${TEST_PREFIX}_mim_summary_known_child PROPERTIES PASS_REGULAR_EXPRESSION "BrainModel" ) + # The CIFTI extension is found even when another extension precedes it. + add_test( NAME ${TEST_PREFIX}_tool_cext_not_first + COMMAND $ + -input ${CMAKE_CURRENT_LIST_DIR}/testdata/cext_second_extension.nii + -disp_cext ) + set_tests_properties( ${TEST_PREFIX}_tool_cext_not_first + PROPERTIES PASS_REGULAR_EXPRESSION "demo" + FAIL_REGULAR_EXPRESSION "no CIFTI extension" ) + # 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 diff --git a/cifti/cifti_tool.c b/cifti/cifti_tool.c index b895c09f..18f023b9 100644 --- a/cifti/cifti_tool.c +++ b/cifti/cifti_tool.c @@ -210,19 +210,22 @@ int disp_cifti_extension(nifti_image * nim, opts_t * opts) opts->fout ? opts->fout : "DEFAULT" ); if( !nim ) return 1; - ext = nim->ext_list; + /* find the CIFTI extension, if any; it need not be first in the list */ + ext = NULL; for( ind = 0; ind < nim->num_ext; ind++ ) - if( ext->ecode == NIFTI_ECODE_CIFTI ) break; + if( nim->ext_list[ind].ecode == NIFTI_ECODE_CIFTI ) { + ext = nim->ext_list + ind; + break; + } fp = open_write_stream(opts->fout); - if( ext && ext->ecode != NIFTI_ECODE_CIFTI ) { + if( !ext ) { fprintf(fp, "** no CIFTI extension in %s\n",nim->fname?nim->fname:"NULL"); + close_stream(fp); return 1; } - if(ext) { - fprintf(fp, "%.*s\n", ext->esize-8, ext->edata); - } + fprintf(fp, "%.*s\n", ext->esize-8, ext->edata); /* possibly close file */ close_stream(fp); From d74f89b81d3ac9d0295b56655d1eebf8bbb30352 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 08:45:47 -0500 Subject: [PATCH 2/2] ENH: Cover a CIFTI extension that is not first in the list cext_second_extension.nii carries a comment extension ahead of the CIFTI one. The test requires the extension's payload in the output and forbids the 'no CIFTI extension' message, and the existing unterminated-cext test still pins the first-position case, so neither a search that always matches nor one that never does would pass. --- cifti/testdata/cext_second_extension.nii | Bin 0 -> 752 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 cifti/testdata/cext_second_extension.nii diff --git a/cifti/testdata/cext_second_extension.nii b/cifti/testdata/cext_second_extension.nii new file mode 100644 index 0000000000000000000000000000000000000000..f3eca967ff8bbdc709e43643e83d04eadc9fd8be GIT binary patch literal 752 zcmdr|J!``-5LMdJAya;W(Ap0toifNaCQiW6gpjDEg@O^*Q3SChVo{u}KfRKLFbdYF?m z8Pkr6UwhmWN$8r}qip-TP(oJFthvFNF$W0^(*co7H%n;mnh!Xpb8b018Hh>OzAQHR z@~vd=+hw_5WY5`d?_@T-2I1OzA>)%oK&@%iXEYiRuIOhPG!l;sV@91uGRFU6VXMRW E1N7=}=>Px# literal 0 HcmV?d00001