Repository navigation
ENH: Make the 64-to-32 bit truncations explicit - #131
hjmjohnson wants to merge 1 commit into
Conversation
3095b44 to
b133669
Compare
2d90824 to
89d1d56
Compare
51ca871 to
cead6d5
Compare
c5b60c5 to
4ed6127
Compare
gdevenyi
left a comment
There was a problem hiding this comment.
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:
- [CRITICAL — Breaks
.nii.gzseeks >= 2 GiB]FslSeekVolume(fsliolib/fslio.c:1178):
return (int)znzseek(fslio->fileptr, offset, SEEK_SET);— When reading a compressed.nii.gzfile,znzseekcalls zlib'sgzseek, which returns the new 64-bit uncompressed offset on success (0onfseekuncompressed,offsetongzseekcompressed,-1on error)! Castingznzseek(...)to(int)wraps any.nii.gzseek into[2 GiB, 4 GiB)to a negativeint(and wrapsoffset == 4 GiB - 1to-1!), making callers treat a valid seek as a fatal error, while wrapping-1or>= 4 GiBunpredictably! SinceFslSeekVolumereturns0on success /< 0on error (or check callers),return znzseek(fslio->fileptr, offset, SEEK_SET) < 0 ? -1 : 0;. - [HIGH — Division by zero]
FslReadVolumes(fsliolib/fslio.c:850-852):volbytes = FslGetVolSize(fslio) * (size_t)fslio->niftiptr->nbyper;followed immediately byretval /= volbytes;— if anynx/ny/nz == 0ornbyper == 0on a malformed header,volbytes == 0crashes withSIGFPE! Guardif (volbytes == 0) return 0;. - [HIGH — Corrupts >= 2 GiB
.niaASCII reads]nifti_image_read(nifti2/nifti2_io.c:5990): Passing(int)filesizetonifti_read_ascii_image(..., int flen, ...)wraps[2 GiB, 4 GiB)negative (silently dropping extensions innifti2_io.c:6192) and wraps>= 4 GiBto a small positiveint! Change internalstaticnifti_read_ascii_image'sflenparameter toint64_tinnifti2_io.c(wherenifti_get_filesizealready returnsint64_t!) instead of truncatingfilesizetoint. - Unchecked
(int)casts on publicsize_t/int64_tinputs (FslSetDimensionalityfslio.c:1263-1264andaxio_num_tokenscifti/afni_xml_io.c:139), plus rebase onto#129's updated4ed61275(currently reverts#129's force-pushed fixes) and unbundle#121.
| } | ||
| 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 ); |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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.
cead6d5 to
deede18
Compare
4ed6127 to
30a79d5
Compare
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)
30a79d5 to
dcb70d6
Compare
deede18 to
a343d9a
Compare
Re-submission of #52, reverted from
masteron 2026-09-24. Content isunchanged 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
stack/test-regression-coveragemasterstack/pr-fix-alloc-null-checksstack/test-regression-coveragestack/fix-axml-skip-depthstack/pr-fix-alloc-null-checksstack/pr-fix-analyzer-leaksstack/fix-axml-skip-depthstack/pr-fix-sign-conversionstack/pr-fix-analyzer-leaksstack/fix-fslio-64bit-arithmeticstack/pr-fix-sign-conversionstack/fix-cifti-null-streamstack/fix-fslio-64bit-arithmeticstack/pr-fix-calloc-transposed-argsstack/fix-cifti-null-streamstack/pr-fix-shorten-64-to-32<- this PRstack/pr-fix-calloc-transposed-argsstack/fix-image-read-complex-checkstack/pr-fix-shorten-64-to-32stack/pr-fix-xml-read-errorsstack/fix-image-read-complex-checkThe order is the order these changes sat on
masterbefore the revert, soit builds and tests at every step.
Commits introduced by this PR
Ordering for all the re-submitted work is tracked in #84.