Skip to content
Merged
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
9 changes: 9 additions & 0 deletions cifti/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<TARGET_FILE:${NIFTI_PACKAGE_PREFIX}cifti_tool>
-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()
Binary file added cifti/testdata/cext_unterminated.nii
Binary file not shown.
49 changes: 49 additions & 0 deletions nifti2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,55 @@ 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 $<TARGET_FILE:${TOOL_NAME}> -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 $<TARGET_FILE:${TOOL_NAME}> -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 $<TARGET_FILE:${TOOL_NAME}> -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 $<TARGET_FILE:${TOOL_NAME}> -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 $<TARGET_FILE:${TOOL_NAME}> -disp_nim -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/n1_overflow.nii )
add_test( NAME ${TEST_PREFIX}_tool_reject_nvox_overflow_n2 COMMAND $<TARGET_FILE:${TOOL_NAME}> -disp_nim -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/n2_overflow.nii )
add_test( NAME ${TEST_PREFIX}_tool_reject_volsize_overflow COMMAND $<TARGET_FILE:${TOOL_NAME}> -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" )

# 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 $<TARGET_FILE:${TOOL_NAME}> -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=$<TARGET_FILE:${TOOL_NAME}>
-DDIR=${CMAKE_CURRENT_BINARY_DIR}/ambig
-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 $<TARGET_FILE:${TOOL_NAME}> $<TARGET_FILE:${NIFTI_PACKAGE_PREFIX}nifti1_tool> )
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)
Expand Down
30 changes: 30 additions & 0 deletions nifti2/ambiguous_hdrname.cmake
Original file line number Diff line number Diff line change
@@ -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()
14 changes: 7 additions & 7 deletions nifti2/nifti2_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -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);
Expand Down
16 changes: 1 addition & 15 deletions nifti2/nifti_regress_test/cmake_testscripts/c22_copy_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions nifti2/nifti_regress_test/cmake_testscripts/mod_header_errpaths.sh
Original file line number Diff line number Diff line change
@@ -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 <nifti_tool> [nifti1_tool]
NT=$1
NT1=$2
[ -x "$NT" ] || { echo "usage: $0 <nifti_tool> [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"
15 changes: 15 additions & 0 deletions nifti2/nifti_regress_test/cmake_testscripts/nii_cmp.sh
Original file line number Diff line number Diff line change
@@ -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"
}
32 changes: 32 additions & 0 deletions nifti2/nifti_regress_test/cmake_testscripts/nii_cmp_selftest.sh
Original file line number Diff line number Diff line change
@@ -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 <helper-dir>
. "$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"
Binary file added nifti2/testdata/dup_attr.nia
Binary file not shown.
Binary file added nifti2/testdata/n1_magic_n2.nii
Binary file not shown.
Binary file added nifti2/testdata/n1_overflow.nii
Binary file not shown.
Binary file added nifti2/testdata/n1_volsize.nii
Binary file not shown.
Binary file added nifti2/testdata/n2_bad_dim0.nii
Binary file not shown.
Binary file added nifti2/testdata/n2_overflow.nii
Binary file not shown.
19 changes: 19 additions & 0 deletions niftilib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,25 @@ 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 $<TARGET_FILE:${TOOL_NAME}> -disp_nim -infiles ${CMAKE_CURRENT_LIST_DIR}/testdata/n1_overflow.nii )
add_test( NAME ${TEST_PREFIX}_tool_reject_volsize_overflow COMMAND $<TARGET_FILE:${TOOL_NAME}> -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" )

# The NIFTI-1 library carries the same ASCII reader.
add_test( NAME ${TEST_PREFIX}_tool_ascii_dup_attr COMMAND $<TARGET_FILE:${TOOL_NAME}> -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=$<TARGET_FILE:${TOOL_NAME}>
-DDIR=${CMAKE_CURRENT_BINARY_DIR}/ambig1
-P ${CMAKE_CURRENT_LIST_DIR}/ambiguous_hdrname.cmake )
endif()

unset(TEST_SUFFIX)
unset(TEST_PREFIX)
unset(TOOL_NAME)
Expand Down
30 changes: 30 additions & 0 deletions niftilib/ambiguous_hdrname.cmake
Original file line number Diff line number Diff line change
@@ -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()
14 changes: 7 additions & 7 deletions niftilib/nifti1_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -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);
Expand Down
Binary file added niftilib/testdata/dup_attr.nia
Binary file not shown.
Binary file added niftilib/testdata/n1_overflow.nii
Binary file not shown.
Binary file added niftilib/testdata/n1_volsize.nii
Binary file not shown.
Loading