Skip to content

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

Merged
hjmjohnson merged 2 commits into
InsightSoftwareConsortium:masterfrom
gdevenyi:fix/cifti-null-stream
Sep 23, 2026
Merged

hjmjohnson merged 2 commits into
InsightSoftwareConsortium:masterfrom
gdevenyi:fix/cifti-null-stream

Conversation

@gdevenyi

Copy link
Copy Markdown

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.

Split out of #56, which is about a different defect in one of the same functions (an extension search that never advanced). #56 touches adjacent lines, so if both are in flight, merge #56 first and this rebases.

Note cifti/ is behind USE_CIFTI_CODE, which defaults to OFF, so the standard workflows do not compile this file. Verified locally with -DUSE_CIFTI_CODE=ON.

🤖 Generated with Claude Code

@hjmjohnson

Copy link
Copy Markdown
Member

Rebased onto current master (b4876bf) and added a red-green regression test. Full suite: 363/363 pass.

Worth your call: process() in cifti_tool.c discards the return value of both disp_cifti_extension() and eval_cifti_extension(), so even with this fix the tool exits 0 after failing to open its -output. This PR does not change that, and the test asserts the current exit-0 behavior. If you want the failure propagated to the exit status, that is a one-line follow-up and I can add it here.

Rebase and commit-message cleanup

The ~29 shared-base commits are already on master under new SHAs, so the rebase replayed only the single commit this PR owns. No conflicts.

Message cleanup: removed the Co-Authored-By: trailer naming an AI tool, and rewrapped the body to the project's line limits. Author, committer, dates and tree are unchanged.

Test added, and the red proof

cifti/test_write_failure.cmake, driven by cmake -P rather than a plain add_test, because PASS_REGULAR_EXPRESSION ignores the exit status — a crash that had already printed the diagnostic would pass.

Red, with only the cifti_tool.c fix hunks reverted:

1/1 Test #363: cifti_write_failure ...***Failed
CMake Error at cifti/test_write_failure.cmake:16 (message):
  expected result 0, got 'Segmentation fault'

  ** failed to open '/tmp/prb-u1/cifti/no_such_dir/out.txt' for writing

Green with the fix. The script asserts a normal exit and the presence of the diagnostic, not merely that the command failed. Companion assertion: an openable -output must still be written and must contain MapName, so a fix that refused every write would not pass.

Scope note: the red proof covers the disp_cifti_extension() hunk. The eval_cifti_extension() hunk has no observable difference on this platform — unfixed, axml_set_wstream(NULL) is accepted and the run still exits 0 with the same diagnostic — so it is not separately pinned. It is still the right change.

@hjmjohnson
hjmjohnson force-pushed the fix/cifti-null-stream branch from a46d8f4 to 162e8dc Compare September 22, 2026 15:54
gdevenyi and others added 2 commits September 22, 2026 19:45
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.
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.
@hjmjohnson
hjmjohnson force-pushed the fix/cifti-null-stream branch from 162e8dc to 812fbb1 Compare September 23, 2026 00:47
@hjmjohnson
hjmjohnson merged commit 5c08eb3 into InsightSoftwareConsortium:master Sep 23, 2026
26 checks passed
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