Repository navigation
BUG: Fix the REJECT_COMPLEX path in nifti_image_read - #132
hjmjohnson wants to merge 4 commits into
Conversation
3095b44 to
b133669
Compare
36f38a1 to
74def89
Compare
b133669 to
61b13f6
Compare
74def89 to
1fafaa0
Compare
61b13f6 to
51ca871
Compare
1fafaa0 to
afcf77f
Compare
51ca871 to
cead6d5
Compare
7788205 to
ef2023d
Compare
gdevenyi
left a comment
There was a problem hiding this comment.
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:
- Bundled
axml_read_fileferror(fp)hunk leaks partialxlist(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#129d95b1985). Second, inaxml_read_file, whenferror(fp)fires on chunk N > 1 (after Expat has already parsed top-level elements intoxlist),break;freesparserandbufand closesfpbut returns the partially populatedxlistto callers without callingaxml_free_xlist(&xlist), violatingaxml_read_file's error contract (xlist.len == 0) and returning half-parsed XML as if valid! .niaASCII 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)callsnifti_read_ascii_image(...)and returnsnimimmediately, completely skipping#ifdef REJECT_COMPLEXfor.niacomplex images!nifti_reject_complex_testonly wired inniftilib/CMakeLists.txt:nifti2/nifti2_io.c:6050-6056is modified identically, so please also wire a NIFTI-2nifti2_reject_complex_testtarget innifti2/CMakeLists.txt(testing both N1 and N2 complex headers, plus.nia!).- Test naming & sanitizer regex nits (details inline).
| blen = (unsigned)fread(buf, 1, (size_t)bsize, fp); | ||
| if( ferror(fp) ) { | ||
| fprintf(stderr,"** failed to read XML file '%s'\n", fname); | ||
| break; |
There was a problem hiding this comment.
[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.)
There was a problem hiding this comment.
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.
| exit(13); | ||
| } | ||
| #endif | ||
| fprintf(stderr,"Image Exception Unsupported datatype (COMPLEX): use fslcomplex to manipulate: %s\n", hname); |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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.
cead6d5 to
deede18
Compare
ef2023d to
b31165d
Compare
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)
b31165d to
c64fbca
Compare
deede18 to
a343d9a
Compare
Re-submission of #74, reverted from
masteron 2026-09-24. Content isunchanged 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
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-32stack/pr-fix-calloc-transposed-argsstack/fix-image-read-complex-check<- this PRstack/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.