Skip to content

ENH: Make the implicit sign conversions explicit - #127

Draft
hjmjohnson wants to merge 4 commits into
stack/pr-fix-analyzer-leaksfrom
stack/pr-fix-sign-conversion
Draft

hjmjohnson wants to merge 4 commits into
stack/pr-fix-analyzer-leaksfrom
stack/pr-fix-sign-conversion

Conversation

@hjmjohnson

Copy link
Copy Markdown
Member

Re-submission of #53, reverted from master on 2026-09-24. Content is
unchanged 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

# 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 <- this PR 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 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
  • ENH: Make the integer conversions in allocations, copies and reads explicit
  • ENH: Make the remaining integer conversions explicit
  • ENH: Make the last implicit sign conversions explicit

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/pr-fix-analyzer-leaks branch from a0f6bd3 to eae24db Compare November 2, 2026 15:32
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-sign-conversion branch from 03bcc02 to 9303888 Compare November 2, 2026 15:32
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-analyzer-leaks branch from eae24db to 5748f94 Compare November 6, 2026 14:59
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-sign-conversion branch from 9303888 to 3d5f812 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; 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:

  1. [BLOCKER — Regression vs master 21b764b] strip_whitespace in cifti/afni_xml.c:148, 895, 905, 909, 916: Re-introduces (char *)str casts on const char *str, reverting master commit 21b764b (const char *strip_whitespace) and re-breaking -Wcast-qual!
  2. [CRITICAL — Heap Buffer Overflow on 32-bit / >2 GiB ROI] rci_alloc_mem (niftilib/nifti1_io.c:7433): int size is computed in 32-bit signed int (size = prods[0] * nbyper etc.), which overflows on >2 GiB collapse/ROI reads; casting malloc((size_t)size) silences -Wsign-conversion while allocating a tiny wrapped buffer (or SIZE_MAX-ish negative wrap) right before rci_read_data (lines 7380–7399, widened to size_t in this very PR!) reads the full 64-bit read_size into *data! Compare nifti2/nifti2_io.c, which uses int64_t / size_t in rci_alloc_mem.
  3. [HIGH] Unchecked (size_t)znztell(...) casts (fsliolib/fslio.c:1024, 1083, 1144 and niftilib/nifti1_io.c:7094): znztell() returns signed znz_off_t (-1 on error). Casting directly to size_t turns -1 into SIZE_MAX (18446744073709551615) and passes it into rci_read_data / znzseek! Check znz_off_t pos = znztell(...); if (pos < 0) ... before casting.
  4. [MEDIUM] Broken 2–4 GiB .img/.nia filesize check (niftilib/nifti1_io.c:2455, 4939-4940): nifti_get_filesize in nifti1_io.c:2455 casts buf.st_size to int (return (int)buf.st_size;)! Assigning that int to const int64_t fsize at line 4939 sign-extends any [2 GiB, 4 GiB) file to a negative int64_t, causing the new if (fsize <= 0) check at line 4940 to reject valid 2–4 GiB files (whereas previously ii = (size_t)nifti_get_filesize(...) happened to recover [2 GiB, 4 GiB) on 32-bit and failed differently)! Fix nifti_get_filesize internal helper or check buf.st_size properly without 32-bit int truncation.
  5. Narrowing (int)dataLength + redundant memset after strncpy (nifti2/nifti_tool.c:4025-4028, niftilib/nifti1_tool.c:3034-3037): Details inline. (Also drop hitchhiking #115, #116, #118 on rebase.)

Comment thread cifti/afni_xml.c Resolved
Comment thread niftilib/nifti1_io.c
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 ) ;

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] 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.

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.

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.

Comment thread niftilib/nifti1_io.c Resolved
Comment thread niftilib/nifti1_io.c Resolved
Comment thread fsliolib/fslio.c Resolved
Comment thread nifti2/nifti_tool.c
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));

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] 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.

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.

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.

@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-sign-conversion branch from 3d5f812 to 80202e6 Compare November 17, 2026 22:50
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-analyzer-leaks branch from 5748f94 to 0472f7c Compare November 17, 2026 22:50
@hjmjohnson

Copy link
Copy Markdown
Member Author

Restacked on the updated #126. This PR's own fixes: strip_whitespace stays const (7904aea), the collapsed-image byte count is guarded and tested (e458c8e), and znztell results are checked (80202e6, 7904aea). The five commits already on master drop in the rebase onto master.

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)
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-sign-conversion branch from 80202e6 to 5a30d68 Compare November 19, 2026 19:01
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-analyzer-leaks branch from 0472f7c to 0bc2c4f Compare November 19, 2026 19:01
@hjmjohnson

Copy link
Copy Markdown
Member Author

Restacked on the rebased #126 (5a30d68). Master already carries five of this PR's commits (gopt linkage, memcpy, sscanf, and the two cdfbin commits), so those are 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