Skip to content

BUG: Stop returning -1 from functions that return size_t - #111

Merged
hjmjohnson merged 2 commits into
masterfrom
pr/fix-size-t-error-returns
Nov 14, 2026
Merged

hjmjohnson merged 2 commits into
masterfrom
pr/fix-size-t-error-returns

Conversation

@hjmjohnson

@hjmjohnson hjmjohnson commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

znzread(), znzwrite(), and nifti_read_buffer() return size_t but reported errors as -1 (SIZE_MAX), which every < caller read as a complete transfer. They now return 0 on error.
Effect for ITK and other readers: a truncated or corrupt .nii/.nii.gz now makes nifti_image_read() return NULL instead of loading an image with an uninitialized tail.

Re-submission of #43, reverted from master on 2026-09-24 so it can be reviewed before merging. Review round 1 added the .nii.gz test cases and the return-contract notes.

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

Commits
  • BUG: Stop returning -1 from functions that return size_t
  • ENH: Test that a truncated or corrupt image is rejected
Test coverage

nifti_short_read_test writes a reference image and three damaged copies:

  • a .nii 1000 bytes short;
  • a .nii.gz 1000 bytes short, where gzread returns a short count;
  • a .nii.gz whose deflate stream hits a reserved block type mid-image, where gzread returns -1.

Reverting only the nifti1_io.c change accepts all three; reverting only the znzlib.c change accepts the corrupt .nii.gz.

The znzwrite() error branch is not exercised; gzwrite() reports errors as 0, not a negative count.

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-size-t-error-returns branch from d9f5d2d to 987acbd Compare October 6, 2026 14:35
@hjmjohnson
hjmjohnson force-pushed the pr/fix-size-t-error-returns branch from 987acbd to 19d840a Compare November 6, 2026 16:54
Comment thread niftilib/nifti1_io.c Resolved

@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

Great bug fix and thorough test (nifti_short_read_test.c covering truncated .nii, truncated .nii.gz, mid-stream corrupt DEFLATE block BTYPE=11, and intact .nii control)!

  • Previously, znzread casted gzread's -1 error return to (size_t)-1 (SIZE_MAX), and nifti1's nifti_read_buffer returned (size_t)-1 on short reads while nifti_image_load_bricks tested nifti_read_buffer(...) < bsize — silently accepting truncated .nii/.nii.gz and corrupt .nii.gz streams with uninitialized voxel tails in NIFTI-1 (and on 32-bit NIFTI-2 where (int64_t)(uint32_t)-1 == +4294967295!).
  • Returning 0 matches fread/fwrite semantics cleanly.

Left two inline notes — including the exact answer to @seanm's question on nifti_read_buffer (NIO_API public symbol in niftilib/nifti1_io.h:373) and a minor header-order portability tweak in nifti_short_read_test.c for vendored zlib builds.

Comment thread niftilib/nifti1_io.c Resolved
Comment thread niftilib/nifti_short_read_test.c Resolved
@hjmjohnson

Copy link
Copy Markdown
Member Author

Force-push 19d840a..62c67c5 is a plain rebase onto master (33 commits); both commits are unchanged (range-diff =).

@hjmjohnson
hjmjohnson force-pushed the pr/fix-size-t-error-returns branch 2 times, most recently from 62c67c5 to 45e905a Compare November 14, 2026 15:37
gdevenyi and others added 2 commits November 14, 2026 09:39
znzread(), znzwrite() and nifti_read_buffer() return size_t and
returned -1 on error. As a size_t that is SIZE_MAX, so a caller that
tests "ii < ntot" reads the error as a complete transfer, and
nifti_image_read() returned a truncated image with an uninitialized
tail. FslReadVolumes() divided the same value into a volume count.

They now return 0 on error, which every caller already treats as
failure; the contract is stated beside nifti_read_buffer() and in
znzlib.h. The published return types are unchanged. The nifti2
equivalents return int64_t, where -1 is representable, and are left
alone.
Writes a 31x31x31 float image and three damaged copies, and asserts
nifti_image_read() returns NULL for each copy and an image for the
original, so a blanket refusal cannot pass: a .nii 1000 bytes short
(the fread path), a .nii.gz 1000 bytes short (gzread's short count),
and a .nii.gz whose deflate stream ends in a reserved block type
(gzread returns -1 inside the image data).

Without the fix all three are accepted with an uninitialized tail.
Reverting only the nifti1_io.c change accepts all three; reverting only
the znzlib.c change accepts the corrupt .nii.gz. The znzwrite() error
branch is not exercised: gzwrite() does not return a negative count.
@hjmjohnson
hjmjohnson force-pushed the pr/fix-size-t-error-returns branch from 45e905a to e0cec9f Compare November 14, 2026 15:39
@hjmjohnson
hjmjohnson merged commit 060eb74 into master Nov 14, 2026
8 checks passed
@hjmjohnson
hjmjohnson deleted the pr/fix-size-t-error-returns branch November 14, 2026 15:41
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