Skip to content

BUG: Free the header on nifti_tool's duplicate-file failure paths - #101

Merged
hjmjohnson merged 1 commit into
masterfrom
pr/fix-tool-error-path-leaks
Nov 14, 2026
Merged

hjmjohnson merged 1 commit into
masterfrom
pr/fix-tool-error-path-leaks

Conversation

@hjmjohnson

Copy link
Copy Markdown
Member

Re-submission of #60, reverted from master on 2026-09-24 so it can be
reviewed before merging. Content is unchanged from the original.

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

Commits
  • BUG: Free the header on nifti_tool's duplicate-file failure paths

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
act_mod_hdrs(), act_mod_hdr2s() and act_swap_hdrs() -- five functions
across the two tools -- read a header, then, when -prefix is given,
duplicate the dataset before writing the modified header back.  Each of
the three ways that duplication can fail returns without freeing the
header, and the last of them also loses the strdup'd duplicate name:

    nhdr = nt_read_header(fname, &nver, &swap, 0, ...);
    ...
    if( opts->prefix ) {
       nim = nt_image_read(opts, fname, 1, 1);
       if( !nim ) { fprintf(...); return 1; }              /* nhdr */
       if( nifti_set_filenames(nim, opts->prefix, 1, 1) ) {
          nifti_image_free(nim); return 1;                 /* nhdr */
       }
       dupname = nifti_strdup(nim->fname);
       if( nifti_image_write_status(nim) ) {
          nifti_image_free(nim); return 1;                 /* nhdr, dupname */
       }
    }
    ...
    free(dupname);
    free(nhdr);

The normal path frees both.  Reproduced by pointing -prefix at a
directory that cannot be written:

    nifti_tool -mod_hdr -prefix <read-only dir>/anat1 -infiles anat0.nii \
               -mod_field qoffset_x -17.325

  before:  definitely lost: 348 bytes in 1 blocks
  after:   ERROR SUMMARY: 0 errors from 0 contexts

Separately, act_diff_nims() in both tools releases the first image with
free(nim0) when reading the second one fails.  That is a shallow free: it
loses nim0's fname, iname and any data or extensions.  It now calls
nifti_image_free() like the success path six lines below.

Found by running the test suite under valgrind.  On the normal paths the
suite is clean -- 484 traced processes, no invalid access, no
uninitialised value and no leak in any nifti binary -- so these are
error-path defects that the tests do not otherwise reach.

(cherry picked from commit 40009a9)
@hjmjohnson
hjmjohnson force-pushed the pr/fix-tool-error-path-leaks branch from 1e81f62 to a31bd40 Compare October 6, 2026 14:35

@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

Verified every modified error return path across nifti2/nifti_tool.c (diff_nim, act_mod_hdrs, act_mod_hdr2s, act_swap_hdrs) and niftilib/nifti1_tool.c (diff_nim, act_mod_hdrs, act_swap_hdrs):

  • Ownership of nim0, nhdr, and dupname is tracked accurately on every early return with zero double-free or use-after-free paths (nifti_convert_n1hdr2nim / nifti_convert_n2hdr2nim copy out of *nhdr without retaining the caller's pointer, and write_hdr_to_file closes its stream without freeing nhdr or dupname).
  • Full NIFTI-1 / NIFTI-2 parity (niftilib has no act_mod_hdr2s, accounting for the 3-function vs 4-function difference).

Left one non-blocking suggestion inline for a small failure-path CTest so LeakSanitizer/ASan runs cover these error returns automatically.

Comment thread nifti2/nifti_tool.c Resolved
Comment thread nifti2/nifti_tool.c Resolved
@hjmjohnson
hjmjohnson merged commit 918bdf9 into master Nov 14, 2026
4 checks passed
@hjmjohnson
hjmjohnson deleted the pr/fix-tool-error-path-leaks branch November 14, 2026 14:44
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