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); diff --git a/cifti/testdata/cext_second_extension.nii b/cifti/testdata/cext_second_extension.nii new file mode 100644 index 00000000..f3eca967 Binary files /dev/null and b/cifti/testdata/cext_second_extension.nii differ