Skip to content

BUG: Fix the REJECT_COMPLEX path in nifti_image_read - #132

Draft
hjmjohnson wants to merge 4 commits into
stack/pr-fix-shorten-64-to-32from
stack/fix-image-read-complex-check
Draft

hjmjohnson wants to merge 4 commits into
stack/pr-fix-shorten-64-to-32from
stack/fix-image-read-complex-check

Conversation

@hjmjohnson

Copy link
Copy Markdown
Member

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

Position 10 of 11 in the deep stack. Base: stack/pr-fix-shorten-64-to-32.

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 stack/pr-fix-calloc-transposed-args
10 stack/fix-image-read-complex-check <- this PR 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: Return from the REJECT_COMPLEX path instead of calling exit()
  • ENH: Test that a complex image is rejected without ending the process

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-shorten-64-to-32 branch from 3095b44 to b133669 Compare November 2, 2026 15:32
@hjmjohnson
hjmjohnson force-pushed the stack/fix-image-read-complex-check branch from 36f38a1 to 74def89 Compare November 2, 2026 15:32
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-shorten-64-to-32 branch from b133669 to 61b13f6 Compare November 3, 2026 11:59
@hjmjohnson
hjmjohnson force-pushed the stack/fix-image-read-complex-check branch from 74def89 to 1fafaa0 Compare November 3, 2026 11:59
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-shorten-64-to-32 branch from 61b13f6 to 51ca871 Compare November 3, 2026 12:05
@hjmjohnson
hjmjohnson force-pushed the stack/fix-image-read-complex-check branch from 1fafaa0 to afcf77f Compare November 3, 2026 12:05
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-shorten-64-to-32 branch from 51ca871 to cead6d5 Compare November 6, 2026 14:59
@hjmjohnson
hjmjohnson force-pushed the stack/fix-image-read-complex-check branch from 7788205 to ef2023d 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; memory leak on partial axml_read_file error + .nia bypasses REJECT_COMPLEX + missing nifti2 test + bundled commits)

Replacing exit(13) in library #ifdef REJECT_COMPLEX with clean resource release + return NULL (with nifti_reject_complex_test) is a big improvement for library callers! Four issues need to be addressed:

  1. Bundled axml_read_file ferror(fp) hunk leaks partial xlist (cifti/afni_xml.c:204-207): First, this branch silently bundles Position 11 (cifti/afni_xml.c + cifti/CMakeLists.txt, not mentioned in PR #132's title/description) as well as #122 (and sits on outdated #129 d95b1985). Second, in axml_read_file, when ferror(fp) fires on chunk N > 1 (after Expat has already parsed top-level elements into xlist), break; frees parser and buf and closes fp but returns the partially populated xlist to callers without calling axml_free_xlist(&xlist), violating axml_read_file's error contract (xlist.len == 0) and returning half-parsed XML as if valid!
  2. .nia ASCII complex images bypass #ifdef REJECT_COMPLEX (niftilib/nifti1_io.c:4331 & nifti2/nifti2_io.c:6049): Right above #ifdef REJECT_COMPLEX (nifti1_io.c:4320-4323, nifti2_io.c:5989-5993), if (nifti_is_complete_filename(hfile) && ... ASCII) calls nifti_read_ascii_image(...) and returns nim immediately, completely skipping #ifdef REJECT_COMPLEX for .nia complex images!
  3. nifti_reject_complex_test only wired in niftilib/CMakeLists.txt: nifti2/nifti2_io.c:6050-6056 is modified identically, so please also wire a NIFTI-2 nifti2_reject_complex_test target in nifti2/CMakeLists.txt (testing both N1 and N2 complex headers, plus .nia!).
  4. Test naming & sanitizer regex nits (details inline).

Comment thread cifti/afni_xml.c
blen = (unsigned)fread(buf, 1, (size_t)bsize, fp);
if( ferror(fp) ) {
fprintf(stderr,"** failed to read XML file '%s'\n", fname);
break;

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] If ferror(fp) occurs after the first fread chunk (once Expat has already closed one or more root elements into xlist, or left open elements on gAXD.stack), simply breaking returns a non-empty partial xlist to the caller (who treats xlist.len > 0 as a successful parse!) and leaves gAXD.stack uncleaned! On ferror(fp), free any unpopped gAXD.stack entries and call axml_free_xlist(&xlist) so axml_read_file returns an empty {0, NULL} afni_xml_list on read failure. (Also separate this commit from the REJECT_COMPLEX PR.)

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.

The XML_Parse error path just below also breaks and returns what was parsed, so returning an empty list for a read error alone would be inconsistent. Changing both is a behavior change I would rather decide separately. The XML read fix shares this PR because the stack merged positions 10 and 11.

Comment thread cifti/CMakeLists.txt Resolved
Comment thread niftilib/nifti1_io.c
exit(13);
}
#endif
fprintf(stderr,"Image Exception Unsupported datatype (COMPLEX): use fslcomplex to manipulate: %s\n", hname);

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] Note that 10 lines above this block (has_ascii_header branch), nifti_image_read calls nifti_read_ascii_image(...), closes fp, frees hfile, and return nim; early — bypassing #ifdef REJECT_COMPLEX completely for .nia complex datasets (both here and in nifti2/nifti2_io.c:5989-5993 above line 6050)! Checking REJECT_COMPLEX before returning on the ASCII branch as well makes complex-type rejection consistent across all formats.

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.

