Repository navigation
BUG: Stop returning -1 from functions that return size_t - #111
Merged
Merged
Conversation
hjmjohnson
marked this pull request as ready for review
September 24, 2026 12:17
hjmjohnson
force-pushed
the
pr/fix-size-t-error-returns
branch
from
October 6, 2026 14:35
d9f5d2d to
987acbd
Compare
hjmjohnson
force-pushed
the
pr/fix-size-t-error-returns
branch
from
November 6, 2026 16:54
987acbd to
19d840a
Compare
seanm
reviewed
Nov 9, 2026
gdevenyi
approved these changes
Nov 12, 2026
gdevenyi
left a comment
Collaborator
There was a problem hiding this comment.
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,
znzreadcastedgzread's-1error return to(size_t)-1(SIZE_MAX), andnifti1'snifti_read_bufferreturned(size_t)-1on short reads whilenifti_image_load_brickstestednifti_read_buffer(...) < bsize— silently accepting truncated.nii/.nii.gzand corrupt.nii.gzstreams with uninitialized voxel tails in NIFTI-1 (and on 32-bit NIFTI-2 where(int64_t)(uint32_t)-1 == +4294967295!). - Returning
0matchesfread/fwritesemantics 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.
Member
Author
hjmjohnson
force-pushed
the
pr/fix-size-t-error-returns
branch
2 times, most recently
from
November 14, 2026 15:37
62c67c5 to
45e905a
Compare
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
force-pushed
the
pr/fix-size-t-error-returns
branch
from
November 14, 2026 15:39
45e905a to
e0cec9f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
znzread(),znzwrite(), andnifti_read_buffer()returnsize_tbut 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.gznow makesnifti_image_read()return NULL instead of loading an image with an uninitialized tail.Re-submission of #43, reverted from
masteron 2026-09-24 so it can be reviewed before merging. Review round 1 added the.nii.gztest cases and the return-contract notes.Base:
master. Independent: nothing has to land before it.Commits
Test coverage
nifti_short_read_testwrites a reference image and three damaged copies:.nii1000 bytes short;.nii.gz1000 bytes short, wheregzreadreturns a short count;.nii.gzwhose deflate stream hits a reserved block type mid-image, wheregzreadreturns -1.Reverting only the
nifti1_io.cchange accepts all three; reverting only theznzlib.cchange 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.