Repository navigation
BUG: Free the previous filename when an ASCII header repeats the attribute - #100
Open
hjmjohnson wants to merge 2 commits into
Open
hjmjohnson wants to merge 2 commits into
hjmjohnson wants to merge 2 commits into
Conversation
hjmjohnson
marked this pull request as ready for review
September 24, 2026 12:17
hjmjohnson
force-pushed
the
fix/ascii-dup-key-leak
branch
from
October 6, 2026 14:35
61bed64 to
5ad36e8
Compare
…ibute
nifti_image_from_ascii() walks the attributes of an ASCII header and
assigns the two filename fields with
nim->fname = nifti_strdup(rhs) ;
nim->iname = nifti_strdup(rhs) ;
Nothing stops a header from carrying header_filename or image_filename
twice, and nothing rejects the repeat, so the second assignment drops
the first string. The input comes from a file, so the leak is
attacker-controlled in size and count: one copy per repetition.
Direct leak of 5 byte(s) in 1 object(s) allocated from:
#0 malloc
#1 nifti_strdup
#2 nifti_image_from_ascii
Freeing before the assignment costs one call and keeps the last value,
which is what the function already documented by overwriting. The
fields are NULL until the first assignment, and free(NULL) is defined,
so no other path changes.
The same two lines exist in nifti1_io.c and are fixed there too.
Found by fuzzing nifti_image_from_ascii() with clang's libFuzzer under
AddressSanitizer.
nifti_image_from_ascii overwrote fname and iname without releasing what they already held, so an ASCII header naming either attribute twice leaked the earlier copy. One fixture, kept under niftilib/testdata, drives both libraries. The pass regex asserts the last value survives. A pass regex makes ctest ignore the exit status, so the leak report itself is matched by a fail regex; the sanitize-clang-linux leg, whose LeakSanitizer reports the lost copies, is the one that turns red without the fix.
hjmjohnson
force-pushed
the
fix/ascii-dup-key-leak
branch
from
November 6, 2026 16:53
5ad36e8 to
0b852c8
Compare
Member
Author
|
@greptileai review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Re-submission of #69, reverted from
masteron 2026-09-24 so it can bereviewed before merging. The fix is the original one, minus a source
comment, and now carries its regression test (moved here from #123).
Base:
master. Independent: nothing has to land before it.Commits
Test
nifti1_tool_ascii_dup_attrandnifti_tool_ascii_dup_attrread onefixture (
niftilib/testdata/dup_attr.nia) that namesheader_filenameand
image_filenametwice. The pass regex checks that the last valuewins; a fail regex on
LeakSanitizer|leaked inmakes thesanitize-clang-linux leg fail when the earlier copy leaks, since a pass
regex alone makes ctest ignore the exit status.
Checked locally with an ASan build (Homebrew clang, macOS,
ASAN_OPTIONS=detect_leaks=1): with the fourfree()lines removed bothtests fail ("12 byte(s) leaked in 2 allocation(s)"); with them restored
both pass, and the full suite passes 346/346.
Ordering for all the re-submitted work is tracked in #84.