Skip to content

BUG: Do fslio's size and offset arithmetic at 64-bit width - #128

Draft
hjmjohnson wants to merge 2 commits into
stack/pr-fix-sign-conversionfrom
stack/fix-fslio-64bit-arithmetic
Draft

hjmjohnson wants to merge 2 commits into
stack/pr-fix-sign-conversionfrom
stack/fix-fslio-64bit-arithmetic

Conversation

@hjmjohnson

Copy link
Copy Markdown
Member

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

Position 6 of 11 in the deep stack. Base: stack/pr-fix-sign-conversion.

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 <- this PR 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 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: Do fslio's size and offset arithmetic at 64-bit width
  • ENH: Test that FslGetVolSize forms its product at 64-bit width

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
@hjmjohnson
hjmjohnson marked this pull request as draft October 6, 2026 15:02
@hjmjohnson
hjmjohnson force-pushed the stack/fix-fslio-64bit-arithmetic branch from b3e460e to fb27d34 Compare November 2, 2026 15:32
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-sign-conversion branch 2 times, most recently from 9303888 to 3d5f812 Compare November 6, 2026 14:59
@hjmjohnson
hjmjohnson force-pushed the stack/fix-fslio-64bit-arithmetic branch from fb27d34 to 3ca1aca 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; incomplete 64-bit fixes in fslio.c + 32-bit size_t test portability)

Widening FslGetVolSize (nx * ny * nz) and byte-offset products in FslWriteVolumes, FslReadRowSeries, and FslReadTimeSeries to size_t fixes real 32-bit int overflows on large 3D/4D volumes! However, looking across the rest of those exact functions in fsliolib/fslio.c reveals three remaining 32-bit truncation bugs plus a 32-bit test issue:

  1. FslWriteVolumes (fsliolib/fslio.c:921-945) still uses 32-bit long int on 64-bit Windows (LLP64) + unchecked calloc: Even with nbytes widened at line 921, bpv and nrows are long int (32-bit on Windows x64!), calloc(1, nx * bpv) is unchecked for NULL and multiplies short nx * long bpv in 32-bit on Windows, and the loop for (r = 0; r < nrows; r++) overflows 32-bit long nrows when writing a multi-GiB volume with low nx!
  2. FslSetDim (fsliolib/fslio.c, sibling to FslSetDimensionality in #127) still overflows 32-bit int (fslio->niftiptr->nvox = nx * ny * nz * t * 1 * 1 * 1 evaluated in short/int!).
  3. fsliolib/fslio_volsize_test.c:30-37 breaks on 32-bit (ILP32) builds and omits <string.h>: Details inline.

Comment thread fsliolib/fslio.c Resolved
Comment thread fsliolib/fslio_volsize_test.c Resolved
@hjmjohnson
hjmjohnson force-pushed the stack/fix-fslio-64bit-arithmetic branch from 3ca1aca to a17e418 Compare November 17, 2026 22:50
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-sign-conversion branch from 3d5f812 to 80202e6 Compare November 17, 2026 22:50
@hjmjohnson

Copy link
Copy Markdown
Member Author

Restacked on the updated #127. This PR's own fixes: the FslWriteVolumes swap loop (6ad5cd7) and test portability (a17e418).

A byte count or file offset is declared size_t, but the expression
assigned to it multiplies ints, so the arithmetic happens at 32 bits
and only the result is widened:

    size_t FslGetVolSize(...)
    { return (nx * ny * nz); }           /* int product */

    offset = ((ydim * zVox + yVox) * xdim + xVox) * wordsize;

For a volume over 2GB the product overflows before it is ever assigned
to the size_t, so the destination type buys nothing.  The signed
overflow is undefined, so whether the wide value survives is up to the
optimizer: clang folds it to the correct 64-bit result at -O2 and
yields 0 at -O0.  Affects FslGetVolSize(), FslWriteVolumes(),
FslReadSliceSeries(), FslReadRowSeries() and FslReadTimeSeries().
Each operand is now widened before the multiply rather than after.

The slbytes and volbytes assignments in FslReadSliceSeries() and
FslReadTimeSeries() are left alone because another open pull request
changes those exact lines.

The neurological-order swap in FslWriteVolumes() indexes its buffer
with size_t rather than long, which is 32 bits on Windows, and returns 0
when its scratch buffer cannot be allocated.

(cherry picked from commit aee824e)
The test drives FslGetVolSize() with dimensions whose product exceeds
INT_MAX and asserts the exact 64-bit value, plus a small volume so an
over-broad change cannot pass it.  It fails in a -O0 build without the
widening fix and passes with it; at -O2 clang folds the signed overflow
to the wide result, so only the unoptimized build observes it.

(cherry picked from commit 87b5264)
@hjmjohnson
hjmjohnson force-pushed the stack/fix-fslio-64bit-arithmetic branch from a17e418 to 2c94955 Compare November 19, 2026 19:01
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-sign-conversion branch from 80202e6 to 5a30d68 Compare November 19, 2026 19:01
@hjmjohnson

Copy link
Copy Markdown
Member Author

Restacked on the rebased #127 (2c94955); no other changes.

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