Repository navigation
BUG: Do not use sscanf's output when sscanf matched nothing - #115
Merged
Merged
Conversation
hjmjohnson
marked this pull request as ready for review
September 24, 2026 12:17
hjmjohnson
force-pushed
the
fix/ascii-scan-failure
branch
from
October 6, 2026 14:34
49c6ec7 to
e210ddd
Compare
nifti_image_from_ascii() scans its input three times with
ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; spos += nn ;
if( ii == 0 || strcmp(lhs,"<nifti_image") != 0 ) return NULL ;
Two things are wrong with that pair of lines. sscanf() returns EOF,
not 0, when the input ends before anything is matched, so ii == 0 does
not detect the failure; and nn is added to spos before ii is looked
at, so the position advances by an indeterminate amount.
For a string holding nothing but whitespace the first scan returns
EOF, the test passes, and strcmp() reads the uninitialized 1024 byte
stack buffer. MemorySanitizer:
WARNING: MemorySanitizer: use-of-uninitialized-value
#0 nifti_image_from_ascii nifti2_io.c
All three now test for the one successful conversion and only then
advance spos, which is what the surrounding code assumed. The two
inside the loop cannot fail today, because whitespace is skipped and
end of string is checked before each, and the in-tree readers gate on
has_ascii_header(), so the first is reached only through a direct call
to this published function. The same three lines exist in
nifti1_io.c and are corrected there too.
The sto_xyz_matrix sscanf is left alone on purpose: its sixteen
targets are zeroed by the calloc of the image, so a short match keeps
those zeros and reads nothing uninitialized.
nifti_first_test (nifti1) and the new nifti2_from_ascii_test pass
" \n\t " and require NULL. Both versions of the code return NULL, so
plain ctest passes either way; the defect shows only under a memory
checker. ASan and UBSan do not detect uninitialized reads. With the
fix reverted, the valgrind-gcc-linux job's ctest_memcheck pass would
report an uninitialized-value defect in strcmp() for both tests on its
CDash submission; the job itself stays green, because the dashboard
script does not fail on memcheck defects.
Found by fuzzing nifti_image_from_ascii() with libFuzzer under
MemorySanitizer.
hjmjohnson
force-pushed
the
fix/ascii-scan-failure
branch
from
November 6, 2026 16:53
e210ddd to
08e0b39
Compare
gdevenyi
approved these changes
Nov 12, 2026
gdevenyi
left a comment
Collaborator
There was a problem hiding this comment.
Review by Google Carbon
Verdict: Approve
Clean, textbook fix across both nifti2/nifti2_io.c and niftilib/nifti1_io.c with regression tests in both nifti2_from_ascii_test.c and nifti_tester001.c.
- Per C11 §7.21.6.2,
%ndoes not incrementsscanf's assignment count, and whensscanfreturnsEOF(whitespace-only / end-of-string) or0, neitherlhs/rhsnornnis written — soif( ii != 1 )is the exact condition required before touchingnnorlhs/rhs. - Verified all early
break/return NULLpaths:nimhas not yet been allocated at the firstreturn NULL, and the twobreaks inside the attribute loop exit cleanly tonifti_image_free(nim); return NULL;when/>was not reached.
seanm
approved these changes
Nov 15, 2026
| @@ -0,0 +1,19 @@ | |||
| /* sscanf matches nothing here; run under memcheck to catch an uninitialized read */ | |||
| #include <stdio.h> | |||
| #include <nifti2_io.h> | |||
Collaborator
There was a problem hiding this comment.
This ought to be "" not <>. But seems the codebase is inconsistent about this. I'll make a followup PR.
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.
nifti_image_from_ascii()advanced its position and calledstrcmp()onan uninitialized buffer when
sscanf()matched nothing (whitespace-onlyinput). All three header scans now check for one conversion before using
the result. Fixed in
nifti1_io.candnifti2_io.c, with a test for each.Base:
master. Independent: nothing has to land before it.Scope
%1023s%nscans in the ASCII header scanner.The
sto_xyz_matrixscan is left alone on purpose: its targets arezeroed by the image's
calloc, so a short match keeps zeros and readsnothing uninitialized.
behavior;
*bytes_readis not written. Both in-tree callers check forNULL, and the in-tree readers reach this function only after
has_ascii_header().kk != ';'compares the index,not
str[kk]) is not touched here.Tests
nifti_first_test(niftilib/nifti_tester001.c) and the newnifti2_from_ascii_testpass" \n\t "and require NULL.way. The defect is visible only to a memory checker: with the fix
reverted, valgrind memcheck (the
valgrind-gcc-linuxjob) reports anuninitialized-value use in
strcmp(). That job submits memcheck defectsto CDash but does not fail on them. ASan and UBSan do not detect
uninitialized reads; the original report came from MemorySanitizer.
Commits
Ordering for all the re-submitted work is tracked in #84.