From 3276516b963d71822dd78fdbf434c0f39b6f2f13 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 14 Aug 2026 23:12:34 -0400 Subject: [PATCH 1/4] 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. The znzlib.h prototype gains __attribute__((format(printf, 2, 3))) on GCC and Clang, through a ZNZ_PRINTF_ATTR macro, so callers' format strings are checked and the vsnprintf() on the caller's format passes -Wformat-nonliteral. Callers may see new -Wformat diagnostics; nothing else changes for them. This is the only unbounded formatted write in the tree; there is no sprintf() and no gets() anywhere. --- znzlib/znzlib.c | 8 ++++++-- znzlib/znzlib.h | 8 +++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/znzlib/znzlib.c b/znzlib/znzlib.c index 52b5665d..a09ca8f1 100644 --- a/znzlib/znzlib.c +++ b/znzlib/znzlib.c @@ -302,13 +302,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; /* znzprintf_test.c mirrors this 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 ff031687..829d71d7 100644 --- a/znzlib/znzlib.h +++ b/znzlib/znzlib.h @@ -150,7 +150,13 @@ ZNZ_API int znzputc(int c, znzFile file); ZNZ_API int znzgetc(znzFile file); #if !defined(WIN32) -ZNZ_API int znzprintf(znzFile stream, const char *format, ...); +/* checks callers' formats, so the inner vsnprintf passes -Wformat-nonliteral */ +#if defined(__GNUC__) || defined(__clang__) +#define ZNZ_PRINTF_ATTR __attribute__((format(printf, 2, 3))) +#else +#define ZNZ_PRINTF_ATTR +#endif +ZNZ_API int znzprintf(znzFile stream, const char *format, ...) ZNZ_PRINTF_ATTR; #endif #endif From b07e851d3c61e51abe2168e734cada06d3b488aa Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 18 Sep 2026 22:01:29 -0400 Subject: [PATCH 2/4] 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. The gz branch also passed the formatted text back through gzprintf("%s"), which formats into zlib's own buffer (8192 bytes by default). For any longer output zlib writes nothing and returns 0, so every gz write from 8192 bytes up to the 1000000 byte bound failed silently. The text is already formatted, so it is written with gzwrite(), and a short count returns -1. 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. --- znzlib/znzlib.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/znzlib/znzlib.c b/znzlib/znzlib.c index a09ca8f1..6802a967 100644 --- a/znzlib/znzlib.c +++ b/znzlib/znzlib.c @@ -297,7 +297,8 @@ int znzprintf(znzFile stream, const char *format, ...) int retval=0; char *tmpstr; va_list va; - if (stream==NULL) { return 0; } + /* failures return -1 as printf does; 0 is a successful empty write */ + if (stream==NULL) { return -1; } va_start(va, format); #ifdef HAVE_ZLIB if (stream->zfptr!=NULL) { @@ -308,12 +309,19 @@ 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 ){ + /* a partial record reported as complete is worse than none */ fprintf(stderr,"** ERROR: znzprintf output truncated at %zu bytes\n", size-1); - retval=gzprintf(stream->zfptr,"%s",tmpstr); + free(tmpstr); + va_end(va); + return -1; + } + /* gzprintf writes nothing and returns 0 past its 8192 byte buffer */ + retval=gzwrite(stream->zfptr,tmpstr,(unsigned)written); + if( retval != written ) retval = -1; free(tmpstr); } else #endif From 1adac6146c1502e6d19fe1c194bb42344d699328 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 10:09:08 -0500 Subject: [PATCH 3/4] 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. No build compiles that block today, so the omission was invisible; the znzprintf test that follows compiles it, and -Werror=missing-declarations would then reject both definitions. 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. --- znzlib/znzlib.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/znzlib/znzlib.h b/znzlib/znzlib.h index 829d71d7..c4b5b53f 100644 --- a/znzlib/znzlib.h +++ b/znzlib/znzlib.h @@ -145,6 +145,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 d64146eb2eaebfa3d6e561b3bb82978c3a188644 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 08:36:48 -0500 Subject: [PATCH 4/4] ENH: Cover the znzprintf failure paths and the gz write 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 checks: - a NULL stream returns a negative value; - a 100000 byte gz write, past zlib's 8192 byte gzprintf buffer, returns its length and lands in the file; - a 2 MB argument, past the 1000000 byte bound, returns a negative value and writes nothing. Without the bound the 2 MB write overflows the allocation; with gzprintf in place of gzwrite the 100000 byte write returns 0 and writes nothing. The file content is pinned too, so an over-broad fix that refuses everything does not pass. ZNZPRINTF_BOUND in the test mirrors the bound in znzlib.c, and a comment there points back here. --- znzlib/CMakeLists.txt | 10 ++++ znzlib/znzprintf_test.c | 108 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 znzlib/znzprintf_test.c diff --git a/znzlib/CMakeLists.txt b/znzlib/CMakeLists.txt index 10239be6..a703ad94 100644 --- a/znzlib/CMakeLists.txt +++ b/znzlib/CMakeLists.txt @@ -27,3 +27,13 @@ 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 exists only under COMPILE_NIFTIUNUSED_CODE and off Windows, so build a private copy. + 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 + 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..23373872 --- /dev/null +++ b/znzlib/znzprintf_test.c @@ -0,0 +1,108 @@ +/* Exercises znzprintf(), which is compiled only under + COMPILE_NIFTIUNUSED_CODE and has no caller inside this tree. */ + +#include +#include +#include + +#include "znzlib.h" + +/* must match the strlen(format) + 1000000 buffer in znzprintf, znzlib.c */ +#define ZNZPRINTF_BOUND 1000000 +/* past zlib's 8192 byte gzprintf buffer, inside ZNZPRINTF_BOUND */ +#define MID_LEN 100000 +#define BIG_LEN (2 * ZNZPRINTF_BOUND) + +static const char expected[] = "abc\n"; + +static char *filled(size_t len) +{ + char *s = (char *)malloc(len + 1); + if( s != NULL ){ memset(s, 'x', len); s[len] = '\0'; } + return s; +} + +int main(int argc, char *argv[]) +{ + const char *path; + znzFile zf; + char *mid; + char *big; + char *readback; + int ret; + int status = 0; + size_t want = sizeof(expected) - 1 + MID_LEN; + size_t got; + + if( argc < 2 ){ fprintf(stderr,"usage: %s OUTFILE.gz\n", argv[0]); return 1; } + path = argv[1]; + + ret = znzprintf(NULL, "x"); + if( ret >= 0 ){ + fprintf(stderr,"** FAIL: NULL stream returned %d, expected a negative value\n", ret); + status = 1; + } + + mid = filled(MID_LEN); + big = filled(BIG_LEN); + readback = (char *)calloc(1, want + 2); + if( mid == NULL || big == NULL || readback == NULL ){ + fprintf(stderr,"** cannot allocate\n"); + free(mid); free(big); free(readback); + return 1; + } + + zf = znzopen(path, "wb", 1); + if( zf == NULL ){ + fprintf(stderr,"** cannot open %s\n", path); + free(mid); free(big); free(readback); + return 1; + } + + ret = znzprintf(zf, "%s", expected); + if( ret != (int)strlen(expected) ){ + fprintf(stderr,"** FAIL: ordinary write returned %d, expected %d\n", + ret, (int)strlen(expected)); + status = 1; + } + + ret = znzprintf(zf, "%s", mid); + if( ret != MID_LEN ){ + fprintf(stderr,"** FAIL: %d byte write returned %d, expected %d\n", + MID_LEN, ret, MID_LEN); + status = 1; + } + + ret = znzprintf(zf, "%s", big); + if( ret >= 0 ){ + fprintf(stderr,"** FAIL: truncating write returned %d, expected a " + "negative value\n", ret); + status = 1; + } + + if( znzclose(zf) != 0 ){ + fprintf(stderr,"** cannot close %s\n", path); + free(mid); free(big); free(readback); + return 1; + } + + zf = znzopen(path, "rb", 1); + if( zf == NULL ){ + fprintf(stderr,"** cannot reopen %s\n", path); + free(mid); free(big); free(readback); + return 1; + } + got = znzread(readback, 1, want + 1, zf); + znzclose(zf); + + if( got != want || strncmp(readback, expected, strlen(expected)) != 0 + || strcmp(readback + strlen(expected), mid) != 0 ){ + fprintf(stderr,"** FAIL: file holds %zu bytes, expected only the %zu " + "bytes of the two complete writes\n", got, want); + status = 1; + } + + free(mid); free(big); free(readback); + if( status == 0 ) printf("znzprintf test passed\n"); + return status; +}