From 8c5d43ac192209dce055b15b3b942a618b4517d5 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 07:44:30 -0500 Subject: [PATCH 01/11] COMP: Give the pigz writers internal linkage doPigz and doPigz2 are called only from the file that defines them and appear in no header, so they are internal. Declaring them so is what lets -Wmissing-declarations be promoted to an error on the FSLSTYLE path, where they were the only offenders. The copy of doPigz2 in the NIFTI-1 library is byte-identical to doPigz beside it and nothing calls it, so it is excluded from the build rather than given linkage it does not need. The exported symbol set is unchanged: this code compiles only under PIGZ, which the shared build behind the baseline does not define. (cherry picked from commit 1abbd8572bc489638c2568e0151086e8c2914a5b) --- nifti2/nifti2_io.c | 4 ++-- niftilib/nifti1_io.c | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index 5b42cfed..c0111577 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -7922,7 +7922,7 @@ 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) { +static int doPigz2(nifti_image *nim, struct nifti_2_header nhdr, const nifti_brick_list * NBL) { FILE *pigzPipe; char command[768]; strcpy(command, "pigz" ); @@ -7954,7 +7954,7 @@ int doPigz2(nifti_image *nim, struct nifti_2_header nhdr, const nifti_brick_list return 0; } -int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { +static int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { FILE *pigzPipe; char command[768]; strcpy(command, "pigz" ); diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index a0b3fd4c..ead6dc26 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -5809,6 +5809,7 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, #ifdef PIGZ #ifdef HAVE_ZLIB +#if 0 /* unused here: identical to doPigz below */ int doPigz2(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { FILE *pigzPipe; char command[768]; @@ -5840,8 +5841,9 @@ int doPigz2(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list free(fp); return 0; } +#endif -int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { +static int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { FILE *pigzPipe; char command[768]; strcpy(command, "pigz" ); From 333253181ab53bb787be589b2168af799940aebe Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 07:44:54 -0500 Subject: [PATCH 02/11] COMP: Make an undeclared external function fail the build -Wmissing-declarations is already in the project's clean set, but a warning in a build that passes anyway is not read, so two changes that added declarations reached master with nothing to stop the next one. Both FSLSTYLE settings are covered. The flag catches nifti_fileexists on one side and axml_recur_find_xml, FslGetHdrImgNames and FslSetIntensityScaling on the other, each of which reached master as a warning nobody acted on. (cherry picked from commit b4876bf7dfe0d650c2cb675aa7a080416b34479f) --- .github/workflows/cmake-multi-platform.yml | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index 68197e71..57639632 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -196,3 +196,37 @@ jobs: exit 1 fi echo "Exported symbols match the baseline." + + missing-declarations: + # A function defined without a prior declaration is either missing + # from its header or should have been static. Both shapes reached + # master before, because the flag is in the project's clean set but + # nothing makes it fatal, and a warning in a build that passes is + # not read. + # + # Promoting this one flag needs no preparatory cleanup. The whole + # NIFTI_WARNINGS_AS_ERRORS set is still blocked by + # -Wmaybe-uninitialized in fslio.c, and waiting for that would + # leave this uncovered meanwhile. + name: no undeclared external functions (FSLSTYLE=${{ matrix.fslstyle }}) + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + fslstyle: ["OFF", "ON"] + steps: + - uses: actions/checkout@v4 + - name: Install dependencies + run: sudo apt-get update && sudo apt-get install -y libexpat1-dev zlib1g-dev ninja-build + - name: Configure CMake + run: > + cmake -G Ninja -B ${{ github.workspace }}/build + -DCMAKE_BUILD_TYPE=Release + -DCMAKE_C_FLAGS=-Werror=missing-declarations + -DNIFTI_BUILD_APPLICATIONS=ON + -DUSE_CIFTI_CODE=ON + -DUSE_FSL_CODE=ON + -DFSLSTYLE=${{ matrix.fslstyle }} + -S ${{ github.workspace }} + - name: Build + run: cmake --build ${{ github.workspace }}/build From fda99660b8c9fa977ccf39c607abe385f9a55a8b Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 09:31:10 -0500 Subject: [PATCH 03/11] COMP: Build and test on Windows Twelve source files carry WIN32 or _MSC_VER guards and no workflow has ever compiled them, so a change that breaks the Windows path is invisible here and surfaces only when a consumer reports it. Static and shared, because the ZNZ_API and NIFTI_API decorations differ between them and only the shared build exercises the dllexport path. zlib and expat come from vcpkg, which the runner image already carries. VCPKG_INSTALLATION_ROOT is set by the runner image rather than by the workflow, so the toolchain path is read in PowerShell and checked before cmake runs, which reports a missing toolchain as itself rather than as a CMake error several lines removed from the cause. (cherry picked from commit 250532501b313598d690572acf89c29d9b0bdeeb) --- .github/workflows/cmake-multi-platform.yml | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index 57639632..e7250e81 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -230,3 +230,47 @@ jobs: -S ${{ github.workspace }} - name: Build run: cmake --build ${{ github.workspace }}/build + + windows: + # Twelve source files carry WIN32 or _MSC_VER guards and nothing has + # compiled them for as long as the workflows have existed, so a change + # that breaks the Windows path is invisible until a consumer reports + # it. zlib comes from vcpkg because the project requires it. + name: windows-latest msvc ${{ matrix.linkage }} + runs-on: windows-latest + strategy: + fail-fast: false + matrix: + include: + - linkage: static + shared: "OFF" + triplet: x64-windows-static + - linkage: shared + shared: "ON" + triplet: x64-windows + steps: + - uses: actions/checkout@v4 + - name: Install zlib and expat + run: vcpkg install zlib:${{ matrix.triplet }} expat:${{ matrix.triplet }} + - name: Configure CMake + shell: pwsh + run: | + $toolchain = Join-Path $env:VCPKG_INSTALLATION_ROOT 'scripts/buildsystems/vcpkg.cmake' + Write-Host "vcpkg toolchain: $toolchain" + if (-not (Test-Path $toolchain)) { throw "toolchain file not found: $toolchain" } + cmake -B "${{ github.workspace }}/build" ` + -DCMAKE_TOOLCHAIN_FILE="$toolchain" ` + -DVCPKG_TARGET_TRIPLET=${{ matrix.triplet }} ` + -DBUILD_SHARED_LIBS=${{ matrix.shared }} ` + -DCMAKE_RUNTIME_OUTPUT_DIRECTORY="${{ github.workspace }}/build/bin" ` + -DNIFTI_BUILD_APPLICATIONS=ON ` + -DUSE_NIFTI2_CODE=ON ` + -DUSE_CIFTI_CODE=ON ` + -S "${{ github.workspace }}" + - name: Build + run: cmake --build ${{ github.workspace }}/build --config Release + - name: Test + working-directory: ${{ github.workspace }}/build + # A test that cannot find a DLL can raise a modal dialog and block the + # runner rather than exit, so every test carries a timeout. + run: ctest --build-config Release --output-on-failure --no-tests=ignore --timeout 120 From 3ddecc795f1fedfd7d9ce1c41083a265d1470ab5 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 09:31:20 -0500 Subject: [PATCH 04/11] COMP: Export the afni_xml half of the cifti library afni_xml_io.h decorates its declarations with CIF_API and afni_xml.h decorated none of its own, so a Windows shared build produced a DLL missing every axml_ entry point and both cifti tools failed to link against the library they are built with. The macro definition moves to afni_xml.h, which afni_xml_io.h includes at its top, so one definition now serves both headers rather than each carrying its own. Nothing changes where the attribute expands to default visibility: the exported set of the shared build is byte-identical. Found by the Windows job added in the preceding commit. (cherry picked from commit 63b361adc95805ae58d235929a3141b734ba6e46) --- cifti/afni_xml.h | 68 ++++++++++++++++++++++++++++++--------------- cifti/afni_xml_io.h | 23 --------------- 2 files changed, 46 insertions(+), 45 deletions(-) diff --git a/cifti/afni_xml.h b/cifti/afni_xml.h index ecc4ab3e..16d0ae7c 100644 --- a/cifti/afni_xml.h +++ b/cifti/afni_xml.h @@ -70,38 +70,62 @@ typedef struct { } afni_xml_control; +#ifndef CIF_API + #if defined(_WIN32) || defined(__CYGWIN__) + #if defined(CIFTI_BUILD_SHARED) + #ifdef __GNUC__ + #define CIF_API __attribute__ ((dllexport)) + #else + #define CIF_API __declspec( dllexport ) + #endif + #elif defined(CIFTI_USE_SHARED) + #ifdef __GNUC__ + #define CIF_API __attribute__ ((dllimport)) + #else + #define CIF_API __declspec( dllimport ) + #endif + #else + #define CIF_API + #endif + #elif (defined(__GNUC__) && __GNUC__ >= 4) || defined(__clang__) + #define CIF_API __attribute__ ((visibility ("default"))) + #else + #define CIF_API + #endif +#endif + /* --------------------------- prototypes --------------------------------- */ /* main interface */ -afni_xml_list axml_read_buf (const char * buf_in, int64_t bin_len); -afni_xml_list axml_read_file(const char * fname, int read_data); +CIF_API afni_xml_list axml_read_buf (const char * buf_in, int64_t bin_len); +CIF_API afni_xml_list axml_read_file(const char * fname, int read_data); -int axml_disp_xlist( const char *mesg, afni_xml_list * axlist, int verb); -int axml_disp_xml_t( const char *mesg, afni_xml_t * ax, int indent, int verb); +CIF_API int axml_disp_xlist( const char *mesg, afni_xml_list * axlist, int verb); +CIF_API int axml_disp_xml_t( const char *mesg, afni_xml_t * ax, int indent, int verb); /* create/free */ -afni_xml_t * new_afni_xml (const char * name); -int axml_add_attrs (afni_xml_t * ax, const char ** attr); -int axml_free_xml_t(afni_xml_t * ax); -int axml_free_xlist(afni_xml_list * axlist); - -char * axml_attr_value(afni_xml_t * ax, const char * name); -int axml_recur(int(*func)(FILE*,afni_xml_t*,int), afni_xml_t * ax); -afni_xml_t * axml_recur_find_xml(int (*func)(afni_xml_t *, int), afni_xml_t * ax, +CIF_API afni_xml_t * new_afni_xml (const char * name); +CIF_API int axml_add_attrs (afni_xml_t * ax, const char ** attr); +CIF_API int axml_free_xml_t(afni_xml_t * ax); +CIF_API int axml_free_xlist(afni_xml_list * axlist); + +CIF_API char * axml_attr_value(afni_xml_t * ax, const char * name); +CIF_API int axml_recur(int(*func)(FILE*,afni_xml_t*,int), afni_xml_t * ax); +CIF_API afni_xml_t * axml_recur_find_xml(int (*func)(afni_xml_t *, int), afni_xml_t * ax, int depth, int max_depth); /* control API */ -int axml_set_verb ( int val ); -int axml_get_verb ( void ); -int axml_set_dstore ( int val ); -int axml_get_dstore ( void ); -int axml_set_indent ( int val ); -int axml_get_indent ( void ); -int axml_set_buf_size ( int val ); -int axml_get_buf_size ( void ); -int axml_set_wstream ( FILE *fp ); -FILE * axml_get_wstream ( void ); +CIF_API int axml_set_verb ( int val ); +CIF_API int axml_get_verb ( void ); +CIF_API int axml_set_dstore ( int val ); +CIF_API int axml_get_dstore ( void ); +CIF_API int axml_set_indent ( int val ); +CIF_API int axml_get_indent ( void ); +CIF_API int axml_set_buf_size ( int val ); +CIF_API int axml_get_buf_size ( void ); +CIF_API int axml_set_wstream ( FILE *fp ); +CIF_API FILE * axml_get_wstream ( void ); #endif /* AFNI_XML_H */ diff --git a/cifti/afni_xml_io.h b/cifti/afni_xml_io.h index ae8aeb2a..ee9b6a1b 100644 --- a/cifti/afni_xml_io.h +++ b/cifti/afni_xml_io.h @@ -50,29 +50,6 @@ - convert as in SUMA_Create_Fake_CIFTI() * ----------------------------------------------------------------------*/ -#ifndef CIF_API - #if defined(_WIN32) || defined(__CYGWIN__) - #if defined(CIFTI_BUILD_SHARED) - #ifdef __GNUC__ - #define CIF_API __attribute__ ((dllexport)) - #else - #define CIF_API __declspec( dllexport ) - #endif - #elif defined(CIFTI_USE_SHARED) - #ifdef __GNUC__ - #define CIF_API __attribute__ ((dllimport)) - #else - #define CIF_API __declspec( dllimport ) - #endif - #else - #define CIF_API - #endif - #elif (defined(__GNUC__) && __GNUC__ >= 4) || defined(__clang__) - #define CIF_API __attribute__ ((visibility ("default"))) - #else - #define CIF_API - #endif -#endif /* --------------------------- structures --------------------------------- */ From 67b91b8f6e35bdc344a4708ed5ad9dc10973b66d Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 14 Aug 2026 23:12:34 -0400 Subject: [PATCH 05/11] BUG: Bound the formatted write in znzprintf, and end the va_list znzprintf() built its output with vsprintf(), which has no bound; the buffer was sized strlen(format) + 1000000 under a comment reading "overkill I hope". A single %s argument longer than a megabyte writes past the end of the allocation. vsnprintf() with the size already being computed turns that overflow into a reported truncation. The same block also returned on calloc failure without calling va_end(), leaving the va_list unterminated. This is the only unbounded formatted write in the tree; there is no sprintf() and no gets() anywhere. (cherry picked from commit 57a61a7fd296a6f99fb3bb0f9a5b1a0a92ad94b5) --- znzlib/znzlib.c | 8 ++++++-- znzlib/znzlib.h | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/znzlib/znzlib.c b/znzlib/znzlib.c index 3efe2a86..ed2c7c60 100644 --- a/znzlib/znzlib.c +++ b/znzlib/znzlib.c @@ -303,13 +303,17 @@ int znzprintf(znzFile stream, const char *format, ...) #ifdef HAVE_ZLIB if (stream->zfptr!=NULL) { size_t size; /* local to HAVE_ZLIB block */ - size = strlen(format) + 1000000; /* overkill I hope */ + int written; + size = strlen(format) + 1000000; /* still generous, but now a bound */ tmpstr = (char *)calloc(1, size); if( tmpstr == NULL ){ fprintf(stderr,"** ERROR: znzprintf failed to alloc %zu bytes\n", size); + va_end(va); return retval; } - vsprintf(tmpstr,format,va); + written = vsnprintf(tmpstr,size,format,va); + if( written < 0 || (size_t)written >= size ) + fprintf(stderr,"** ERROR: znzprintf output truncated at %zu bytes\n", size-1); retval=gzprintf(stream->zfptr,"%s",tmpstr); free(tmpstr); } else diff --git a/znzlib/znzlib.h b/znzlib/znzlib.h index c0052a40..e6514758 100644 --- a/znzlib/znzlib.h +++ b/znzlib/znzlib.h @@ -152,9 +152,16 @@ ZNZ_API int znzputc(int c, znzFile file); ZNZ_API int znzgetc(znzFile file); #if !defined(WIN32) +/* the attribute lets the caller's format be checked, which is what makes + the internal vsnprintf on it acceptable to -Wformat-nonliteral */ +#if defined(__GNUC__) || defined(__clang__) +ZNZ_API int znzprintf(znzFile stream, const char *format, ...) + __attribute__((format(printf, 2, 3))); +#else ZNZ_API int znzprintf(znzFile stream, const char *format, ...); #endif #endif +#endif /*=================*/ #ifdef __cplusplus From 16f543f9bc032f6565321558345fc9b2e1e28b42 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 18 Sep 2026 22:01:29 -0400 Subject: [PATCH 06/11] BUG: Report a znzprintf failure as a failure The preceding commit bounds the write but still returned the gzprintf count after a truncation, so a caller could not tell. It also returned 0 for an allocation failure, and the function returns 0 for a NULL stream. The printf family reports failure with a negative value; 0 is a successful empty write, so it cannot carry either meaning. All three now return -1, and a truncated result is not written at all: a partial record reported as a whole one is worse than nothing. znzprintf sits inside COMPILE_NIFTIUNUSED_CODE in both znzlib.c and znzlib.h and is absent from libznz.a in a default build, so this is a latent defect in code no released configuration compiles. (cherry picked from commit ac7ec091d1a87813822f1f40c379c292f17623eb) --- znzlib/znzlib.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/znzlib/znzlib.c b/znzlib/znzlib.c index ed2c7c60..68910f5f 100644 --- a/znzlib/znzlib.c +++ b/znzlib/znzlib.c @@ -298,7 +298,9 @@ int znzprintf(znzFile stream, const char *format, ...) int retval=0; char *tmpstr; va_list va; - if (stream==NULL) { return 0; } + /* the printf family reports failure with a negative value; 0 means an + empty write succeeded, so it cannot be used for the failures below */ + if (stream==NULL) { return -1; } va_start(va, format); #ifdef HAVE_ZLIB if (stream->zfptr!=NULL) { @@ -309,11 +311,17 @@ int znzprintf(znzFile stream, const char *format, ...) if( tmpstr == NULL ){ fprintf(stderr,"** ERROR: znzprintf failed to alloc %zu bytes\n", size); va_end(va); - return retval; + return -1; } written = vsnprintf(tmpstr,size,format,va); - if( written < 0 || (size_t)written >= size ) + if( written < 0 || (size_t)written >= size ){ + /* writing the truncated text would put a partial record in the + file and report it as a complete one, so write nothing */ fprintf(stderr,"** ERROR: znzprintf output truncated at %zu bytes\n", size-1); + free(tmpstr); + va_end(va); + return -1; + } retval=gzprintf(stream->zfptr,"%s",tmpstr); free(tmpstr); } else From 78a8c9943c9708e681f19e9590b26164608ec6a8 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 08:36:48 -0500 Subject: [PATCH 07/11] ENH: Cover the znzprintf truncation path znzprintf has no caller in this tree and is compiled only under COMPILE_NIFTIUNUSED_CODE, so nothing reached it. The test builds its own copy of znzlib.c with that definition and drives a 2 MB argument through the 1000001 byte buffer. Without the bound the write overflows the allocation, and the call reports success; the test pins the negative return, and pins the ordinary write and the resulting file content so an over-broad fix that refuses everything does not pass. (cherry picked from commit 0350b30e729cda08615b3c9ee2f0b95a810ef107) --- znzlib/CMakeLists.txt | 12 ++++++++ znzlib/znzprintf_test.c | 67 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 znzlib/znzprintf_test.c diff --git a/znzlib/CMakeLists.txt b/znzlib/CMakeLists.txt index 10239be6..5be28397 100644 --- a/znzlib/CMakeLists.txt +++ b/znzlib/CMakeLists.txt @@ -27,3 +27,15 @@ if(BUILD_SHARED_LIBS) target_compile_definitions(${NIFTI_ZNZLIB_NAME} INTERFACE ZNZ_USE_SHARED) endif() install_nifti_target(${NIFTI_ZNZLIB_NAME}) + +if(NIFTI_BUILD_TESTING AND ZLIB_FOUND AND NOT WIN32) + # znzprintf is only compiled under COMPILE_NIFTIUNUSED_CODE, and not at + # all on Windows, so the test builds its own copy of znzlib.c with that + # definition and is registered only where the function exists. + add_executable(znzprintf_test znzprintf_test.c znzlib.c) + target_compile_definitions(znzprintf_test PRIVATE COMPILE_NIFTIUNUSED_CODE) + target_include_directories(znzprintf_test PRIVATE ${CMAKE_CURRENT_LIST_DIR} ${ZLIB_INCLUDE_DIR}) + target_link_libraries(znzprintf_test PRIVATE ${NIFTI_ZLIB_LIBRARIES}) + add_test(NAME znzprintf_truncation + COMMAND $ ${CMAKE_CURRENT_BINARY_DIR}/znzprintf_test.gz) +endif() diff --git a/znzlib/znzprintf_test.c b/znzlib/znzprintf_test.c new file mode 100644 index 00000000..24b6c321 --- /dev/null +++ b/znzlib/znzprintf_test.c @@ -0,0 +1,67 @@ +/* Exercises znzprintf(), which is compiled only under + COMPILE_NIFTIUNUSED_CODE and has no caller inside this tree. */ + +#include +#include +#include + +#include "znzlib.h" + +#define BIG_LEN 2000000 + +static const char expected[] = "abc\n"; + +int main(int argc, char *argv[]) +{ + const char *path; + znzFile zf; + char *big; + int normal; + int truncated; + int status = 0; + size_t got; + char readback[64]; + + if( argc < 2 ){ fprintf(stderr,"usage: %s OUTFILE.gz\n", argv[0]); return 1; } + path = argv[1]; + + zf = znzopen(path, "wb", 1); + if( zf == NULL ){ fprintf(stderr,"** cannot open %s\n", path); return 1; } + + normal = znzprintf(zf, "%s", expected); + if( normal != (int)strlen(expected) ){ + fprintf(stderr,"** FAIL: ordinary write returned %d, expected %d\n", + normal, (int)strlen(expected)); + status = 1; + } + + big = (char *)malloc(BIG_LEN + 1); + if( big == NULL ){ fprintf(stderr,"** cannot allocate\n"); znzclose(zf); return 1; } + memset(big, 'x', BIG_LEN); + big[BIG_LEN] = '\0'; + + truncated = znzprintf(zf, "%s", big); + if( truncated >= 0 ){ + fprintf(stderr,"** FAIL: truncating write returned %d, expected a " + "negative value\n", truncated); + status = 1; + } + free(big); + + if( znzclose(zf) != 0 ){ fprintf(stderr,"** cannot close %s\n", path); return 1; } + + zf = znzopen(path, "rb", 1); + if( zf == NULL ){ fprintf(stderr,"** cannot reopen %s\n", path); return 1; } + memset(readback, 0, sizeof(readback)); + got = znzread(readback, 1, sizeof(readback) - 1, zf); + znzclose(zf); + + if( got != strlen(expected) || strcmp(readback, expected) != 0 ){ + fprintf(stderr,"** FAIL: file holds %zu bytes, expected only the %d " + "bytes of the ordinary write\n", got, (int)strlen(expected)); + status = 1; + } + + if( status == 0 ) printf("znzprintf test passed\n"); + return status; +} From 218eefa4c6943b55371c34760759093f0ad0558b Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 10:09:08 -0500 Subject: [PATCH 08/11] COMP: Declare the two znzlib functions that no header declares znzflush() and znzeof() are defined inside COMPILE_NIFTIUNUSED_CODE and declared nowhere, unlike znzgets(), znzputc() and znzgetc() beside them. Nothing compiled that block until a test in this branch did, so the omission was invisible. They are declared where the rest of the block is declared, so the warning set the project already requires stays clean when the block is built. (cherry picked from commit 8449a1f579045ff4a3792aa1193879dc4ccdd182) --- znzlib/znzlib.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/znzlib/znzlib.h b/znzlib/znzlib.h index e6514758..90d80f1e 100644 --- a/znzlib/znzlib.h +++ b/znzlib/znzlib.h @@ -147,6 +147,10 @@ ZNZ_API int znzputs(const char *str, znzFile file); #ifdef COMPILE_NIFTIUNUSED_CODE ZNZ_API char * znzgets(char* str, int size, znzFile file); +ZNZ_API int znzflush(znzFile file); + +ZNZ_API int znzeof(znzFile file); + ZNZ_API int znzputc(int c, znzFile file); ZNZ_API int znzgetc(znzFile file); From d0dc60fe1c685dce00e70334be00b5f6cbd87849 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Sat, 15 Aug 2026 00:00:00 -0400 Subject: [PATCH 09/11] BUG: Check the allocations whose result is used immediately Three allocations had their result used without a NULL check, in every case within a line or two, so an allocation failure is a null dereference rather than an error. nifti2_io.c nifti_read_n2_hdr() malloc(nifti_2_header), then nifti_convert_nim2n2hdr() fills it afni_xml.c new_afni_xml(), strdup results stored straight axml_add_attrs() into the structure Each returns an error the way the failure paths beside it do. One line further on, nifti_read_n2_hdr() released the image it had just converted with free(nim) rather than nifti_image_free(), leaking the filename strings nifti_read_ascii_image() had allocated. That is corrected in the same place. The four fslio.c allocations in the same class are left out: the idiom that file uses to report an allocation failure, FSLIOERR, calls exit(), and whether the library may end its host process is for the maintainers to decide. (cherry picked from commit 923e869c41ba3cc3a93d8861f47ab4e8dc94d849) --- cifti/afni_xml.c | 21 ++++++++++++++++++++- nifti2/nifti2_io.c | 7 ++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index 39da4433..e1e9b6e1 100644 --- a/cifti/afni_xml.c +++ b/cifti/afni_xml.c @@ -416,7 +416,14 @@ afni_xml_t * new_afni_xml(const char * name) newp->xparent = NULL; newp->xchild = NULL; - if( name ) newp->name = strdup(name); + if( name ) { + newp->name = strdup(name); + if( ! newp->name ) { + fprintf(stderr,"** new_afni_xml: failed to copy name '%s'\n", name); + free(newp); + return NULL; + } + } return newp; } @@ -461,6 +468,18 @@ int axml_add_attrs(afni_xml_t * ax, const char ** attr) for(c = 0, aind = 0; attr[c]; c += 2, aind++) { ax->attrs.name[aind] = strdup(strip_whitespace(attr[c],0)); ax->attrs.value[aind] = strdup(strip_whitespace(attr[c+1],0)); + if( ! ax->attrs.name[aind] || ! ax->attrs.value[aind] ) { + fprintf(stderr,"** NAX: failed to copy attribute %d\n", aind); + for( c = 0; c <= aind; c++ ) { + free(ax->attrs.name[c]); + free(ax->attrs.value[c]); + } + free(ax->attrs.name); + free(ax->attrs.value); + ax->attrs.name = ax->attrs.value = NULL; + ax->attrs.length = 0; + return 1; + } } return 0; diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index c0111577..fae0f477 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -5501,8 +5501,13 @@ nifti_2_header * nifti_read_n2_hdr(const char * hname, int * swapped, if( ! nim ) return NULL; hptr = (nifti_2_header *)malloc(sizeof(nifti_2_header)); + if( ! hptr ){ + fprintf(stderr,"** nifti_read_n2_hdr: failed to alloc nifti_2_header\n"); + nifti_image_free(nim); + return NULL; + } rv = nifti_convert_nim2n2hdr(nim, hptr); - free(nim); + nifti_image_free(nim); /* free(nim) leaked nim's filename strings */ if( rv ) { free(hptr); return NULL; } return hptr; From e396556544dd635ce6df85736696c8a4a21fd703 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 18 Sep 2026 21:52:16 -0400 Subject: [PATCH 10/11] BUG: Report the attribute failure instead of discarding it axml_add_attrs() returns 1 when it cannot copy an attribute, but make_afni_xml() ignored the result, so the element was returned with a partial attribute list and nothing said so. Every failure the function reports is an allocation failure, so there is no case where continuing is right. Propagate it: epush() already treats a NULL from make_afni_xml() by entering a skip block, which is what the allocation-failure paths beside it do. axml_free_xml_t() releases what was built. It walks the attributes with attrs.length, which axml_add_attrs() lowers to the pair it failed on, so the half of that pair that was copied is freed and the entries past it are never read. That matters because the two arrays come from malloc(), not calloc(), so those entries hold uninitialized pointers. (cherry picked from commit 0685f7fca7a13fcbb872ea7cd5939a7041ca6f11) --- cifti/afni_xml.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index e1e9b6e1..25e0d6ee 100644 --- a/cifti/afni_xml.c +++ b/cifti/afni_xml.c @@ -861,7 +861,11 @@ static afni_xml_t * make_afni_xml(const char * ename, const char ** attr) newp = new_afni_xml(ename); if( ! newp ) return NULL; - axml_add_attrs(newp, attr); + /* a failure here is an allocation failure; epush() skips on NULL */ + if( axml_add_attrs(newp, attr) ) { + axml_free_xml_t(newp); + return NULL; + } return newp; } From 743ace96ac708e2f111625245972b7c91906e531 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 10:20:02 -0500 Subject: [PATCH 11/11] ENH: Cover the ASCII path through the NIFTI-2 header reader nifti_read_n2_hdr() builds a nifti_image to convert an ASCII header and then released it with free(), which leaves its filename strings behind. No test reached that path, so the suite reported 362 of 362 either way. The fixture is the one the ASCII attribute test already uses, read through -disp_hdr2 rather than -disp_nim so the NIFTI-2 reader is the one exercised. The leak is visible to the memcheck and sanitizer legs; an ordinary build stays green with or without the fix. (cherry picked from commit c6410f52e112af11b624c651fb2969a494b293c4) --- nifti2/CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/nifti2/CMakeLists.txt b/nifti2/CMakeLists.txt index 6630fc79..957c6f00 100644 --- a/nifti2/CMakeLists.txt +++ b/nifti2/CMakeLists.txt @@ -247,6 +247,10 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) add_test( NAME ${TEST_PREFIX}_nii_cmp_selftest COMMAND sh ${NIFTI_TEST_SCRIPT_DIR}/nii_cmp_selftest.sh ${NIFTI_TEST_SCRIPT_DIR} ) endif() + # Reading an ASCII header through the NIFTI-2 reader releases the image + # it built; the leak is what the memcheck and sanitizer legs observe. + add_test( NAME ${TEST_PREFIX}_tool_disp_hdr2_ascii COMMAND $ -disp_hdr2 -infiles ${PROJECT_SOURCE_DIR}/niftilib/testdata/dup_attr.nia ) + unset(TEST_SUFFIX) unset(TEST_PREFIX) unset(TOOL_NAME)