Repository navigation
ENH: Make the implicit sign conversions explicit - #127
hjmjohnson wants to merge 4 commits into
Conversation
a0f6bd3 to
eae24db
Compare
03bcc02 to
9303888
Compare
eae24db to
5748f94
Compare
9303888 to
3d5f812
Compare
gdevenyi
left a comment
There was a problem hiding this comment.
Review by Google Carbon
Verdict: Request Changes (Draft; 1 master regression + 3 dangerous sign/overflow conversions)
Audited every -Wsign-conversion cast across all 6 files. Most (size_t) widening casts after > 0 checks (and fixing the transposed calloc(slen+1, sizeof(char)) in nifti1_io.c:4425) are clean, but five hunks introduce regressions or mask real bugs:
- [BLOCKER — Regression vs
master21b764b]strip_whitespaceincifti/afni_xml.c:148, 895, 905, 909, 916: Re-introduces(char *)strcasts onconst char *str, revertingmastercommit21b764b(const char *strip_whitespace) and re-breaking-Wcast-qual! - [CRITICAL — Heap Buffer Overflow on 32-bit / >2 GiB ROI]
rci_alloc_mem(niftilib/nifti1_io.c:7433):int sizeis computed in 32-bit signedint(size = prods[0] * nbyperetc.), which overflows on >2 GiB collapse/ROI reads; castingmalloc((size_t)size)silences-Wsign-conversionwhile allocating a tiny wrapped buffer (orSIZE_MAX-ish negative wrap) right beforerci_read_data(lines 7380–7399, widened tosize_tin this very PR!) reads the full 64-bitread_sizeinto*data! Comparenifti2/nifti2_io.c, which usesint64_t/size_tinrci_alloc_mem. - [HIGH] Unchecked
(size_t)znztell(...)casts (fsliolib/fslio.c:1024, 1083, 1144andniftilib/nifti1_io.c:7094):znztell()returns signedznz_off_t(-1on error). Casting directly tosize_tturns-1intoSIZE_MAX(18446744073709551615) and passes it intorci_read_data/znzseek! Checkznz_off_t pos = znztell(...); if (pos < 0) ...before casting. - [MEDIUM] Broken 2–4 GiB
.img/.niafilesize check (niftilib/nifti1_io.c:2455, 4939-4940):nifti_get_filesizeinnifti1_io.c:2455castsbuf.st_sizetoint(return (int)buf.st_size;)! Assigning thatinttoconst int64_t fsizeat line 4939 sign-extends any[2 GiB, 4 GiB)file to a negativeint64_t, causing the newif (fsize <= 0)check at line 4940 to reject valid 2–4 GiB files (whereas previouslyii = (size_t)nifti_get_filesize(...)happened to recover[2 GiB, 4 GiB)on 32-bit and failed differently)! Fixnifti_get_filesizeinternal helper or checkbuf.st_sizeproperly without 32-bitinttruncation. - Narrowing
(int)dataLength+ redundantmemsetafterstrncpy(nifti2/nifti_tool.c:4025-4028,niftilib/nifti1_tool.c:3034-3037): Details inline. (Also drop hitchhiking#115,#116,#118on rebase.)
| ii = nifti_get_filesize( nim->iname ) ; | ||
| if( ii == 0 ){ | ||
| if( g_opts.debug > 0 ) LNI_FERR(fname,"empty data file",nim->iname); | ||
| const int64_t fsize = nifti_get_filesize( nim->iname ) ; |
There was a problem hiding this comment.
[Google Carbon] Subtle 2–4 GiB bug: in nifti1_io.c:2455 (unlike nifti2_io.c where nifti_get_filesize returns int64_t), nifti_get_filesize returns int (return (int)buf.st_size;)! So for a valid 2.5 GiB .img file, nifti_get_filesize wraps to a negative int, fsize becomes negative, and if (fsize <= 0) on line 4940 rejects the file as "empty or unreadable data file"! (And for a 4.1 GiB file, (int)buf.st_size wraps to +100 MiB!) Note nifti_get_filesize is NIO_API (int in nifti1_io.h), so consider an internal static int64_t nifti_get_filesize64(const char *) helper for internal callers.
There was a problem hiding this comment.
This is not a regression on LP64: the old code turned the wrapped int into a garbage size_t offset, and the new one rejects it with a message. The real limit is the int return of the public nifti_get_filesize, and only .nia images (iname_offset < 0) reach this path, so I would fix it in a separate change rather than here.
| strncpy(dest, data, (size_t)(field->len)); | ||
| if( nchars < field->len ) /* clear the rest */ | ||
| memset(dest+nchars, '\0', field->len-nchars); | ||
| memset(dest+nchars, '\0', (size_t)(field->len-nchars)); |
There was a problem hiding this comment.
[Google Carbon] Per C11 §7.24.2.4, strncpy(dest, data, (size_t)field->len) already pads dest with '\0' bytes out to field->len whenever strlen(data) < (size_t)field->len! So nchars = (int)dataLength (which truncates if dataLength > INT_MAX) and the manual memset on lines 4027–4028 (and niftilib/nifti1_tool.c:3034-3037) are completely redundant — strncpy(dest, data, (size_t)field->len); dest[field->len - 1] = '\0'; (or leaving deliberate fixed-width behavior if non-NUL-terminated) suffices without nchars.
There was a problem hiding this comment.
dataLength is the strlen of a command-line argument, which ARG_MAX bounds far below INT_MAX, so the cast cannot truncate. The memset is redundant but harmless upstream code, and I would rather not diverge from upstream for it.
3d5f812 to
80202e6
Compare
5748f94 to
0472f7c
Compare
The build instructions in README.md were two lines about 'make all',
which builds a subset of the tree with a Makefile nothing else in the
project uses. They now describe the CMake build the CI and the install
rules actually use, and note that the Makefile is unmaintained and does
not cover nifti2 or cifti.
A new section records how to run the sanitizers and valgrind, including
three things that each cost an afternoon to work out:
* valgrind's memcheck needs --trace-children=yes here, because most of
the tests are shell scripts that exec the tools. Without it valgrind
inspects the shell, sees nothing and reports a clean run;
* ctest -T memcheck exits 0 even when valgrind reports defects, so the
logs have to be read;
* valgrind refuses to start without the C library's debug symbols, and
on distributions that ship a stripped ld.so with no debuginfo package
-- Arch and its derivatives -- it cannot be run at all, DEBUGINFOD_URLS
included, because those builds are not on any debuginfod server. A
container recipe is given.
Every command in the new sections was run against this tree.
(cherry picked from commit 36976a2)
…plicit 152 of the -Wsign-conversion findings are an int or int64_t count reaching a size_t parameter of malloc(), calloc(), realloc(), memcpy(), memset(), strncpy(), znzread(), znzwrite() or fread(). Where a count is multiplied by a sizeof, the cast goes on the count rather than around the product, so the multiply happens at 64-bit width: (int)count * sizeof(T) can overflow before it is widened. The counts cannot be negative. nifti_update_dims_from_array() clamps every dim[i] to at least 1, nifti_datatype_sizes() leaves nbyper at 0 for an unknown datatype, and nifti_image_load() requires nbyper > 0 and nvox > 0. rci_alloc_mem() and make_pivot_list() formed the collapsed-image byte count in int, the type nifti_read_collapsed_image() returns, so a cast at malloc() would only hide an overflow there. Both now refuse a product past INT_MAX; nifti_collapsed_overflow_test covers it. (cherry picked from commit 1371c71)
The findings that are not allocation, copy or I/O lengths: file offsets, extension bookkeeping, and the ANALYZE orientation byte. znztell() returns a signed znz_off_t stored in size_t locals, so the znzseek() arguments are cast to znz_off_t and the arithmetic happens at the width the function takes. fslio's nvox is widened per operand so the product of seven int dimensions is computed at 64 bits. XML_Parse() takes an int length while blen is unsigned; the conversion is now at the call. The ANALYZE orientation byte is read through unsigned char * rather than a signed lvalue. That is value-preserving either way -- signed to unsigned char is defined modulo 256 -- so it states the intent only. (cherry picked from commit 77e495f)
One change is a fix. nifti_read_buffer() passed (int)(ntot / nim->swapsize) to nifti_swap_Nbytes(), which takes a size_t count that was widened in 2010 because it "might not fit as int". The cast reinstated that truncation, leaving most of an image above 2 GiB unswapped. The cast is gone. nifti_image_read() took the file size through (size_t), which turns nifti_get_filesize()'s -1 error return into SIZE_MAX and slips past the guard below it. A signed temporary is used instead, matching what nifti2_io.c already does at the same place. The rest casts an int dimension to size_t at the point of use. (cherry picked from commit 2696f66)
80202e6 to
5a30d68
Compare
0472f7c to
0bc2c4f
Compare
Re-submission of #53, reverted from
masteron 2026-09-24. Content isunchanged from the original.
Position 5 of 11 in the deep stack. Base:
stack/pr-fix-analyzer-leaks.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-conversion<- this PRstack/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-32stack/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.