Repository navigation
BUG: Fix the ambiguous-filename path in nifti_findhdrname - #108
hjmjohnson wants to merge 2 commits into
Conversation
166220a to
0183a14
Compare
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.
0183a14 to
75326eb
Compare
|
@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.
75326eb to
187bd0e
Compare
gdevenyi
left a comment
There was a problem hiding this comment.
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:
- Fixes the
basenameuse-after-free when printing the"Multiple possible filenames for %s"diagnostic. - Checks
gzname = calloc(...)forNULLand frees bothbasenameandhdrnameon OOM. - Replaces library
exit(134)withfree(gzname); free(basename); free(hdrname); return NULL;(and updates the Doxygeneturncontract), 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 removingamb.nii.gz.
One tiny non-blocking test-driver suggestion inline.
| if(NOT rv STREQUAL "1") | ||
| message(FATAL_ERROR "expected exit 1, got '${rv}'\n${err}") | ||
| endif() | ||
| if(NOT err MATCHES "Multiple possible filenames") |
There was a problem hiding this comment.
[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.
| #ifdef FSLSTYLE | ||
| if (nifti_fileexists(hdrname)) { | ||
| free(basename); | ||
| /* basename is read by the ambiguity message below */ |
There was a problem hiding this comment.
Why this comment here? I guess the bot didn't want to just remove the free without justifying itself? Let's remove this comment.
Re-submission of #54, reverted from
masteron 2026-09-24 so it can bereviewed before merging. Under FSLSTYLE, an ambiguous
.nii/.nii.gzpair now makes
nifti_findhdrnamereturn NULL instead of callingexit(134), and the PR now carries its own test.Base:
master. Independent: nothing has to land before it.Visible change:
nifti_toolexits 1 with the "Multiple possiblefilenames" diagnostic instead of 134, and
FslFileExists()returns 0 foran ambiguous pair. FSLSTYLE is off by default, so ITK and other default
builds see no change.
Commits
(use-after-free of
basename, uncheckedgznameallocation, andexit(134), in bothnifti1_io.candnifti2_io.c)(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 forcesFSLSTYLE_NAME_CONFLICTSon andregisters
nifti_ambiguous_hdrnameandnifti1_ambiguous_hdrname. All346 tests pass. With the fix reverted, both new tests fail with
"expected exit 1, got '134'".
Not covered: the read of
basenameafter its free is caught only under asanitizer, and the
gznameallocation-failure branch is not exercised.Ordering for all the re-submitted work is tracked in #84.