Skip to content

BUG: Do not write to the stream that failed to open - #129

Draft
hjmjohnson wants to merge 3 commits into
stack/fix-fslio-64bit-arithmeticfrom
stack/fix-cifti-null-stream
Draft

hjmjohnson wants to merge 3 commits into
stack/fix-fslio-64bit-arithmeticfrom
stack/fix-cifti-null-stream

Conversation

@hjmjohnson

@hjmjohnson hjmjohnson commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Re-submission of #72, reverted from master on 2026-09-24. Review added
an exit-status fix: cifti_tool now exits 1 when it cannot display or
evaluate 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

# 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 <- this PR 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
  • BUG: Do not write to the stream that failed to open
  • ENH: Cover the unopenable -output path in cifti_tool
  • BUG: Give the statement macros a do/while(0) body
  • STYLE: Align the continuations in the macros this branch rewrites
  • BUG: Exit non-zero when cifti_tool cannot display or evaluate

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
seanm
seanm previously requested changes Oct 20, 2026
Comment thread cifti/cifti_tool.c Resolved
@hjmjohnson
hjmjohnson force-pushed the stack/fix-fslio-64bit-arithmetic branch from b3e460e to fb27d34 Compare November 2, 2026 15:32
@hjmjohnson
hjmjohnson force-pushed the stack/fix-cifti-null-stream branch 3 times, most recently from c5b60c5 to 4ed6127 Compare November 6, 2026 14:59
@hjmjohnson
hjmjohnson force-pushed the stack/fix-fslio-64bit-arithmetic branch from fb27d34 to 3ca1aca 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; 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:

  1. [HIGH — Dangling FILE * in global gAXD.wstream] eval_cifti_extension (cifti/cifti_tool.c:248): Calls axml_set_wstream(fp) and then at the end of eval_cifti_extension calls close_stream(fp) (fclose(fp) when fp != stdout), leaving global gAXD.wstream pointing to a closed, freed FILE *! Any subsequent axml_* call using axml_get_wstream() triggers use-after-free on FILE *. Reset axml_set_wstream(NULL) (which sets stdout) before close_stream(fp).
  2. disp_cifti_extension (cifti/cifti_tool.c:222-226) truncates -output and writes the error message into the output file instead of stderr: Because fp = open_write_stream(opts->fout) runs before if (!ext), passing -disp_cext -output out.xml on an input with no CIFTI extension creates/truncates out.xml and writes "** no CIFTI extension in ... " into out.xml rather than stderr! Check if (!ext) (writing to stderr) before calling open_write_stream(opts->fout).
  3. Stack sync: Downstream PRs #131 and #132 are still based on pre-force-push #129 (d95b1985 instead of 4ed61275), reverting #129's review fixes; and #129 bundles #120.

Comment thread cifti/cifti_tool.c
}

fp = open_write_stream(opts->fout);
if( !fp ) return 1;

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

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.

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.

Comment thread cifti/cifti_tool.c Resolved
@hjmjohnson
hjmjohnson dismissed seanm’s stale review November 17, 2026 17:56

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

@hjmjohnson
hjmjohnson force-pushed the stack/fix-fslio-64bit-arithmetic branch from 3ca1aca to a17e418 Compare November 17, 2026 22:50
@hjmjohnson
hjmjohnson force-pushed the stack/fix-cifti-null-stream branch from 4ed6127 to 30a79d5 Compare November 17, 2026 22:50
@hjmjohnson

Copy link
Copy Markdown
Member Author

Restacked on the updated #128. This PR's own fix: the write stream is reset before it is closed (8ade645). #131 and #132 now sit on this head (30a79d5).

gdevenyi and others added 3 commits November 19, 2026 13:00
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>
@hjmjohnson
hjmjohnson force-pushed the stack/fix-cifti-null-stream branch from 30a79d5 to dcb70d6 Compare November 19, 2026 19:01
@hjmjohnson
hjmjohnson force-pushed the stack/fix-fslio-64bit-arithmetic branch from a17e418 to 2c94955 Compare November 19, 2026 19:01
@hjmjohnson

Copy link
Copy Markdown
Member Author

Restacked on the rebased #128 (dcb70d6). Master already carries the two statement-macro 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