Skip to content

BUG: Fix the ambiguous-filename path in nifti_findhdrname - #108

Open
hjmjohnson wants to merge 2 commits into
masterfrom
pr/fix-library-no-exit
Open

hjmjohnson wants to merge 2 commits into
masterfrom
pr/fix-library-no-exit

Conversation

@hjmjohnson

@hjmjohnson hjmjohnson commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Re-submission of #54, reverted from master on 2026-09-24 so it can be
reviewed before merging. Under FSLSTYLE, an ambiguous .nii/.nii.gz
pair now makes nifti_findhdrname return NULL instead of calling
exit(134), and the PR now carries its own test.

Base: master. Independent: nothing has to land before it.

Visible change: nifti_tool exits 1 with the "Multiple possible
filenames" diagnostic instead of 134, and FslFileExists() returns 0 for
an ambiguous pair. FSLSTYLE is off by default, so ITK and other default
builds see no change.

Commits
  • BUG: Fix the ambiguous-filename path in nifti_findhdrname
    (use-after-free of basename, unchecked gzname allocation, and
    exit(134), in both nifti1_io.c and nifti2_io.c)
  • ENH: Cover the ambiguous-filename path in nifti_findhdrname
    (moved here from ENH: Add regression tests for the bug fixes of the past two days #123; one shared driver script,
    cmake/test_ambiguous_hdrname.cmake)
Testing

Built with -DFSLSTYLE=ON, which forces FSLSTYLE_NAME_CONFLICTS on and
registers nifti_ambiguous_hdrname and nifti1_ambiguous_hdrname. All
346 tests pass. With the fix reverted, both new tests fail with
"expected exit 1, got '134'".

Not covered: the read of basename after its free is caught only under a
sanitizer, and the gzname allocation-failure branch is not exercised.

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:17
@hjmjohnson
hjmjohnson force-pushed the pr/fix-library-no-exit branch from 166220a to 0183a14 Compare October 6, 2026 14:35
Under FSLSTYLE the block that reports "Multiple possible filenames" has
three defects, and they cannot be separated.

  free(basename);                                  <- freed here
  char *gzname = calloc(...);                      <- unchecked
  strcpy(gzname, hdrname);                         <- used at once
  ...
    fprintf(stderr,"... %s\n", basename);          <- read after free
    exit(134);                                     <- kills the caller

basename is read by the message, so it cannot be freed at the top.
gzname goes straight into strcpy(), so it has to be checked.  And once
the function returns NULL instead of calling exit(), every path out has
to release what it holds, which is what ties the three together: moving
the free of basename decides what the error paths must free, and there
are no error paths to speak of until exit() is gone.

Callers now see NULL for an ambiguous name, and the doc comment says so.
FslFileExists() returns 0 for an ambiguous .nii/.nii.gz pair, and
nifti_tool exits 1 with the diagnostic instead of 134.

FSLSTYLE is off by default but is a supported configuration:
-DFSLSTYLE:BOOL=ON turns it on, and `FSLSTYLE=1 make` defines it.
Built and tested with -DFSLSTYLE=ON.

The nifti_image_read() half of the original change is a separate commit.
@hjmjohnson
hjmjohnson force-pushed the pr/fix-library-no-exit branch from 0183a14 to 75326eb Compare November 6, 2026 16:54
@hjmjohnson

Copy link
Copy Markdown
Member Author

@greptileai review

The tool writes both members of the pair itself, so no data is needed.
The driver asserts exit 1 and the diagnostic, because a library reports
the clash to its caller rather than ending the process, and a plain
add_test cannot tell that exit from the abort it replaced.

It then removes the .gz and repeats, so an unconditional NULL return
could not pass. Guarded on FSLSTYLE_NAME_CONFLICTS, which is off by
default (-DFSLSTYLE=ON forces it on), so the test is registered only
where the code is compiled. The nifti1 and nifti2 tests share one
driver script.

Two parts of the fix are not covered: the read of basename after its
free is caught only when the build uses a sanitizer, and nothing
exercises the branch where the gzname allocation fails.
@hjmjohnson
hjmjohnson force-pushed the pr/fix-library-no-exit branch from 75326eb to 187bd0e Compare November 6, 2026 17:00

@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: Approve

Clean, complete fix for all three #ifdef FSLSTYLE defects in nifti_findhdrname across both nifti2/nifti2_io.c and niftilib/nifti1_io.c:

  1. Fixes the basename use-after-free when printing the "Multiple possible filenames for %s" diagnostic.
  2. Checks gzname = calloc(...) for NULL and frees both basename and hdrname on OOM.
  3. Replaces library exit(134) with free(gzname); free(basename); free(hdrname); return NULL; (and updates the Doxygen eturn contract), with a clean cross-platform CMake script test (cmake/test_ambiguous_hdrname.cmake) verifying both rejection (amb.nii + amb.nii.gz) and unambiguous resolution after removing amb.nii.gz.

One tiny non-blocking test-driver suggestion inline.

Comment thread nifti2/nifti2_io.c Resolved
if(NOT rv STREQUAL "1")
message(FATAL_ERROR "expected exit 1, got '${rv}'\n${err}")
endif()
if(NOT err MATCHES "Multiple possible filenames")

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] Nice self-contained CMake test! Minor nit: checking if(err MATCHES "ERROR: (Address|Leak)Sanitizer|runtime error:") as well ensures that an LSan leak or UBSan fault (which can also exit with code 1) cannot masquerade as the expected exit 1 if the ambiguity diagnostic was already printed.

Comment thread niftilib/nifti1_io.c
#ifdef FSLSTYLE
if (nifti_fileexists(hdrname)) {
free(basename);
/* basename is read by the ambiguity message below */

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.

Why this comment here? I guess the bot didn't want to just remove the free without justifying itself? Let's remove this comment.

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