Skip to content

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

Open
hjmjohnson wants to merge 2 commits into
masterfrom
pr/fix-size-t-error-returns
Open

hjmjohnson wants to merge 2 commits into
masterfrom
pr/fix-size-t-error-returns

Conversation

@hjmjohnson

Copy link
Copy Markdown
Member

Re-submission of #43, reverted from master on 2026-09-24 so it can be
reviewed before merging. Content is unchanged from the original.

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 image is rejected rather than accepted

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

gdevenyi and others added 2 commits September 24, 2026 07:10
znzread(), znzwrite() and nifti_read_buffer() all return size_t and
all returned -1 to report an error.  In a size_t that value is
SIZE_MAX, larger than any length a caller can have asked for, so a
caller that tests the result with '<' reads the error as a complete
transfer:

    ii = nifti_read_buffer(fp, nim->data, ntot, nim);
    if( ii < ntot ){ ... }          /* SIZE_MAX < ntot is false */

The visible effect is that a truncated image loads as if it were
whole: nifti_image_read() prints its short-read warning, ignores the
failure and returns an image whose tail is uninitialized heap.
FslReadVolumes() has the same problem one level up, dividing the
returned byte count by the volume size to report SIZE_MAX/volbytes
volumes read.

These now return 0, which every one of these callers already treats
as failure and which is the only value a size_t function has for
"nothing was transferred".  The published return types in znzlib.h
and nifti1_io.h are unchanged.

The nifti2 equivalents return int64_t, where -1 is representable,
and are left alone.

(cherry picked from commit b344fe4)
Writes a 31x31x31 float image, copies it 1000 bytes short, and asserts
nifti_image_read() returns NULL for the copy and a populated image for
the original, so a blanket refusal cannot pass.  Without the fix the
truncated file comes back accepted with nvox=29791 and an
uninitialized tail.

(cherry picked from commit 9b31abf)
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