Repository navigation
BUG: Do not write to the stream that failed to open - #129
hjmjohnson wants to merge 3 commits into
Conversation
b3e460e to
fb27d34
Compare
c5b60c5 to
4ed6127
Compare
fb27d34 to
3ca1aca
Compare
gdevenyi
left a comment
There was a problem hiding this comment.
Review by Google Carbon
Verdict: Request Changes (Draft; dangling global FILE * bug in eval_cifti_extension + truncated -output on error + stack desync with #131/#132)
Checking open_write_stream(opts->fout) for NULL and propagating disp_cifti_extension / eval_cifti_extension failures into cifti_tool's exit status (with test_write_failure.cmake) is a great fix! Two bugs in those functions and a stack-sync issue need attention:
- [HIGH — Dangling
FILE *in globalgAXD.wstream]eval_cifti_extension(cifti/cifti_tool.c:248): Callsaxml_set_wstream(fp)and then at the end ofeval_cifti_extensioncallsclose_stream(fp)(fclose(fp)whenfp != stdout), leaving globalgAXD.wstreampointing to a closed, freedFILE *! Any subsequentaxml_*call usingaxml_get_wstream()triggers use-after-free onFILE *. Resetaxml_set_wstream(NULL)(which setsstdout) beforeclose_stream(fp). disp_cifti_extension(cifti/cifti_tool.c:222-226) truncates-outputand writes the error message into the output file instead ofstderr: Becausefp = open_write_stream(opts->fout)runs beforeif (!ext), passing-disp_cext -output out.xmlon an input with no CIFTI extension creates/truncatesout.xmland writes"** no CIFTI extension in ... "intoout.xmlrather thanstderr! Checkif (!ext)(writing tostderr) before callingopen_write_stream(opts->fout).- Stack sync: Downstream PRs
#131and#132are still based on pre-force-push#129(d95b1985instead of4ed61275), reverting#129's review fixes; and#129bundles#120.
| } | ||
|
|
||
| fp = open_write_stream(opts->fout); | ||
| if( !fp ) return 1; |
There was a problem hiding this comment.
[Google Carbon] Move the if (!ext) check (lines 224–227) above fp = open_write_stream(opts->fout) and write that diagnostic to stderr instead of fp — otherwise -disp_cext -output existing.xml clobbers existing.xml with "** no CIFTI extension in ..." on failure!
There was a problem hiding this comment.
That branch is unreachable from cifti_tool: process() returns at 'failed to read CIFTI ext' before disp_cifti_extension when there is no CIFTI extension, so -output is never opened. I confirmed it with an image that has no extension, so I left the order as is.
Addressed: the comment is gone, and process() now returns 1 when disp_cifti_extension or eval_cifti_extension fails; cifti_write_failure expects exit 1 for both bad-output cases (passes at 4ed6127).
3ca1aca to
a17e418
Compare
4ed6127 to
30a79d5
Compare
open_write_stream() returns NULL when its fopen() fails, and reports it. Both callers then used the result without testing it. $ cifti_tool -input c.nii -disp_cext -output /nonexistent-dir/x.txt ** failed to open '/nonexistent-dir/x.txt' for writing Segmentation fault (core dumped) disp_cifti_extension() passes it to fprintf(). eval_cifti_extension() hands it to axml_set_wstream(), so the NULL becomes the xml library's write stream and every later write follows it. Both now return 1; the message has already been printed. This file is behind USE_CIFTI_CODE, which defaults to OFF, so the standard workflows do not compile it. Verified with -DUSE_CIFTI_CODE=ON. (cherry picked from commit f368af9)
Driven through a cmake -P script rather than add_test, because PASS_REGULAR_EXPRESSION ignores the exit status and a crash that had already printed the diagnostic would pass. The script pins that the tool exits normally and reports the failure, and that an openable -output is still written with the extension in it, so refusing every write would not pass. (cherry picked from commit 5c08eb3)
process() discarded the return values of disp_cifti_extension() and eval_cifti_extension(), so the tool exited 0 after failing to open its -output file, finding no CIFTI extension, or having no image to display. A script chaining on the exit status went on with output that was never written. Those failures now exit 1, as a missing -input or an unreadable CIFTI file already did. Co-Authored-By: Sean McBride <sean@rogue-research.com>
30a79d5 to
dcb70d6
Compare
a17e418 to
2c94955
Compare
Re-submission of #72, reverted from
masteron 2026-09-24. Review addedan exit-status fix:
cifti_toolnow exits 1 when it cannot display orevaluate the extension.
Position 7 of 11 in the deep stack. Base:
stack/fix-fslio-64bit-arithmetic.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-stream<- this PRstack/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.