From 4728be8e85b3b5d2f53e8beb256f9c09604d5861 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Mon, 21 Sep 2026 19:16:56 -0500 Subject: [PATCH 1/9] COMP: Install the tools the analysis jobs actually invoke Two of the five Build and Test jobs have never run to completion. sanitize-clang-linux invokes scan-build, which lives in clang-tools and was not installed, so the job exits 127 before configuring. rel-clang-macos asks brew for "sed", which is not a formula; brew fails the step and the job exits 1 before configuring. The GNU sed the dashboard scripts expect is gnu-sed. Both predate the workflow's first successful run, so neither has regressed; the trigger named the wrong branch until recently and the jobs never executed. --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e2bd685b..3bb63fbe 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -98,12 +98,12 @@ jobs: if: runner.os == 'Linux' run: | sudo apt-get update - sudo apt-get install -y cmake valgrind help2man + sudo apt-get install -y cmake valgrind help2man clang-tools - name: Install Dependencies (macOS) if: runner.os == 'macOS' run: | - brew install cmake sed help2man + brew install cmake gnu-sed help2man - name: Set Compiler and Flags run: | From fb14ede3e26a8531c96ce3af14fa0165eed5ecc1 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Sat, 15 Aug 2026 16:26:39 -0400 Subject: [PATCH 2/9] BUG: Stop loc_strnlen reading one byte past the buffer it is given loc_strnlen measures a string that need not be terminated, but it dereferences before testing the bound, so when no NUL appears in the first maxlen bytes the last iteration reads str[maxlen]. Both callers pass an unterminated buffer: axml_read_buf takes the caller's buffer and its length, and axml_read_file measures what fread returned, which fills the buffer for any larger file. AddressSanitizer on a buffer sized to its content reports a heap-buffer-overflow read 0 bytes after a 324-byte region. The returned length is unchanged wherever the old order was in bounds. --- cifti/afni_xml.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index a9910a1b..d7cc9a4f 100644 --- a/cifti/afni_xml.c +++ b/cifti/afni_xml.c @@ -1021,7 +1021,8 @@ static int64_t loc_strnlen(const char * str, int64_t maxlen) const char * sptr; int64_t len; - for( sptr=str, len=0; *sptr && len Date: Sat, 15 Aug 2026 16:55:29 -0400 Subject: [PATCH 3/9] BUG: Refuse a header whose dimensions overflow the voxel count Both header converters multiply the dimensions into nim->nvox without checking the product: for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) nim->nvox *= nhdr.dim[ii]; Seven NIFTI-1 dimensions of 32767 are enough to overflow int64_t, and a NIFTI-2 header needs only two dimensions to do it: nifti2_io.c:4838: runtime error: signed integer overflow: 1152780773560811521 * 32767 cannot be represented in type 'int64_t' Signed overflow is undefined, and what the compiler does produce is a voxel count that no longer describes the file. nvox then goes on to nifti_get_volsize(), which multiplies it by nbyper for another unchecked product, and that result is used as an allocation size and a read length. Both products are now checked before they are made, in the way the surrounding code already reports a bad header. A header that overflows is rejected with a message instead of producing a silently wrong image. No valid image is affected: an int64_t voxel count is more than any file can hold. Found by fuzzing the header converters with clang's libFuzzer under UndefinedBehaviorSanitizer. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_014m231RPDZPDbYDVxawxpjG --- nifti2/nifti2_io.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index 43a26f71..12cd59ab 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -4807,8 +4807,14 @@ nifti_image* nifti_convert_n1hdr2nim(nifti_1_header nhdr, const char * fname) nim->nv = nim->dim[6] = nhdr.dim[6]; nim->nw = nim->dim[7] = nhdr.dim[7]; - for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) + /* the product of the dimensions becomes an allocation size, so refuse + the header rather than let it wrap */ + for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ){ + if( nhdr.dim[ii] > 0 && nim->nvox > INT64_MAX / nhdr.dim[ii] ){ + free(nim); ERREX("dim[] overflows the voxel count"); + } nim->nvox *= nhdr.dim[ii]; + } /**- set the type of data in voxels and how many bytes per voxel */ @@ -4817,6 +4823,11 @@ nifti_image* nifti_convert_n1hdr2nim(nifti_1_header nhdr, const char * fname) nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ; if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); } + /* nifti_get_volsize() multiplies these two */ + if( nim->nvox > INT64_MAX / nim->nbyper ){ + free(nim); ERREX("dim[] and datatype overflow the volume size"); + } + /**- set the grid spacings */ nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ; @@ -5077,8 +5088,14 @@ nifti_image* nifti_convert_n2hdr2nim(nifti_2_header nhdr, const char * fname) nim->nv = nim->dim[6] = nhdr.dim[6]; nim->nw = nim->dim[7] = nhdr.dim[7]; - for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) + /* the product of the dimensions becomes an allocation size, so refuse + the header rather than let it wrap */ + for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ){ + if( nhdr.dim[ii] > 0 && nim->nvox > INT64_MAX / nhdr.dim[ii] ){ + free(nim); ERREX("dim[] overflows the voxel count"); + } nim->nvox *= nhdr.dim[ii]; + } /**- set the type of data in voxels and how many bytes per voxel */ @@ -5087,6 +5104,11 @@ nifti_image* nifti_convert_n2hdr2nim(nifti_2_header nhdr, const char * fname) nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ; if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); } + /* nifti_get_volsize() multiplies these two */ + if( nim->nvox > INT64_MAX / nim->nbyper ){ + free(nim); ERREX("dim[] and datatype overflow the volume size"); + } + /**- set the grid spacings */ nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ; From 191f84fd7ce05825aec3f5f1772b50859e57b2fd Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 18 Sep 2026 21:50:37 -0400 Subject: [PATCH 4/9] BUG: Guard the voxel count in the NIFTI-1 library too The first commit guards both converters in nifti2/nifti2_io.c. niftilib/nifti1_io.c has the same loop in nifti_convert_nhdr2nim(), on the same untrusted path, and was left unguarded. $ nifti1_tool -disp_nim -infiles huge.nii # dim[0]=7, dim[1..7]=32767 dim 32 8 7 32767 32767 32767 32767 32767 32767 32767 nvox 64 1 -1073512449 The wrapped count then becomes the data allocation size and the result of nifti_get_volsize(). With the guard the header is refused: ** ERROR: nifti_convert_nhdr2nim: dim[] overflows the voxel count nifti_image.nvox is a size_t in this library and an int64_t in the NIFTI-2 one, so the bound here is SIZE_MAX rather than INT64_MAX. nifti_get_volsize() is size_t * size_t to match. dim[0] is already bounded by need_nhdr_swap(), and the loop that raises every dim to at least 1 still runs first, so the dim[ii] > 0 test never has to reject. Two more nvox loops remain in each library, in nifti_update_dims_from_array() and update_nifti_image_for_brick_list(). Both take dims that a caller has already set rather than dims read from a file, and reach them from nifti_tool's command line, so they are left for a separate change. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_0176ZLorDY784us6dFAbE1ZJ --- niftilib/nifti1_io.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index 0580c629..f25d3df8 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -3766,8 +3766,15 @@ nifti_image* nifti_convert_nhdr2nim(struct nifti_1_header nhdr, nim->nv = nim->dim[6] = nhdr.dim[6]; nim->nw = nim->dim[7] = nhdr.dim[7]; - for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) + /* the product of the dimensions becomes an allocation size, so refuse + the header rather than let it wrap. nvox is a size_t here, where the + NIFTI-2 library uses int64_t, so the bound is SIZE_MAX. */ + for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ){ + if( nhdr.dim[ii] > 0 && nim->nvox > SIZE_MAX / (size_t)nhdr.dim[ii] ){ + free(nim); ERREX("dim[] overflows the voxel count"); + } nim->nvox *= nhdr.dim[ii]; + } /**- set the type of data in voxels and how many bytes per voxel */ @@ -3776,6 +3783,11 @@ nifti_image* nifti_convert_nhdr2nim(struct nifti_1_header nhdr, nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ; if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); } + /* nifti_get_volsize() multiplies these two */ + if( nim->nvox > SIZE_MAX / (size_t)nim->nbyper ){ + free(nim); ERREX("dim[] and datatype overflow the volume size"); + } + /**- set the grid spacings */ nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ; From 7d0bd08bfe49f006edd558563df31ce42eb7edfc Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Sat, 15 Aug 2026 16:41:33 -0400 Subject: [PATCH 5/9] BUG: Check dim[0] before using it to index dim[] in the NIFTI-2 converter nifti_convert_n2hdr2nim() uses nhdr.dim[0], the number of dimensions, as a loop bound over the eight-element dim[] array: for( ii=2 ; ii <= nhdr.dim[0] ; ii++ ) ... for( ii=nhdr.dim[0]+1 ; ii <= 7 ; ii++ ) ... for( ii=1 ; ii <= nhdr.dim[0] ; ii++ ) ... It never checks that dim[0] is in [0,7]. The NIFTI-1 converter gets that check for free, because need_nhdr_swap() rejects a dim[0] outside [1,7] in either byte order, but the NIFTI-2 path decides swapping from sizeof_hdr and reaches the loops with whatever the file said. A NIFTI-2 header with a large negative dim[0] therefore starts the second loop at a wild negative index, reads far outside the header and segfaults. This is not confined to the header API: nifti_image_read() gets there for any file with a valid 540 byte NIFTI-2 header, so a 604 byte file crashes nifti_tool: $ nifti_tool -disp_nim -infiles bad_n2_dim0.nii nifti2_io.c:5079: runtime error: index -6727636073941130588 out of bounds for type 'int64_t[8]' AddressSanitizer: SEGV ... in nifti_convert_n2hdr2nim dim[0] is now range checked in the same place, and in the same style, as the dim[1] check just below it. Zero stays acceptable, as it is on the NIFTI-1 side. Valid headers are unaffected: dim[0] outside [0,7] has no meaning in either format. Found by fuzzing nifti_convert_n2hdr2nim() with clang's libFuzzer under AddressSanitizer. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_014m231RPDZPDbYDVxawxpjG --- nifti2/nifti2_io.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index 43a26f71..566005c7 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -5036,6 +5036,14 @@ nifti_image* nifti_convert_n2hdr2nim(nifti_2_header nhdr, const char * fname) ERREX("bad datatype") ; } + /* dim[0] is the number of dimensions and the loops below index dim[] + with it; the NIFTI-1 path gets this check from need_nhdr_swap() */ + if( nhdr.dim[0] < 0 || nhdr.dim[0] > 7 ) + { + free(nim); + ERREX("bad dim[0]") ; + } + if( nhdr.dim[1] <= 0 ) { free(nim); From 6ab81d3532ff3723f0df183509fd0593b326d8ce Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Sat, 15 Aug 2026 16:26:11 -0400 Subject: [PATCH 6/9] BUG: Swap a nifti_1_header as a NIFTI-1 header, not as whatever its magic claims swap_nifti_header() takes a void pointer and a version number, and picks the struct to swap from the version: if ( ni_ver == 0 ) nifti_swap_as_analyze((nifti_analyze75 *)hdr); else if( ni_ver == 1 ) nifti_swap_as_nifti1((nifti_1_header *)hdr); else if( ni_ver == 2 ) nifti_swap_as_nifti2((nifti_2_header *)hdr); Two callers pass it a nifti_1_header, 348 bytes, together with the version taken from that header's own magic string by NIFTI_VERSION(). A file whose magic is "n+2" therefore has 540 bytes swapped in place: 192 bytes of read and write past the end of the caller's stack object. nifti_read_n1_hdr() reads the header straight from a file, so the bytes that decide this come from the file being read. A 348 byte file with sizeof_hdr = 348, dim[0] byte-swapped, magic = "n+2" is enough; need_nhdr_swap() then reports that swapping is needed and the overflow happens before any validity check. AddressSanitizer: ERROR: AddressSanitizer: stack-buffer-overflow READ of size 1 ... #0 nifti_swap_4bytes nifti2_io.c:3051 #1 nifti_swap_as_nifti2 nifti2_io.c:3194 #2 nifti_read_n1_hdr nifti2_io.c:5394 Address ... is located in stack of thread T0 at offset 412 in frame [64, 412) 'nhdr' (line 5339) <== Memory access ... overflows nifti_convert_n1hdr2nim() has the same line and takes its header from the caller, so any application that fills a nifti_1_header itself can reach it too. Both call sites know the struct they hold, so both now ask for the swap that struct supports: analyze when the magic is absent, NIFTI-1 otherwise. Headers claiming versions 3 to 9, which swap_nifti_header() previously refused with a message and left unswapped, are now swapped as NIFTI-1 as well, which is the only interpretation 348 bytes allow. nifti1_io.c is not affected: its swap_nifti_header() takes a nifti_1_header * and a flag, so it cannot choose a wider struct. Found by fuzzing nifti_convert_n1hdr2nim() with clang's libFuzzer under AddressSanitizer. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_014m231RPDZPDbYDVxawxpjG --- nifti2/nifti2_io.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index 43a26f71..2602a42f 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -4744,7 +4744,10 @@ nifti_image* nifti_convert_n1hdr2nim(nifti_1_header nhdr, const char * fname) } if( doswap ) { if ( g_opts.debug > 3 ) disp_nifti_1_header("-d ni1 pre-swap: ", &nhdr); - swap_nifti_header( &nhdr , ni_ver ) ; + /* nhdr is a nifti_1_header, so swap it as one. ni_ver comes from the + magic string, and a magic of "n+2" would otherwise have + swap_nifti_header() treat these 348 bytes as a 540 byte header. */ + swap_nifti_header( &nhdr , ni_ver ? 1 : 0 ) ; } if ( g_opts.debug > 2 ) disp_nifti_1_header("-d nhdr2nim : ", &nhdr); @@ -5367,7 +5370,9 @@ nifti_1_header * nifti_read_n1_hdr(const char * hname, int *swapped, int check) if( lswap ) { if ( g_opts.debug > 3 ) disp_nifti_1_header("-d nhdr pre-swap: ", &nhdr); - swap_nifti_header( &nhdr , NIFTI_VERSION(nhdr) ) ; + /* only sizeof(nifti_1_header) bytes were read, so swap as that; see + the same guard in nifti_convert_n1hdr2nim() */ + swap_nifti_header( &nhdr , NIFTI_VERSION(nhdr) ? 1 : 0 ) ; } if ( g_opts.debug > 2 ) disp_nifti_1_header("-d nhdr post-swap: ", &nhdr); From bd353ead8cb2e1b188f1ae4318ecc199e1689702 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 18 Sep 2026 21:49:03 -0400 Subject: [PATCH 7/9] BUG: Swap the tool's headers at their own width too nifti_tool.c has the same defect this branch fixes in the library: it passes NIFTI_VERSION(*nhdr), read from the magic string, to swap_nifti_header(), which selects the struct to swap by that number. act_mod_hdrs() and act_swap_hdrs() hold a nifti_1_header of 348 bytes. A magic of "n+2" makes NIFTI_VERSION() return 2, so swap_nifti_header() treats the allocation as a 540 byte nifti_2_header and reads and writes past its end. Both functions refuse a header that is valid NIFTI-2, but a header that is valid as neither version reaches the call. $ printf ... > evil.nii # 348 bytes, sizeof_hdr byte swapped, # magic "n+2" $ nifti_tool -mod_hdr -mod_field descrip hello -overwrite \ -infiles evil.nii ==2747364==ERROR: AddressSanitizer: heap-buffer-overflow #0 nifti_swap_4bytes nifti2_io.c:3029 #1 nifti_swap_as_nifti2 nifti2_io.c:3172 #2 swap_nifti_header nifti2_io.c:3131 #3 act_mod_hdrs nifti_tool.c:3389 0 bytes after 348-byte region allocated in nifti_read_n1_hdr() Both now pass 1, or 0 for ANALYZE, which are the two 348 byte layouts. act_mod_hdr2s() holds a nifti_2_header and gets the explicit 2, as act_swap_hdrs() already does for its own NIFTI-2 display path. old_swap_nifti_header() takes a nifti_1_header and a boolean, so the calls beside these need no change. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_0176ZLorDY784us6dFAbE1ZJ --- nifti2/nifti_tool.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/nifti2/nifti_tool.c b/nifti2/nifti_tool.c index 7dfa3272..85bc3f54 100644 --- a/nifti2/nifti_tool.c +++ b/nifti2/nifti_tool.c @@ -3391,7 +3391,11 @@ int act_mod_hdrs( nt_opts * opts ) nifti_image_free(nim); } else if ( swap ) - swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); + /* nhdr is a nifti_1_header, so swap it as one. NIFTI_VERSION() + reads the magic string, and a magic of "n+2" would otherwise + have swap_nifti_header() treat these 348 bytes as a 540 byte + header. ni_ver 0 and 1 are both 348 byte layouts. */ + swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr) ? 1 : 0); /* if all is well, overwrite header in fname dataset */ (void)write_hdr_to_file(nhdr, fname); /* errors printed in function */ @@ -3512,7 +3516,9 @@ int act_mod_hdr2s( nt_opts * opts ) nifti_image_free(nim); } else if ( swap ) - swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); + /* nhdr is a nifti_2_header; use the explicit version rather than + the magic, which could claim "n+1" and swap 540 bytes as 348 */ + swap_nifti_header(nhdr, 2); /* if all is well, overwrite header in fname dataset */ (void)write_hdr2_to_file(nhdr, fname); /* errors printed in function */ @@ -3613,8 +3619,12 @@ int act_swap_hdrs( nt_opts * opts ) swap_nifti_header(nhdr, 0); /* undo ANALYZE */ swap_nifti_header(nhdr, 1); /* swap NIFTI */ } else if ( opts->swap_old ) { - /* undo whichever was done and apply the old way */ - swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); + /* undo whichever was done and apply the old way. As above, + nhdr is a nifti_1_header, so it must not be swapped as a + 540 byte NIFTI-2 header just because its magic says "n+2". + old_swap_nifti_header() takes a nifti_1_header and a + boolean, so it needs no such guard. */ + swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr) ? 1 : 0); old_swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); } From 03d8b6cc11e9d3812e888add1bc71623272160f2 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 14 Aug 2026 23:04:50 -0400 Subject: [PATCH 8/9] STYLE: Add the missing newline at end of nifti1_tool.h The only file in the tree without a trailing newline, and the only -Wnewline-eof warning. C11 5.1.1.2p1 requires a non-empty source file to end in a newline not immediately preceded by a backslash, so this is undefined behaviour rather than only a diff annoyance. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01KSPnbwpDjVcAYqDdVqLkMU --- niftilib/nifti1_tool.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/niftilib/nifti1_tool.h b/niftilib/nifti1_tool.h index a8ca5ddd..14912ed9 100644 --- a/niftilib/nifti1_tool.h +++ b/niftilib/nifti1_tool.h @@ -157,4 +157,4 @@ nifti_1_header * nt_read_header(nt_opts * opts, const char * fname, int * swappe int check); -#endif /* NIFTI1_TOOL_H */ \ No newline at end of file +#endif /* NIFTI1_TOOL_H */ From e584a0e9cb5ca24673a2fa6dde8d0d04488c2d23 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Tue, 4 Feb 2025 16:01:22 -0500 Subject: [PATCH 9/9] ENH: Replace `exit` with `return NULL` to avoid crashing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In C++ library code, it’s generally considered bad practice to use assert for input validation or error handling. --Terminates the program exit causes dependant applications to terminate - not ideal for libraries meant to be reused safely by others. --Not user-friendly Gives no chance to recover or handle the failure gracefully. --Violates encapsulation Forces application behavior based on internal library assumptions. --- nifti2/nifti2_io.c | 134 +++++++++++++++++++++---------------------- niftilib/nifti1_io.c | 98 +++++++++++-------------------- 2 files changed, 100 insertions(+), 132 deletions(-) diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index f0bd9aae..08a9b99e 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -3767,7 +3767,7 @@ char * nifti_findhdrname(const char* fname) if (nifti_fileexists(gzname)) { fprintf(stderr,"Image Exception : Multiple possible filenames detected for basename (*.nii, *.nii.gz): %s\n", basename); free(gzname); - exit(134); + return NULL; } free(gzname); return hdrname; @@ -6020,8 +6020,8 @@ nifti_image *nifti_image_read( const char *hname , int read_data ) #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); + fprintf(stderr,"Image Exception Unsupported datatype (COMPLEX): use fslcomplex to manipulate: %s\n", hname); + return NULL; } #endif @@ -7894,68 +7894,68 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, #ifdef PIGZ #ifdef HAVE_ZLIB -int doPigz2(nifti_image *nim, struct nifti_2_header nhdr, const nifti_brick_list * NBL) { - FILE *pigzPipe; - char command[768]; - strcpy(command, "pigz" ); - strcat(command, " -n -f > \""); - strcat(command, nim->fname); - strcat(command, "\""); - #ifdef _MSC_VER - if (( pigzPipe = _popen(command, "w")) == NULL) - return -1; - #else - if (( pigzPipe = popen(command, "w")) == NULL) - return -1; - #endif - znzFile fp; - fp = (znzFile) calloc(1,sizeof(struct znzptr)); - fp->zfptr = NULL; - fp->withz = 0; - fp->nzfptr = pigzPipe; - fwrite(&nhdr, sizeof(nhdr), 1, pigzPipe); - if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) - nifti_write_extensions(fp,nim); - nifti_write_all_data(fp,nim,NBL); - #ifdef _MSC_VER - _pclose(pigzPipe); - #else - pclose(pigzPipe); - #endif - free(fp); - return 0; -} - -int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { - FILE *pigzPipe; - char command[768]; - strcpy(command, "pigz" ); - strcat(command, " -n -f > \""); - strcat(command, nim->fname); - strcat(command, "\""); - #ifdef _MSC_VER - if (( pigzPipe = _popen(command, "w")) == NULL) - return -1; - #else - if (( pigzPipe = popen(command, "w")) == NULL) - return -1; - #endif - znzFile fp; - fp = (znzFile) calloc(1,sizeof(struct znzptr)); - fp->zfptr = NULL; - fp->withz = 0; - fp->nzfptr = pigzPipe; - fwrite(&nhdr, sizeof(nhdr), 1, pigzPipe); - if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) - nifti_write_extensions(fp,nim); - nifti_write_all_data(fp,nim,NBL); - #ifdef _MSC_VER - _pclose(pigzPipe); - #else - pclose(pigzPipe); - #endif - free(fp); - return 0; +static int doPigz2(nifti_image * nim, const nifti_2_header * nhdr, const nifti_brick_list * NBL) { + FILE *pigzPipe; + char command[4096]; + strcpy(command, "pigz" ); + strcat(command, " -n -f > \""); + strcat(command, nim->fname); + strcat(command, "\""); +#ifdef _MSC_VER + if (( pigzPipe = _popen(command, "w")) == NULL) + return -1; +#else + if (( pigzPipe = popen(command, "w")) == NULL) + return -1; +#endif + znzFile fp; + fp = (znzFile) calloc(1, sizeof(struct znzptr)); + fp->zfptr = NULL; + fp->withz = 0; + fp->nzfptr = pigzPipe; + fwrite(nhdr, sizeof(*nhdr), 1, pigzPipe); + if ( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) + nifti_write_extensions(fp, nim); + nifti_write_all_data(fp, nim, NBL); +#ifdef _MSC_VER + _pclose(pigzPipe); +#else + pclose(pigzPipe); +#endif + free(fp); + return 0; +} + +static int doPigz(nifti_image * nim, const nifti_1_header * nhdr, const nifti_brick_list * NBL) { + FILE *pigzPipe; + char command[4096]; + strcpy(command, "pigz" ); + strcat(command, " -n -f > \""); + strcat(command, nim->fname); + strcat(command, "\""); +#ifdef _MSC_VER + if (( pigzPipe = _popen(command, "w")) == NULL) + return -1; +#else + if (( pigzPipe = popen(command, "w")) == NULL) + return -1; +#endif + znzFile fp; + fp = (znzFile) calloc(1, sizeof(struct znzptr)); + fp->zfptr = NULL; + fp->withz = 0; + fp->nzfptr = pigzPipe; + fwrite(nhdr, sizeof(*nhdr), 1, pigzPipe); + if ( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) + nifti_write_extensions(fp, nim); + nifti_write_all_data(fp, nim, NBL); +#ifdef _MSC_VER + _pclose(pigzPipe); +#else + pclose(pigzPipe); +#endif + free(fp); + return 0; } #endif //HAVE_ZLIB #endif //PIGZ @@ -8078,10 +8078,10 @@ static int nifti_image_write_engine(nifti_image *nim, int write_opts, char pigzKey[5] = "PIGZ"; if ((value != NULL) && (strstr(value,pigzKey))) { if( nver == 2 ) { - if (doPigz2(nim, n2hdr, NBL) == 0) + if (doPigz2(nim, &n2hdr, NBL) == 0) return 0; } else { - if (doPigz(nim, n1hdr, NBL) == 0) //success writing with pigz + if (doPigz(nim, &n1hdr, NBL) == 0) //success writing with pigz return 0; } } diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index f25d3df8..ed43a99f 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -2829,7 +2829,7 @@ char * nifti_findhdrname(const char* fname) if (nifti_fileexists(gzname)) { fprintf(stderr,"Image Exception : Multiple possible filenames detected for basename (*.nii, *.nii.gz): %s\n", basename); free(gzname); - exit(134); + return NULL; } free(gzname); return hdrname; @@ -4307,7 +4307,7 @@ nifti_image *nifti_image_read( const char *hname , int read_data ) #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); + fprintf(stderr,"Image Exception Unsupported datatype (COMPLEX): use fslcomplex to manipulate: %s\n", hname); exit(13); } #endif @@ -5788,68 +5788,36 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, #ifdef PIGZ #ifdef HAVE_ZLIB -int doPigz2(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { - FILE *pigzPipe; - char command[768]; - strcpy(command, "pigz" ); - strcat(command, " -n -f > \""); - strcat(command, nim->fname); - strcat(command, "\""); - #ifdef _MSC_VER - if (( pigzPipe = _popen(command, "w")) == NULL) - return -1; - #else - if (( pigzPipe = popen(command, "w")) == NULL) - return -1; - #endif - znzFile fp; - fp = (znzFile) calloc(1,sizeof(struct znzptr)); - fp->zfptr = NULL; - fp->withz = 0; - fp->nzfptr = pigzPipe; - fwrite(&nhdr, sizeof(nhdr), 1, pigzPipe); - if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) - nifti_write_extensions(fp,nim); - nifti_write_all_data(fp,nim,NBL); - #ifdef _MSC_VER - _pclose(pigzPipe); - #else - pclose(pigzPipe); - #endif - free(fp); - return 0; -} - -int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { - FILE *pigzPipe; - char command[768]; - strcpy(command, "pigz" ); - strcat(command, " -n -f > \""); - strcat(command, nim->fname); - strcat(command, "\""); - #ifdef _MSC_VER - if (( pigzPipe = _popen(command, "w")) == NULL) - return -1; - #else - if (( pigzPipe = popen(command, "w")) == NULL) - return -1; - #endif - znzFile fp; - fp = (znzFile) calloc(1,sizeof(struct znzptr)); - fp->zfptr = NULL; - fp->withz = 0; - fp->nzfptr = pigzPipe; - fwrite(&nhdr, sizeof(nhdr), 1, pigzPipe); - if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) - nifti_write_extensions(fp,nim); - nifti_write_all_data(fp,nim,NBL); - #ifdef _MSC_VER - _pclose(pigzPipe); - #else - pclose(pigzPipe); - #endif - free(fp); - return 0; +static int doPigz(nifti_image * nim, const nifti_1_header * nhdr, const nifti_brick_list * NBL) { + FILE *pigzPipe; + char command[4096]; + strcpy(command, "pigz" ); + strcat(command, " -n -f > \""); + strcat(command, nim->fname); + strcat(command, "\""); +#ifdef _MSC_VER + if (( pigzPipe = _popen(command, "w")) == NULL) + return -1; +#else + if (( pigzPipe = popen(command, "w")) == NULL) + return -1; +#endif + znzFile fp; + fp = (znzFile) calloc(1, sizeof(struct znzptr)); + fp->zfptr = NULL; + fp->withz = 0; + fp->nzfptr = pigzPipe; + fwrite(nhdr, sizeof(*nhdr), 1, pigzPipe); + if ( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) + nifti_write_extensions(fp,nim); + nifti_write_all_data(fp,nim,NBL); +#ifdef _MSC_VER + _pclose(pigzPipe); +#else + pclose(pigzPipe); +#endif + free(fp); + return 0; } #endif //HAVE_ZLIB #endif //PIGZ @@ -5951,7 +5919,7 @@ static int nifti_image_write_engine(nifti_image *nim, int write_opts, //export AFNI_COMPRESSOR=PIGZ char pigzKey[5] = "PIGZ"; if ((value != NULL) && (strstr(value,pigzKey))) { - if (doPigz(nim, nhdr, NBL) == 0) //success writing with pigz + if (doPigz(nim, &nhdr, NBL) == 0) //success writing with pigz return 0; } }