From f529667a1ea0749af9cf12ea69c59396b456b8dd Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 07:00:34 -0500 Subject: [PATCH 01/11] BUG: Check the header converted to nifti_image before reading its datatype The REJECT_COMPLEX block reads nim->datatype, and the test for nim being NULL sits immediately below it, so a header the converter refuses crashes the process in builds that define REJECT_COMPLEX. The refusal itself is correct and already reported; only the order is wrong. Found by the malformed-header tests added in this branch: the guards make the converter return NULL for exactly the inputs they cover, which is the case this block never handled. --- nifti2/nifti2_io.c | 14 +++++++------- niftilib/nifti1_io.c | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index 3c782bc0..8e749950 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -6028,13 +6028,6 @@ nifti_image *nifti_image_read( const char *hname , int read_data ) znzclose(fp); free(hfile); return NULL; } - #ifdef REJECT_COMPLEX - if ((nim->datatype == DT_COMPLEX64) || (nim->datatype == DT_COMPLEX128) || (nim->datatype == DT_COMPLEX256)) { - fprintf(stderr,"Image Exception Unsupported datatype (COMPLEX64): use fslcomplex to manipulate: %s\n", hname); - exit(13); - } - #endif - if( nim == NULL ){ znzclose( fp ) ; /* close the file */ if( g_opts.debug > 0 ) @@ -6043,6 +6036,13 @@ nifti_image *nifti_image_read( const char *hname , int read_data ) return NULL; } + #ifdef REJECT_COMPLEX + if ((nim->datatype == DT_COMPLEX64) || (nim->datatype == DT_COMPLEX128) || (nim->datatype == DT_COMPLEX256)) { + fprintf(stderr,"Image Exception Unsupported datatype (COMPLEX64): use fslcomplex to manipulate: %s\n", hname); + exit(13); + } + #endif + if( g_opts.debug > 3 ){ fprintf(stderr,"+d nifti_image_read(), have nifti image:\n"); nifti_image_infodump(nim); diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index 1f447cbd..60a13198 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -4315,13 +4315,6 @@ nifti_image *nifti_image_read( const char *hname , int read_data ) /**- convert all nhdr fields to nifti_image fields */ nim = nifti_convert_nhdr2nim(nhdr,hfile); - #ifdef REJECT_COMPLEX - if ((nim->datatype == DT_COMPLEX64) || (nim->datatype == DT_COMPLEX128) || (nim->datatype == DT_COMPLEX256)) { - fprintf(stderr,"Image Exception Unsupported datatype (COMPLEX64): use fslcomplex to manipulate: %s\n", hname); - exit(13); - } - #endif - if( nim == NULL ){ znzclose( fp ) ; /* close the file */ if( g_opts.debug > 0 ) @@ -4330,6 +4323,13 @@ nifti_image *nifti_image_read( const char *hname , int read_data ) return NULL; } + #ifdef REJECT_COMPLEX + if ((nim->datatype == DT_COMPLEX64) || (nim->datatype == DT_COMPLEX128) || (nim->datatype == DT_COMPLEX256)) { + fprintf(stderr,"Image Exception Unsupported datatype (COMPLEX64): use fslcomplex to manipulate: %s\n", hname); + exit(13); + } + #endif + if( g_opts.debug > 3 ){ fprintf(stderr,"+d nifti_image_read(), have nifti image:\n"); nifti_image_infodump(nim); From 547aaa542614e5e6fc7f0c859b8f2c676750c907 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 06:52:20 -0500 Subject: [PATCH 02/11] ENH: Cover the NIFTI-2 dim[0] range check A dim[0] outside 1..7 is used to index dim[] in nifti_convert_n2hdr2nim, so a header carrying a large negative value reads far outside the struct. The fixture is a 604-byte NIFTI-2 header holding such a value; without the check the tool segfaults in an ordinary Release build, so this test guards on every CI job rather than only the sanitizer one. --- nifti2/CMakeLists.txt | 4 ++++ nifti2/testdata/n2_bad_dim0.nii | Bin 0 -> 604 bytes 2 files changed, 4 insertions(+) create mode 100644 nifti2/testdata/n2_bad_dim0.nii diff --git a/nifti2/CMakeLists.txt b/nifti2/CMakeLists.txt index 758df0b9..f83ebdbc 100644 --- a/nifti2/CMakeLists.txt +++ b/nifti2/CMakeLists.txt @@ -189,6 +189,10 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) endif() endif() + # A dim[0] outside 1..7 must be refused rather than used to index dim[]. + add_test( NAME ${TEST_PREFIX}_tool_disp_nim_bad_dim0 COMMAND $ -disp_nim -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/n2_bad_dim0.nii ) + set_tests_properties( ${TEST_PREFIX}_tool_disp_nim_bad_dim0 PROPERTIES PASS_REGULAR_EXPRESSION "bad dim\\[0\\]" ) + unset(TEST_SUFFIX) unset(TEST_PREFIX) unset(TOOL_NAME) diff --git a/nifti2/testdata/n2_bad_dim0.nii b/nifti2/testdata/n2_bad_dim0.nii new file mode 100644 index 0000000000000000000000000000000000000000..39c702455df7f615fb8b07f4416a8da245c2e79a GIT binary patch literal 604 zcmb1PVqnPAHe%rAlHy`v5MWrc2m*k@V1Q0DLd9{>P-z_02YZMF>9hjOAvjDP6&`#c F008?k5AOf~ literal 0 HcmV?d00001 From 40853eaf4b05df3d6d533756b43febde4c56b187 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 06:52:30 -0500 Subject: [PATCH 03/11] ENH: Cover the swap width of a byte-swapped nifti_1_header nifti_read_n1_hdr reads 348 bytes, so swapping the buffer as whatever width its magic names reaches past the end when the magic says n+2. The fixture is a big-endian NIFTI-1 header wearing an n+2 magic, which the sanitizer legs report as a stack-buffer-overflow without the fix. -check_hdr is the seam that reaches it; -disp_hdr does not. --- nifti2/CMakeLists.txt | 4 ++++ nifti2/testdata/n1_magic_n2.nii | Bin 0 -> 368 bytes 2 files changed, 4 insertions(+) create mode 100644 nifti2/testdata/n1_magic_n2.nii diff --git a/nifti2/CMakeLists.txt b/nifti2/CMakeLists.txt index f83ebdbc..1dbf86da 100644 --- a/nifti2/CMakeLists.txt +++ b/nifti2/CMakeLists.txt @@ -193,6 +193,10 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) add_test( NAME ${TEST_PREFIX}_tool_disp_nim_bad_dim0 COMMAND $ -disp_nim -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/n2_bad_dim0.nii ) set_tests_properties( ${TEST_PREFIX}_tool_disp_nim_bad_dim0 PROPERTIES PASS_REGULAR_EXPRESSION "bad dim\\[0\\]" ) + # A nifti_1_header is swapped at NIFTI-1 width whatever its magic claims. + add_test( NAME ${TEST_PREFIX}_tool_check_hdr_magic_n2 COMMAND $ -check_hdr -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/n1_magic_n2.nii ) + set_tests_properties( ${TEST_PREFIX}_tool_check_hdr_magic_n2 PROPERTIES PASS_REGULAR_EXPRESSION "header IS GOOD" ) + unset(TEST_SUFFIX) unset(TEST_PREFIX) unset(TOOL_NAME) diff --git a/nifti2/testdata/n1_magic_n2.nii b/nifti2/testdata/n1_magic_n2.nii new file mode 100644 index 0000000000000000000000000000000000000000..725219c30f03661a3e507da0baea9df9fa3a9bec GIT binary patch literal 368 wcmZQzV2oiP2#OdOm>HPBkdXlbQT4De2rw|%H{i$48&EY3XI`GR5mxU30KmowrvLx| literal 0 HcmV?d00001 From 88a9141a06cf4ee0e908d8ddcd403b88917efafe Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 06:52:43 -0500 Subject: [PATCH 04/11] ENH: Cover the swap width of the tool's own header copies act_mod_hdrs() and act_swap_hdrs() hold a 348-byte nifti_1_header and swapped it at the width its magic named, reading past the end for an n+2 magic. Both reuse the fixture added for the library-side check, and both rewrite their input, so each test runs against its own copy. --- nifti2/CMakeLists.txt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/nifti2/CMakeLists.txt b/nifti2/CMakeLists.txt index 1dbf86da..d321c443 100644 --- a/nifti2/CMakeLists.txt +++ b/nifti2/CMakeLists.txt @@ -197,6 +197,15 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) add_test( NAME ${TEST_PREFIX}_tool_check_hdr_magic_n2 COMMAND $ -check_hdr -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/n1_magic_n2.nii ) set_tests_properties( ${TEST_PREFIX}_tool_check_hdr_magic_n2 PROPERTIES PASS_REGULAR_EXPRESSION "header IS GOOD" ) + # The tool's own header copies are swapped at their own width too. Both + # invocations rewrite the file, so each works on a copy. + add_test( NAME ${TEST_PREFIX}_tool_mod_hdr_magic_n2_setup COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_LIST_DIR}/testdata/n1_magic_n2.nii ${CMAKE_CURRENT_BINARY_DIR}/mod_magic_n2.nii ) + add_test( NAME ${TEST_PREFIX}_tool_mod_hdr_magic_n2 COMMAND $ -mod_hdr -mod_field descrip hello -overwrite -infiles ${CMAKE_CURRENT_BINARY_DIR}/mod_magic_n2.nii ) + set_tests_properties( ${TEST_PREFIX}_tool_mod_hdr_magic_n2 PROPERTIES DEPENDS ${TEST_PREFIX}_tool_mod_hdr_magic_n2_setup ) + add_test( NAME ${TEST_PREFIX}_tool_swap_as_old_magic_n2_setup COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_LIST_DIR}/testdata/n1_magic_n2.nii ${CMAKE_CURRENT_BINARY_DIR}/swap_magic_n2.nii ) + add_test( NAME ${TEST_PREFIX}_tool_swap_as_old_magic_n2 COMMAND $ -swap_as_old -overwrite -infiles ${CMAKE_CURRENT_BINARY_DIR}/swap_magic_n2.nii ) + set_tests_properties( ${TEST_PREFIX}_tool_swap_as_old_magic_n2 PROPERTIES DEPENDS ${TEST_PREFIX}_tool_swap_as_old_magic_n2_setup ) + unset(TEST_SUFFIX) unset(TEST_PREFIX) unset(TOOL_NAME) From 6cad3d3aa668aaea956b4d8b70e18ba756163dd6 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 06:52:55 -0500 Subject: [PATCH 05/11] ENH: Cover the voxel-count and volume-size overflow guards Three fixtures, because the two guards are reached separately: a product that wraps nvox, the NIFTI-2 form of the same, and one whose nvox fits while nvox * nbyper does not. Without the guards the tool accepts all three and reports a negative nvox. The regex pins which refusal fired; an exit-code test would not tell this rejection apart from any other error. --- nifti2/CMakeLists.txt | 9 +++++++++ nifti2/testdata/n1_overflow.nii | Bin 0 -> 352 bytes nifti2/testdata/n1_volsize.nii | Bin 0 -> 352 bytes nifti2/testdata/n2_overflow.nii | Bin 0 -> 544 bytes 4 files changed, 9 insertions(+) create mode 100644 nifti2/testdata/n1_overflow.nii create mode 100644 nifti2/testdata/n1_volsize.nii create mode 100644 nifti2/testdata/n2_overflow.nii diff --git a/nifti2/CMakeLists.txt b/nifti2/CMakeLists.txt index d321c443..11f6b81f 100644 --- a/nifti2/CMakeLists.txt +++ b/nifti2/CMakeLists.txt @@ -206,6 +206,15 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) add_test( NAME ${TEST_PREFIX}_tool_swap_as_old_magic_n2 COMMAND $ -swap_as_old -overwrite -infiles ${CMAKE_CURRENT_BINARY_DIR}/swap_magic_n2.nii ) set_tests_properties( ${TEST_PREFIX}_tool_swap_as_old_magic_n2 PROPERTIES DEPENDS ${TEST_PREFIX}_tool_swap_as_old_magic_n2_setup ) + # Dimensions whose product overflows the voxel count are refused. + add_test( NAME ${TEST_PREFIX}_tool_reject_nvox_overflow_n1 COMMAND $ -disp_nim -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/n1_overflow.nii ) + add_test( NAME ${TEST_PREFIX}_tool_reject_nvox_overflow_n2 COMMAND $ -disp_nim -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/n2_overflow.nii ) + add_test( NAME ${TEST_PREFIX}_tool_reject_volsize_overflow COMMAND $ -disp_nim -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/n1_volsize.nii ) + set_tests_properties( ${TEST_PREFIX}_tool_reject_nvox_overflow_n1 ${TEST_PREFIX}_tool_reject_nvox_overflow_n2 + PROPERTIES PASS_REGULAR_EXPRESSION "dim\\[\\] overflows the voxel count" ) + set_tests_properties( ${TEST_PREFIX}_tool_reject_volsize_overflow + PROPERTIES PASS_REGULAR_EXPRESSION "dim\\[\\] and datatype overflow the volume size" ) + unset(TEST_SUFFIX) unset(TEST_PREFIX) unset(TOOL_NAME) diff --git a/nifti2/testdata/n1_overflow.nii b/nifti2/testdata/n1_overflow.nii new file mode 100644 index 0000000000000000000000000000000000000000..a5d8c10b71490bf1927a3fb7b7c79bdf0bea03f4 GIT binary patch literal 352 scma!HWFQK#GyJbdhNzlY7zDrq4fYHS`0xg2h&-z9;mFI=HUxVM0IxF>UjP6A literal 0 HcmV?d00001 diff --git a/nifti2/testdata/n1_volsize.nii b/nifti2/testdata/n1_volsize.nii new file mode 100644 index 0000000000000000000000000000000000000000..98160d480f3cdac137dba372785ea7f484afdc62 GIT binary patch literal 352 vcma!HWFQK#G5oKGKo$l}Ee;G0U{f0G85r>44bBjGqO2U!it@A#!R`kD?6D73 literal 0 HcmV?d00001 diff --git a/nifti2/testdata/n2_overflow.nii b/nifti2/testdata/n2_overflow.nii new file mode 100644 index 0000000000000000000000000000000000000000..6402081f1a612e748f94b902d166225f601feaab GIT binary patch literal 544 zcmb1PVqnPAHe%rAlHy`v5MTgukpK$=g98$mV&(^Xh!LdI3MdYP>8IF1qXrNN0RaD@ B3`hU~ literal 0 HcmV?d00001 From 72f1b90d65c7d9ef83ad34b9bd6abde803cd0910 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 06:53:10 -0500 Subject: [PATCH 06/11] ENH: Cover the voxel-count guards in the NIFTI-1 library nifti_convert_nhdr2nim carries its own copy of the guards, so it needs its own tests; reverting the NIFTI-1 half alone leaves the nifti2 tests green and turns exactly these two red. --- niftilib/CMakeLists.txt | 8 ++++++++ niftilib/testdata/n1_overflow.nii | Bin 0 -> 352 bytes niftilib/testdata/n1_volsize.nii | Bin 0 -> 352 bytes 3 files changed, 8 insertions(+) create mode 100644 niftilib/testdata/n1_overflow.nii create mode 100644 niftilib/testdata/n1_volsize.nii diff --git a/niftilib/CMakeLists.txt b/niftilib/CMakeLists.txt index 09d522d2..03bf551f 100644 --- a/niftilib/CMakeLists.txt +++ b/niftilib/CMakeLists.txt @@ -148,6 +148,14 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) ) #==END NIFTI1 and NIFTI2 common tests ============================================ + # The same guards in the NIFTI-1 library, reached through nifti1_tool. + add_test( NAME ${TEST_PREFIX}_tool_reject_nvox_overflow COMMAND $ -disp_nim -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/n1_overflow.nii ) + add_test( NAME ${TEST_PREFIX}_tool_reject_volsize_overflow COMMAND $ -disp_nim -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/n1_volsize.nii ) + set_tests_properties( ${TEST_PREFIX}_tool_reject_nvox_overflow + PROPERTIES PASS_REGULAR_EXPRESSION "dim\\[\\] overflows the voxel count" ) + set_tests_properties( ${TEST_PREFIX}_tool_reject_volsize_overflow + PROPERTIES PASS_REGULAR_EXPRESSION "dim\\[\\] and datatype overflow the volume size" ) + unset(TEST_SUFFIX) unset(TEST_PREFIX) unset(TOOL_NAME) diff --git a/niftilib/testdata/n1_overflow.nii b/niftilib/testdata/n1_overflow.nii new file mode 100644 index 0000000000000000000000000000000000000000..a5d8c10b71490bf1927a3fb7b7c79bdf0bea03f4 GIT binary patch literal 352 scma!HWFQK#GyJbdhNzlY7zDrq4fYHS`0xg2h&-z9;mFI=HUxVM0IxF>UjP6A literal 0 HcmV?d00001 diff --git a/niftilib/testdata/n1_volsize.nii b/niftilib/testdata/n1_volsize.nii new file mode 100644 index 0000000000000000000000000000000000000000..98160d480f3cdac137dba372785ea7f484afdc62 GIT binary patch literal 352 vcma!HWFQK#G5oKGKo$l}Ee;G0U{f0G85r>44bBjGqO2U!it@A#!R`kD?6D73 literal 0 HcmV?d00001 From ad2936a4b84c13f1d112a8c6f1bac7320ea9315a Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 06:53:21 -0500 Subject: [PATCH 07/11] ENH: Cover the repeated-attribute path in the ASCII header reader nifti_image_from_ascii overwrote fname and iname without releasing what they already held, so an ASCII header naming either attribute twice leaked the earlier copy. One fixture drives both libraries. The regex asserts the last value survives, so a fix that ignored the repeat instead of freeing the previous value could not pass. --- nifti2/CMakeLists.txt | 5 +++++ nifti2/testdata/dup_attr.nia | Bin 0 -> 548 bytes niftilib/CMakeLists.txt | 4 ++++ niftilib/testdata/dup_attr.nia | Bin 0 -> 548 bytes 4 files changed, 9 insertions(+) create mode 100644 nifti2/testdata/dup_attr.nia create mode 100644 niftilib/testdata/dup_attr.nia diff --git a/nifti2/CMakeLists.txt b/nifti2/CMakeLists.txt index 11f6b81f..b8633d2c 100644 --- a/nifti2/CMakeLists.txt +++ b/nifti2/CMakeLists.txt @@ -215,6 +215,11 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) set_tests_properties( ${TEST_PREFIX}_tool_reject_volsize_overflow PROPERTIES PASS_REGULAR_EXPRESSION "dim\\[\\] and datatype overflow the volume size" ) + # A repeated ASCII attribute releases the previous value, and the last + # one wins; the regex pins the surviving behavior. + add_test( NAME ${TEST_PREFIX}_tool_ascii_dup_attr COMMAND $ -disp_nim -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/dup_attr.nia ) + set_tests_properties( ${TEST_PREFIX}_tool_ascii_dup_attr PROPERTIES PASS_REGULAR_EXPRESSION "dup2.nia" ) + unset(TEST_SUFFIX) unset(TEST_PREFIX) unset(TOOL_NAME) diff --git a/nifti2/testdata/dup_attr.nia b/nifti2/testdata/dup_attr.nia new file mode 100644 index 0000000000000000000000000000000000000000..ad9932a30bf5a9f3eefddd26756abc992756b252 GIT binary patch literal 548 zcmdT=%L>9U5cKR<>jx#(7_kmTeoHAY!A#Yq$1u~s#Nu37nVQIj|>N5Q8v}*LN z7DC*)ar>;6b2m;isT^GZX&j6IwjcO>Lk0z*D_~IIAryE>4ec+6KCDZS{?Ic{vNcS2 u6wu9++`g%DbD&M$8eUZPM=oxwH~EVgb@2lA_IzXj literal 0 HcmV?d00001 diff --git a/niftilib/CMakeLists.txt b/niftilib/CMakeLists.txt index 03bf551f..c66872b3 100644 --- a/niftilib/CMakeLists.txt +++ b/niftilib/CMakeLists.txt @@ -156,6 +156,10 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) set_tests_properties( ${TEST_PREFIX}_tool_reject_volsize_overflow PROPERTIES PASS_REGULAR_EXPRESSION "dim\\[\\] and datatype overflow the volume size" ) + # The NIFTI-1 library carries the same ASCII reader. + add_test( NAME ${TEST_PREFIX}_tool_ascii_dup_attr COMMAND $ -disp_nim -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/dup_attr.nia ) + set_tests_properties( ${TEST_PREFIX}_tool_ascii_dup_attr PROPERTIES PASS_REGULAR_EXPRESSION "dup2.nia" ) + unset(TEST_SUFFIX) unset(TEST_PREFIX) unset(TOOL_NAME) diff --git a/niftilib/testdata/dup_attr.nia b/niftilib/testdata/dup_attr.nia new file mode 100644 index 0000000000000000000000000000000000000000..ad9932a30bf5a9f3eefddd26756abc992756b252 GIT binary patch literal 548 zcmdT=%L>9U5cKR<>jx#(7_kmTeoHAY!A#Yq$1u~s#Nu37nVQIj|>N5Q8v}*LN z7DC*)ar>;6b2m;isT^GZX&j6IwjcO>Lk0z*D_~IIAryE>4ec+6KCDZS{?Ic{vNcS2 u6wu9++`g%DbD&M$8eUZPM=oxwH~EVgb@2lA_IzXj literal 0 HcmV?d00001 From a7ab7592d2efc3489c4ec243ae1a1850a616d4b1 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 06:53:32 -0500 Subject: [PATCH 08/11] ENH: Cover the loc_strnlen bound on an unterminated extension loc_strnlen read one byte past the buffer it was given. The reachable caller is axml_read_buf, where the buffer is an extension's edata, allocated esize-8 and filled exactly; axml_read_file allocates one spare byte and absorbs the overread, so it is not the seam. The fixture is a NIFTI-1 image carrying a CIFTI extension whose payload is space-padded to hold no NUL. The overread is reported by the sanitizer legs; the build stays green without them. --- cifti/CMakeLists.txt | 9 +++++++++ cifti/testdata/cext_unterminated.nii | Bin 0 -> 704 bytes 2 files changed, 9 insertions(+) create mode 100644 cifti/testdata/cext_unterminated.nii diff --git a/cifti/CMakeLists.txt b/cifti/CMakeLists.txt index 9e539c20..ec8a4307 100644 --- a/cifti/CMakeLists.txt +++ b/cifti/CMakeLists.txt @@ -41,4 +41,13 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) -input ${CMAKE_CURRENT_LIST_DIR}/testdata/mim_known_child.xml ) set_tests_properties( ${TEST_PREFIX}_mim_summary_known_child PROPERTIES PASS_REGULAR_EXPRESSION "BrainModel" ) + + # 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 + COMMAND $ + -input ${CMAKE_CURRENT_LIST_DIR}/testdata/cext_unterminated.nii + -eval_cext -eval_type show_summary ) + set_tests_properties( ${TEST_PREFIX}_tool_unterminated_cext + PROPERTIES PASS_REGULAR_EXPRESSION "MapName : demo" ) endif() diff --git a/cifti/testdata/cext_unterminated.nii b/cifti/testdata/cext_unterminated.nii new file mode 100644 index 0000000000000000000000000000000000000000..43eb9b73af459e4a657c0bb30ff6c378629f3df5 GIT binary patch literal 704 zcma!HWFQJKGcbW6BLf7YYGPp!01GtOGce%8IxY}-RNcdom#1wA3@HYN03e|Nq;2dg za&r{QQj3Z+^Yd(#4D}3@6jJk&^HVbO(ruMOL)>&Nl}-4! zONufpV6jQ{;Rtmh`7Vhii6NB*skTaB z=f!6pX=BB0qjRjd$0OHuCq~_+^=)-t6`Un+3L6Dme WcG!W`L7b@%w!@A~fhGVwtpEVg#bj3i literal 0 HcmV?d00001 From ff3426efccdda38b1f55b6d3a93dcbb6069bc7e6 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 06:54:01 -0500 Subject: [PATCH 09/11] ENH: Cover the ambiguous-filename path in nifti_findhdrname The tool writes both members of the pair itself, so no data is needed. The driver asserts exit 1 and the diagnostic, because a library reports the clash to its caller rather than ending the process, and a plain add_test cannot tell that exit from the abort it replaced. It then removes the .gz and repeats, so an unconditional NULL return could not pass. Guarded on FSLSTYLE_NAME_CONFLICTS, which is off by default, so the test is registered only where the code is compiled. --- nifti2/CMakeLists.txt | 8 ++++++++ nifti2/ambiguous_hdrname.cmake | 30 ++++++++++++++++++++++++++++++ niftilib/CMakeLists.txt | 7 +++++++ niftilib/ambiguous_hdrname.cmake | 30 ++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 nifti2/ambiguous_hdrname.cmake create mode 100644 niftilib/ambiguous_hdrname.cmake diff --git a/nifti2/CMakeLists.txt b/nifti2/CMakeLists.txt index b8633d2c..c4f40b0b 100644 --- a/nifti2/CMakeLists.txt +++ b/nifti2/CMakeLists.txt @@ -220,6 +220,14 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) add_test( NAME ${TEST_PREFIX}_tool_ascii_dup_attr COMMAND $ -disp_nim -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/dup_attr.nia ) set_tests_properties( ${TEST_PREFIX}_tool_ascii_dup_attr PROPERTIES PASS_REGULAR_EXPRESSION "dup2.nia" ) + # Only compiled when the FSL name-conflict path is built. + if(FSLSTYLE_NAME_CONFLICTS) + add_test( NAME ${TEST_PREFIX}_ambiguous_hdrname + COMMAND ${CMAKE_COMMAND} -DTOOL=$ + -DDIR=${CMAKE_CURRENT_BINARY_DIR}/ambig + -P ${CMAKE_CURRENT_LIST_DIR}/ambiguous_hdrname.cmake ) + endif() + unset(TEST_SUFFIX) unset(TEST_PREFIX) unset(TOOL_NAME) diff --git a/nifti2/ambiguous_hdrname.cmake b/nifti2/ambiguous_hdrname.cmake new file mode 100644 index 00000000..3353b6da --- /dev/null +++ b/nifti2/ambiguous_hdrname.cmake @@ -0,0 +1,30 @@ +# Drives the ambiguous-filename path, which must report the clash to its +# caller. A plain add_test cannot tell exit 1 from the abort it replaced. +file(REMOVE_RECURSE ${DIR}) +file(MAKE_DIRECTORY ${DIR}) + +execute_process(COMMAND ${TOOL} -make_im -new_dim 3 4 4 4 0 0 0 0 + -prefix ${DIR}/amb.nii RESULT_VARIABLE mk1) +execute_process(COMMAND ${TOOL} -make_im -new_dim 3 4 4 4 0 0 0 0 + -prefix ${DIR}/amb.nii.gz RESULT_VARIABLE mk2) +if(NOT mk1 STREQUAL "0" OR NOT mk2 STREQUAL "0") + message(FATAL_ERROR "could not create the ambiguous pair: ${mk1} ${mk2}") +endif() + +execute_process(COMMAND ${TOOL} -disp_hdr -infiles ${DIR}/amb + RESULT_VARIABLE rv ERROR_VARIABLE err OUTPUT_VARIABLE out) +if(NOT rv STREQUAL "1") + message(FATAL_ERROR "expected exit 1, got '${rv}'\n${err}") +endif() +if(NOT err MATCHES "Multiple possible filenames") + message(FATAL_ERROR "missing ambiguity diagnostic:\n${err}") +endif() + +# Unambiguous names must still resolve, so that returning NULL +# unconditionally could not pass. +file(REMOVE ${DIR}/amb.nii.gz) +execute_process(COMMAND ${TOOL} -disp_hdr -infiles ${DIR}/amb + RESULT_VARIABLE rv2 ERROR_VARIABLE err2 OUTPUT_VARIABLE out2) +if(NOT rv2 STREQUAL "0") + message(FATAL_ERROR "unambiguous name failed with '${rv2}'\n${err2}") +endif() diff --git a/niftilib/CMakeLists.txt b/niftilib/CMakeLists.txt index c66872b3..d3b09cd8 100644 --- a/niftilib/CMakeLists.txt +++ b/niftilib/CMakeLists.txt @@ -160,6 +160,13 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) add_test( NAME ${TEST_PREFIX}_tool_ascii_dup_attr COMMAND $ -disp_nim -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/dup_attr.nia ) set_tests_properties( ${TEST_PREFIX}_tool_ascii_dup_attr PROPERTIES PASS_REGULAR_EXPRESSION "dup2.nia" ) + if(FSLSTYLE_NAME_CONFLICTS) + add_test( NAME ${TEST_PREFIX}_ambiguous_hdrname + COMMAND ${CMAKE_COMMAND} -DTOOL=$ + -DDIR=${CMAKE_CURRENT_BINARY_DIR}/ambig1 + -P ${CMAKE_CURRENT_LIST_DIR}/ambiguous_hdrname.cmake ) + endif() + unset(TEST_SUFFIX) unset(TEST_PREFIX) unset(TOOL_NAME) diff --git a/niftilib/ambiguous_hdrname.cmake b/niftilib/ambiguous_hdrname.cmake new file mode 100644 index 00000000..3353b6da --- /dev/null +++ b/niftilib/ambiguous_hdrname.cmake @@ -0,0 +1,30 @@ +# Drives the ambiguous-filename path, which must report the clash to its +# caller. A plain add_test cannot tell exit 1 from the abort it replaced. +file(REMOVE_RECURSE ${DIR}) +file(MAKE_DIRECTORY ${DIR}) + +execute_process(COMMAND ${TOOL} -make_im -new_dim 3 4 4 4 0 0 0 0 + -prefix ${DIR}/amb.nii RESULT_VARIABLE mk1) +execute_process(COMMAND ${TOOL} -make_im -new_dim 3 4 4 4 0 0 0 0 + -prefix ${DIR}/amb.nii.gz RESULT_VARIABLE mk2) +if(NOT mk1 STREQUAL "0" OR NOT mk2 STREQUAL "0") + message(FATAL_ERROR "could not create the ambiguous pair: ${mk1} ${mk2}") +endif() + +execute_process(COMMAND ${TOOL} -disp_hdr -infiles ${DIR}/amb + RESULT_VARIABLE rv ERROR_VARIABLE err OUTPUT_VARIABLE out) +if(NOT rv STREQUAL "1") + message(FATAL_ERROR "expected exit 1, got '${rv}'\n${err}") +endif() +if(NOT err MATCHES "Multiple possible filenames") + message(FATAL_ERROR "missing ambiguity diagnostic:\n${err}") +endif() + +# Unambiguous names must still resolve, so that returning NULL +# unconditionally could not pass. +file(REMOVE ${DIR}/amb.nii.gz) +execute_process(COMMAND ${TOOL} -disp_hdr -infiles ${DIR}/amb + RESULT_VARIABLE rv2 ERROR_VARIABLE err2 OUTPUT_VARIABLE out2) +if(NOT rv2 STREQUAL "0") + message(FATAL_ERROR "unambiguous name failed with '${rv2}'\n${err2}") +endif() From 8cd7adef757d3fceeae00a8e2831befb0d53ed8b Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 06:54:32 -0500 Subject: [PATCH 10/11] ENH: Reach nifti_tool's header-modification failure paths Two paths returned without releasing the header, and one without releasing the duplicated name: a write into a directory that refuses it, and a duplication that fails because the input is the other NIfTI version. Both are reachable from the shipped tools with no data beyond what -make_im writes. The script asserts only that the paths are reached and reported; the leak is what the memcheck and sanitizer legs see. It skips itself for root, whom a read-only directory does not stop. --- nifti2/CMakeLists.txt | 5 ++ .../cmake_testscripts/mod_header_errpaths.sh | 50 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100755 nifti2/nifti_regress_test/cmake_testscripts/mod_header_errpaths.sh diff --git a/nifti2/CMakeLists.txt b/nifti2/CMakeLists.txt index c4f40b0b..c8161ec4 100644 --- a/nifti2/CMakeLists.txt +++ b/nifti2/CMakeLists.txt @@ -228,6 +228,11 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) -P ${CMAKE_CURRENT_LIST_DIR}/ambiguous_hdrname.cmake ) endif() + if(UNIX AND NIFTI_SHELL_SCRIPT_TESTS) + # Reaches the failure paths that have to release the header before returning. + add_test( NAME ${TEST_PREFIX}_mod_hdr_errpaths COMMAND sh ${NIFTI_TEST_SCRIPT_DIR}/mod_header_errpaths.sh $ $ ) + endif() + unset(TEST_SUFFIX) unset(TEST_PREFIX) unset(TOOL_NAME) diff --git a/nifti2/nifti_regress_test/cmake_testscripts/mod_header_errpaths.sh b/nifti2/nifti_regress_test/cmake_testscripts/mod_header_errpaths.sh new file mode 100755 index 00000000..7ef0fd88 --- /dev/null +++ b/nifti2/nifti_regress_test/cmake_testscripts/mod_header_errpaths.sh @@ -0,0 +1,50 @@ +#!/bin/sh +# Drive the two failure paths in nifti_tool's header modification, where +# the header and the duplicated name have to be released before returning. +# Both invocations are expected to fail, so a nonzero status cannot be the +# assertion: under a sanitizer it is also what a leak report exits with. +# The check is that the tool reported the failure and said nothing else. +# usage: mod_header_errpaths.sh [nifti1_tool] +NT=$1 +NT1=$2 +[ -x "$NT" ] || { echo "usage: $0 [nifti1_tool]"; exit 1; } + +if [ "$(id -u)" = "0" ]; then + echo "skipping: a read-only directory does not stop root" + exit 0 +fi + +tmp=$(mktemp -d) || exit 1 +trap 'chmod 755 "$tmp/ro" 2>/dev/null; rm -rf "$tmp"' EXIT +cd "$tmp" || exit 1 + +$NT -make_im -new_dim 3 4 4 4 0 0 0 0 -prefix anat0.nii || exit 1 +mkdir ro && chmod 555 ro + +# A. the write fails, so both the header and the duplicated name are held +out=$($NT -mod_hdr2 -prefix ro/anat1 -infiles anat0.nii \ + -mod_field qoffset_x -17.325 2>&1) +if [ $? -eq 0 ]; then + echo "FAIL: writing into a read-only directory succeeded" + echo "$out" + exit 1 +fi +case "$out" in + *Sanitizer*) echo "FAIL: sanitizer report on the write path"; echo "$out"; exit 1;; +esac + +# B. the duplication fails, so the header alone is held +if [ -n "$NT1" ] && [ -x "$NT1" ]; then + out=$($NT1 -mod_hdr -prefix ro/x1 -infiles anat0.nii \ + -mod_field qoffset_x -17.325 2>&1) + if [ $? -eq 0 ]; then + echo "FAIL: nifti1_tool accepted a NIFTI-2 input" + echo "$out" + exit 1 + fi + case "$out" in + *Sanitizer*) echo "FAIL: sanitizer report on the duplication path"; echo "$out"; exit 1;; + esac +fi + +echo "mod_header error paths reached" From 2fe4b60a249d0ae8fc316d021baae3c49ad0a2c8 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 06:55:19 -0500 Subject: [PATCH 11/11] ENH: Self-test the NIfTI content comparison helper c22_copy_image compares by content because gzip output is not reproducible across zlib implementations, but that property was only exercised where the system zlib is zlib-ng. Every CI leg ships stock zlib, so the comparison could revert to comparing compressed bytes and the suite would stay green. Extracting the helper lets it be tested directly. gzip -1 against -9 manufactures the byte difference on any zlib, and the two negative assertions pin that differing content is still reported as different and that the uncompressed path still works. --- nifti2/CMakeLists.txt | 5 +++ .../cmake_testscripts/c22_copy_image.sh | 16 +--------- .../cmake_testscripts/nii_cmp.sh | 15 +++++++++ .../cmake_testscripts/nii_cmp_selftest.sh | 32 +++++++++++++++++++ 4 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 nifti2/nifti_regress_test/cmake_testscripts/nii_cmp.sh create mode 100755 nifti2/nifti_regress_test/cmake_testscripts/nii_cmp_selftest.sh diff --git a/nifti2/CMakeLists.txt b/nifti2/CMakeLists.txt index c8161ec4..d369220c 100644 --- a/nifti2/CMakeLists.txt +++ b/nifti2/CMakeLists.txt @@ -233,6 +233,11 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) add_test( NAME ${TEST_PREFIX}_mod_hdr_errpaths COMMAND sh ${NIFTI_TEST_SCRIPT_DIR}/mod_header_errpaths.sh $ $ ) endif() + if(UNIX AND NIFTI_SHELL_SCRIPT_TESTS) + # Needs no external data, so it carries no NEEDS_DATA label. + add_test( NAME ${TEST_PREFIX}_nii_cmp_selftest COMMAND sh ${NIFTI_TEST_SCRIPT_DIR}/nii_cmp_selftest.sh ${NIFTI_TEST_SCRIPT_DIR} ) + endif() + unset(TEST_SUFFIX) unset(TEST_PREFIX) unset(TOOL_NAME) diff --git a/nifti2/nifti_regress_test/cmake_testscripts/c22_copy_image.sh b/nifti2/nifti_regress_test/cmake_testscripts/c22_copy_image.sh index 18c435c9..d2910d1b 100644 --- a/nifti2/nifti_regress_test/cmake_testscripts/c22_copy_image.sh +++ b/nifti2/nifti_regress_test/cmake_testscripts/c22_copy_image.sh @@ -12,21 +12,7 @@ OUT_DATA=$(dirname ${DATA}) #Need to write to separate directory cd ${OUT_DATA} -# Compare two NIfTI files by content rather than by compressed bytes. -# -# gzip output is not reproducible across zlib implementations: zlib-ng, -# which Arch, CachyOS and other current distributions ship as the system -# zlib, encodes the same input differently from stock zlib. Comparing -# the .gz files directly therefore fails on those systems even though the -# image data round-trips perfectly. Decompress first and compare that. -nii_cmp() { - if [ "${1##*.}" = "gz" ]; then - gzip -dc "$1" > "$1.raw" && gzip -dc "$2" > "$2.raw" || return 1 - cmp "$1.raw" "$2.raw" - return $? - fi - cmp "$1" "$2" -} +. "$(dirname "$0")/nii_cmp.sh" # note the main input file and prefix for all output files infile=$DATA/e4.60005.nii.gz diff --git a/nifti2/nifti_regress_test/cmake_testscripts/nii_cmp.sh b/nifti2/nifti_regress_test/cmake_testscripts/nii_cmp.sh new file mode 100644 index 00000000..a232c21f --- /dev/null +++ b/nifti2/nifti_regress_test/cmake_testscripts/nii_cmp.sh @@ -0,0 +1,15 @@ +# Compare two NIfTI files by content rather than by compressed bytes. +# +# gzip output is not reproducible across zlib implementations: zlib-ng, +# which Arch, CachyOS and other current distributions ship as the system +# zlib, encodes the same input differently from stock zlib. Comparing +# the .gz files directly therefore fails on those systems even though the +# image data round-trips perfectly. Decompress first and compare that. +nii_cmp() { + if [ "${1##*.}" = "gz" ]; then + gzip -dc "$1" > "$1.raw" && gzip -dc "$2" > "$2.raw" || return 1 + cmp "$1.raw" "$2.raw" + return $? + fi + cmp "$1" "$2" +} diff --git a/nifti2/nifti_regress_test/cmake_testscripts/nii_cmp_selftest.sh b/nifti2/nifti_regress_test/cmake_testscripts/nii_cmp_selftest.sh new file mode 100755 index 00000000..2eecaada --- /dev/null +++ b/nifti2/nifti_regress_test/cmake_testscripts/nii_cmp_selftest.sh @@ -0,0 +1,32 @@ +#!/bin/sh +# Verify nii_cmp compares NIfTI content rather than compressed bytes. +# +# gzip -1 and gzip -9 encode the same input differently, which manufactures +# the byte difference locally instead of waiting for a runner whose system +# zlib happens to be zlib-ng. +# usage: nii_cmp_selftest.sh +. "$1/nii_cmp.sh" + +tmp=$(mktemp -d) || exit 1 +trap 'rm -rf "$tmp"' EXIT +cd "$tmp" || exit 1 + +head -c 65536 /dev/urandom > payload +gzip -1 -c payload > a.nii.gz +gzip -9 -c payload > b.nii.gz +head -c 65536 /dev/urandom > other +gzip -c other > c.nii.gz + +if cmp -s a.nii.gz b.nii.gz; then + echo "SKIP: this gzip encodes -1 and -9 identically, nothing to compare" + exit 0 +fi + +nii_cmp a.nii.gz b.nii.gz || { + echo "FAIL: identical content reported different"; exit 1; } +nii_cmp a.nii.gz c.nii.gz >/dev/null 2>&1 && { + echo "FAIL: differing content reported same"; exit 1; } +cp payload d.nii && cp payload e.nii +nii_cmp d.nii e.nii || { echo "FAIL: uncompressed path broken"; exit 1; } + +echo "nii_cmp selftest passed"