Skip to content

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

Open
hjmjohnson wants to merge 2 commits into
stack/pr-fix-calloc-transposed-argsfrom
stack/pr-fix-shorten-64-to-32
Open

hjmjohnson wants to merge 2 commits into
stack/pr-fix-calloc-transposed-argsfrom
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.

seanm and others added 2 commits September 24, 2026 07:12
modify_all_fields() and modify_field() wrote into a caller-supplied
buffer at an offset taken from the field table, with no way to check that
the write stayed inside it. Both now take the buffer size, and
modify_field() rejects a field whose offset plus size * len exceeds it.

The check sits ahead of the switch, so it covers every write path rather
than the string case alone, and it reports and returns like the other
failures in the function; an assert() would compile away in the release
builds that ship.

No field table can trip it today: check_total_size() already requires the
offsets to tile the structure exactly. It bounds future edits to them.

(cherry picked from commit 7356eb1)
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, nifti_read_ascii_image() taking an int header length,
and FslSeekVolume()'s int return.  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.

(cherry picked from commit 08c5f80)
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