Skip to content

ENH: Make the 64-to-32 bit truncations explicit - #131

Draft
hjmjohnson wants to merge 1 commit into
stack/fix-cifti-null-streamfrom
stack/pr-fix-shorten-64-to-32
Draft

hjmjohnson wants to merge 1 commit into
stack/fix-cifti-null-streamfrom
stack/pr-fix-shorten-64-to-32

Conversation

@hjmjohnson

Copy link
Copy Markdown
Member

Re-submission of #52, reverted from master on 2026-09-24. Content is
unchanged from the original.

Position 9 of 11 in the deep stack. Base: stack/pr-fix-calloc-transposed-args.

Based on the pull request above it in the stack, so the diff shown here is
this change alone. Merge the stack bottom-up.

Stack order

# branch base
1 stack/test-regression-coverage master
2 stack/pr-fix-alloc-null-checks stack/test-regression-coverage
3 stack/fix-axml-skip-depth stack/pr-fix-alloc-null-checks
4 stack/pr-fix-analyzer-leaks stack/fix-axml-skip-depth
5 stack/pr-fix-sign-conversion stack/pr-fix-analyzer-leaks
6 stack/fix-fslio-64bit-arithmetic stack/pr-fix-sign-conversion
7 stack/fix-cifti-null-stream stack/fix-fslio-64bit-arithmetic
8 stack/pr-fix-calloc-transposed-args stack/fix-cifti-null-stream
9 stack/pr-fix-shorten-64-to-32 <- this PR stack/pr-fix-calloc-transposed-args
10 stack/fix-image-read-complex-check stack/pr-fix-shorten-64-to-32
11 stack/pr-fix-xml-read-errors stack/fix-image-read-complex-check

The order is the order these changes sat on master before the revert, so
it builds and tests at every step.

Commits introduced by this PR
  • BUG: Fix 64-to-32 bit truncations, including a real overflow in fslio

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:18
Base automatically changed from stack/pr-fix-calloc-transposed-args to stack/fix-cifti-null-stream October 6, 2026 14:27
@hjmjohnson
hjmjohnson marked this pull request as draft October 6, 2026 15:02
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-shorten-64-to-32 branch from 3095b44 to b133669 Compare November 2, 2026 15:32
@hjmjohnson
hjmjohnson force-pushed the stack/fix-cifti-null-stream branch 2 times, most recently from 2d90824 to 89d1d56 Compare November 2, 2026 20:37
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-shorten-64-to-32 branch 3 times, most recently from 51ca871 to cead6d5 Compare November 6, 2026 14:59
@hjmjohnson
hjmjohnson force-pushed the stack/fix-cifti-null-stream branch from c5b60c5 to 4ed6127 Compare November 6, 2026 14:59

@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: Request Changes (Draft; 3 serious runtime regressions introduced while silencing -Wshorten-64-to-32 + out-of-date base vs #129)

Widening nifti_read_extensions / nifti_check_extension / rci_alloc_mem in nifti2/nifti2_io.c from int to int64_t is the right architectural fix, but several explicit (int) casts added to silence -Wshorten-64-to-32 introduce severe bugs on >= 2 GiB files and zero-sized headers:

  1. [CRITICAL — Breaks .nii.gz seeks >= 2 GiB] FslSeekVolume (fsliolib/fslio.c:1178):
    return (int)znzseek(fslio->fileptr, offset, SEEK_SET); — When reading a compressed .nii.gz file, znzseek calls zlib's gzseek, which returns the new 64-bit uncompressed offset on success (0 on fseek uncompressed, offset on gzseek compressed, -1 on error)! Casting znzseek(...) to (int) wraps any .nii.gz seek into [2 GiB, 4 GiB) to a negative int (and wraps offset == 4 GiB - 1 to -1!), making callers treat a valid seek as a fatal error, while wrapping -1 or >= 4 GiB unpredictably! Since FslSeekVolume returns 0 on success / < 0 on error (or check callers), return znzseek(fslio->fileptr, offset, SEEK_SET) < 0 ? -1 : 0;.
  2. [HIGH — Division by zero] FslReadVolumes (fsliolib/fslio.c:850-852): volbytes = FslGetVolSize(fslio) * (size_t)fslio->niftiptr->nbyper; followed immediately by retval /= volbytes; — if any nx/ny/nz == 0 or nbyper == 0 on a malformed header, volbytes == 0 crashes with SIGFPE! Guard if (volbytes == 0) return 0;.
  3. [HIGH — Corrupts >= 2 GiB .nia ASCII reads] nifti_image_read (nifti2/nifti2_io.c:5990): Passing (int)filesize to nifti_read_ascii_image(..., int flen, ...) wraps [2 GiB, 4 GiB) negative (silently dropping extensions in nifti2_io.c:6192) and wraps >= 4 GiB to a small positive int! Change internal static nifti_read_ascii_image's flen parameter to int64_t in nifti2_io.c (where nifti_get_filesize already returns int64_t!) instead of truncating filesize to int.
  4. Unchecked (int) casts on public size_t / int64_t inputs (FslSetDimensionality fslio.c:1263-1264 and axio_num_tokens cifti/afni_xml_io.c:139), plus rebase onto #129's updated 4ed61275 (currently reverts #129's force-pushed fixes) and unbundle #121.

Comment thread fsliolib/fslio.c Resolved
Comment thread fsliolib/fslio.c Resolved
Comment thread fsliolib/fslio.c Resolved
Comment thread nifti2/nifti2_io.c
}
else if ( rv == 1 ) { /* process special file type */
nim = nifti_read_ascii_image( fp, hfile, filesize, read_data );
nim = nifti_read_ascii_image( fp, hfile, (int)filesize, read_data );

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] Don't truncate int64_t filesize to (int)filesize in nifti2! nifti_read_ascii_image is a file-local static function in nifti2/nifti2_io.c, and NIFTI-2 explicitly supports >2 GiB datasets: casting (int)filesize makes flen negative for [2 GiB, 4 GiB) .nia files (breaking extension detection) and wraps filesize >= 4 GiB modulo 2^32! Widen nifti_read_ascii_image's int flen parameter (and remain) to int64_t just like nifti_read_extensions right below.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