Agreed that the .nia path skips the check, but that predates this PR, which only replaces exit() with a return. Extending REJECT_COMPLEX to ASCII images is a behavior change, so I would do it separately.

Comment thread niftilib/CMakeLists.txt Resolved
@hjmjohnson

Copy link
Copy Markdown
Member Author

Restacked on the updated #131. This PR's own fixes: the nifti2 reject-complex test and the prefixed test name (ffa0331), and the sanitizer regex (b31165d).

gdevenyi and others added 4 commits November 19, 2026 13:00
nifti_image_read() ended the calling process with exit(13) when it met
a complex datatype, leaking the open file and hfile on the way out.  A
library reports the condition to its caller: the block now frees the
image, closes the file, releases hfile and returns NULL, which is how
every other rejection in the function behaves.

The message also loses its "64", which was wrong for the 128 and 256
cases.

REJECT_COMPLEX is off by default but is supported: -DFSLSTYLE:BOOL=ON
turns it on, and nifti2/Makefile defines it always.

(cherry picked from commit c8a10fe)
Writes a DT_COMPLEX64 image and a DT_FLOAT32 image, then asserts
nifti_image_read() returns NULL for the first and a usable image for
the second, so a blanket refusal cannot pass.  Registered only when
FSLSTYLE_REJECT_COMPLEX is on.  Against exit(13) the test process dies
with status 13 before reaching either assertion.

(cherry picked from commit 461cd33)
axml_read_file()'s read loop:

    blen = fread(buf, 1, bsize, fp);
    ...
    done = blen < (unsigned)bsize;

A short read is the loop's only stopping condition, so an I/O failure is
indistinguishable from reaching the end of the file: the parse stops
early and the caller is handed whatever was parsed so far, with no
indication that the rest of the document was never read.  ferror() is now
checked and the failure reported.

The loop also called fread() once more after a file ending exactly on a
buffer boundary; testing feof() as part of the stopping condition removes
that read, which the clang static analyzer flags as a read at EOF.

(cherry picked from commit ea212d8)
A directory opens but cannot be read, so afni_xml_tool on cifti/testdata
reaches the ferror() branch; the test pins the diagnostic rather than
the exit status, which was already non-zero before the fix.  A second
test parses a real document and forbids that diagnostic, so a change
that reported a read error unconditionally would not pass.

(cherry picked from commit 283c5d4)
@hjmjohnson
hjmjohnson force-pushed the stack/fix-image-read-complex-check branch from b31165d to c64fbca Compare November 19, 2026 19:01
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-shorten-64-to-32 branch from deede18 to a343d9a Compare November 19, 2026 19:01
@hjmjohnson

Copy link
Copy Markdown
Member Author

Restacked on the rebased #131 (c64fbca). Master already carries the macro-parenthesizing commit, so it is dropped; the REJECT_COMPLEX return path was re-applied on master's text.

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