Skip to content

BUG: Bound the formatted write in znzprintf, and end the va_list on failure - #110

Open
hjmjohnson wants to merge 4 commits into
masterfrom
pr/fix-vsprintf-bounded
Open

hjmjohnson wants to merge 4 commits into
masterfrom
pr/fix-vsprintf-bounded

Conversation

@hjmjohnson

@hjmjohnson hjmjohnson commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Re-submission of #41, reverted from master on 2026-09-24 so it can be
reviewed before merging. Review fixes since then: the gz branch now writes
with gzwrite(), since gzprintf() silently wrote nothing for output of
8192 bytes or more, and the test covers that path and the NULL stream.

Base: master. Independent: nothing has to land before it.

Commits
  • BUG: Bound the formatted write in znzprintf, and end the va_list
    (also adds __attribute__((format(printf, 2, 3))) to the znzprintf
    prototype on GCC and Clang, through a ZNZ_PRINTF_ATTR macro)
  • BUG: Report a znzprintf failure as a failure (NULL stream, allocation
    failure, truncation, and a short gzwrite() all return -1)
  • COMP: Declare the two znzlib functions that no header declares
  • ENH: Cover the znzprintf failure paths and the gz write
Changes from the original #41
  • The gz branch passed the formatted text back through gzprintf("%s").
    zlib formats into its own 8192 byte buffer and returns 0 without writing
    anything for longer output, so every gz write from 8192 bytes up to the
    1000000 byte bound failed silently. It now uses gzwrite() and returns -1
    on a short count.
  • The COMP commit now precedes the test, so every commit builds with
    -Werror=missing-declarations.
  • The test adds a 100000 byte gz write and a NULL stream case to the 2 MB
    truncation case. Each fails when its fix is reverted.
  • The znzprintf prototype is written once, and the source comments are
    one line each.

Ordering for all the re-submitted work is tracked in #84.

@hjmjohnson
hjmjohnson marked this pull request as ready for review September 24, 2026 12:17
@hjmjohnson
hjmjohnson force-pushed the pr/fix-vsprintf-bounded branch from 68a4bf1 to bf4f0f6 Compare October 6, 2026 14:35
gdevenyi and others added 4 commits November 6, 2026 10:52
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.
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.
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.
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.
@hjmjohnson
hjmjohnson force-pushed the pr/fix-vsprintf-bounded branch from bf4f0f6 to d64146e Compare November 6, 2026 16:55
@hjmjohnson

Copy link
Copy Markdown
Member Author

@greptileai review

@seanm

seanm commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator

Wait, indeed znzprintf is unused, so why shouldn't we just remove it entirely?

@hjmjohnson
hjmjohnson requested a review from gdevenyi November 11, 2026 18:43

@gdevenyi gdevenyi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by Google Carbon

Verdict: Approve (with non-blocking suggestions; note @seanm's open question on whether dead znzprintf should be removed instead)

Verified the implementation and znzprintf_test:

  • Calling va_end(va) on every failure return, bounding formatting with vsnprintf, routing output through gzwrite (bypassing zlib gzprintf's silent 8192-byte drop), returning -1 on NULL/truncation/short-write, and adding ZNZ_PRINTF_ATTR (__attribute__((format(printf, 2, 3)))) so -Wformat-nonliteral stays clean are all solid improvements.
  • Also adding missing znzflush and znzeof prototypes to znzlib/znzlib.h fixes real undeclared-export gaps (libznz.so already exports znzflush and znzeof under HAVE_ZLIB).

Left three non-blocking inline suggestions (dynamic va_copy sizing instead of a fixed 1 MB calloc on every call, distinguishing written < 0 vs truncation in stderr, and ${NIFTI_PACKAGE_PREFIX} in znzlib/CMakeLists.txt). Note @seanm's architectural question on the PR thread regarding whether znzprintf (COMPILE_NIFTIUNUSED_CODE) can simply be deleted if no downstream consumer defines COMPILE_NIFTIUNUSED_CODE.

Comment thread znzlib/znzlib.c
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 */

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Google Carbon] Non-blocking suggestion (if znzprintf is kept rather than deleted per @seanm's question): since this codebase requires C11, va_copy lets you format into a small stack buffer (e.g. char stackbuf[512]) first and only malloc((size_t)written + 1) on the rare call that exceeds 512 bytes — avoiding both a 1 MB heap calloc on every tiny znzprintf call and the arbitrary 1 MB truncation ceiling.

Comment thread znzlib/znzlib.c
written = vsnprintf(tmpstr,size,format,va);
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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Google Carbon] Minor diagnostic nit: when written < 0 (vsnprintf encoding/EOVERFLOW failure rather than (size_t)written >= size), this message still reports "znzprintf output truncated at %zu bytes".

Comment thread znzlib/CMakeLists.txt

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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Google Carbon] Minor CMake consistency nit: prefix the test executable and test name with ${NIFTI_PACKAGE_PREFIX} (${NIFTI_PACKAGE_PREFIX}znzprintf_test / ${NIFTI_PACKAGE_PREFIX}znzprintf) to match niftilib/CMakeLists.txt and nifti2/CMakeLists.txt when NIFTI_clib is vendored (e.g. ITK).