nifti_read_ascii_image is NI2_API in nifti2_io.h, not static, so widening flen changes the installed API. The cast changes no value from the implicit conversion before it. A >2GB .nia needs an internal int64 path, which I would do in a separate change.

Comment thread cifti/afni_xml_io.c Resolved
@hjmjohnson

Copy link
Copy Markdown
Member Author

Restacked on the updated #129. This PR's own fixes, with fslio_seek_test: deede18. None of the three issues were introduced here, since each cast changed no value. Details are inline.

31 -Wshorten-64-to-32 warnings.  Most were benign; one was not.

FslSeekVolume() declared `int offset` and assigned it a byte position
into the image file:

    offset = fslio->niftiptr->iname_offset
             + vols * FslGetVolSize(fslio) * fslio->niftiptr->nbyper;
    return znzseek(fslio->fileptr, offset, SEEK_SET);

Seek to any volume past 2GB and the multiplication overflows, so znzseek
-- which takes a 64-bit znz_off_t -- receives a wrong and quite possibly
negative position.  offset is a local, so widening it to znz_off_t and
widening the operands changes no interface.  FslReadVolumes' volbytes had
the same shape and is now size_t.

The rest fall into two groups.  Internal helpers were widened to carry
the value they were already being handed: nifti_read_extensions() and
rci_alloc_mem() now return int64_t rather than truncating their own
results, and nifti_read_next_extension() and nifti_check_extension() take
an int64_t `remain`.  nt_read_bricks(), declared in the uninstalled
nifti_tool.h, takes an int64_t length.

Where the narrowing target is an installed prototype it can only be made
explicit, so each such cast carries a note: nifti_image_load_bricks()
returning a brick count as int, nifti_read_subregion_image() returning a
byte count as int, and nifti_read_ascii_image() taking an int header
length.  The remainder are strlen() results
assigned to int and fread()/znzread() counts, bounded by buffers a few
hundred bytes long.

Two format strings were widened to PRId64 to match the parameters that
changed.

FslSeekVolume() now returns 0 or -1 instead of znzseek()'s result,
which for a .nii.gz is the new offset and does not fit an int past 2GB.
FslReadVolumes() returns 0 for a zero-byte volume rather than dividing
by it, FslSetDimensionality() refuses more than 7 dimensions, and
axio_num_tokens() returns -1 rather than wrapping past INT_MAX.

(cherry picked from commit 08c5f80)
@hjmjohnson
hjmjohnson force-pushed the stack/fix-cifti-null-stream branch from 30a79d5 to dcb70d6 Compare November 19, 2026 19:01
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-shorten-64-to-32 branch from deede18 to a343d9a Compare November 19, 2026 19:01
@hjmjohnson

Copy link
Copy Markdown
Member Author

Restacked on the rebased #129 (a343d9a). Master already carries the field-modification bound commit, so it is dropped.

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