@gdevenyi

Copy link
Copy Markdown
Collaborator

Wait, indeed znzprintf is unused, so why shouldn't we just remove it entirely?

I will do a search to see if anything anywhere uses it

@gdevenyi

Copy link
Copy Markdown
Collaborator

Following up on @seanm's question — a search across GitHub, distro/package recipes, upstream neuroimaging repos, and the web supports removing znzprintf entirely rather than maintaining and testing it here.

Findings

  1. Already absent from default libznz builds and ABI since v3.0.0 (2020)

    • Commit 19e1108 (Oct 2019, released in v3.0.0 and v3.0.1) gated znzprintf behind #ifdef COMPILE_NIFTIUNUSED_CODE (in addition to #if !defined(WIN32)).
    • Searching all of GitHub for COMPILE_NIFTIUNUSED_CODE returns 44 matches, and 44/44 are just vendored copies of znzlib.h/znzlib.c. 0 repositories define COMPILE_NIFTIUNUSED_CODE in any build script or source file.
    • Debian (salsa.debian.org/med-team/nifticlib), Fedora (nifticlib.spec), vcpkg, conda-forge, and ITK (Modules/ThirdParty/NIFTI) all build without COMPILE_NIFTIUNUSED_CODE, so no shipped libznz binary exports znzprintf today. Deleting it is not an API or ABI break.
  2. All ~526 GitHub code search hits for znzprintf accounted for — 0 active nifti_clib callers

    • ~415 hits: Vendored copies of znzlib.{c,h} in downstream trees (AFNI, NiftyReg, RNifti, ITK, libminc, Horos/OsiriX, MIRTK, LAYNII, vistasoft, etc.) with no callers.
    • ~99 hits: Legacy copies of 2004 FSL test files (znzlib/testprog.c, niftiio/mjtest.c) removed from nifti_clib in 2005 (7ccf813). Upstream FSL (git.fmrib.ox.ac.uk/fsl/znzlib, where znzlib originated) has since rewritten znzlib in C++ and dropped znzprintf completely.
    • 12 remaining hits (only 4 distinct projects, none using nifti_clib's znzprintf):
      • FreeSurfer (freesurfer/freesurfer utils/tags.cpp + forks): Calls znzprintf inside znzTAGwriteCommandLine(), which itself has zero callers in FreeSurfer and compiles against FreeSurfer's own vendored 2004 packages/nifti/znzlib.c.
      • Migrate-N (pbeerli/migrate-5-git + mirrors): Non-NIfTI population-genetics tool that vendors its own standalone 2004 znzlib.c.
      • Legacy PyNIfTI SWIG wrapper (badbytes/pymeg extra/nifti/clib.py): Auto-generated ~2009 SWIG binding (superseded by nibabel), unused.
      • Lazarus/FreePascal header binding (sechshelme/Lazarus-GTK4-GStreamer-and-Modern-Bindings nifti_3.0.1/nifti/znzlib.inc): Blindly translated external libznz declarations from znzlib.h without #ifdef COMPILE_NIFTIUNUSED_CODE (already unresolved against libznz 3.0.1 — removing the dead prototype avoids broken auto-generated FFI bindings like this).

Recommendation

Remove znzprintf from znzlib/znzlib.h and znzlib/znzlib.c instead of landing the bounded-vsnprintf / 1 MB calloc / gzwrite workaround and private test harness in this PR. (Note that commit 1adac614 declaring znzflush and znzeof is also inside #ifdef COMPILE_NIFTIUNUSED_CODE and was only needed to compile znzprintf_test under -Werror=missing-declarations; we could optionally retire the whole COMPILE_NIFTIUNUSED_CODE block as well, or just delete znzprintf here.)

@seanm seanm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK so then let's remove znzprintf. @gdevenyi will you update this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants