Skip to content

BUG: Do not use sscanf's output when sscanf matched nothing - #115

Merged
hjmjohnson merged 1 commit into
masterfrom
fix/ascii-scan-failure
Nov 14, 2026
Merged

hjmjohnson merged 1 commit into
masterfrom
fix/ascii-scan-failure

Conversation

@hjmjohnson

@hjmjohnson hjmjohnson commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

nifti_image_from_ascii() advanced its position and called strcmp() on
an uninitialized buffer when sscanf() matched nothing (whitespace-only
input). All three header scans now check for one conversion before using
the result. Fixed in nifti1_io.c and nifti2_io.c, with a test for each.

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

Scope
  • The title covers the three %1023s%n scans in the ASCII header scanner.
    The sto_xyz_matrix scan is left alone on purpose: its targets are
    zeroed by the image's calloc, so a short match keeps zeros and reads
    nothing uninitialized.
  • Whitespace-only input still returns NULL, now without undefined
    behavior; *bytes_read is not written. Both in-tree callers check for
    NULL, and the in-tree readers reach this function only after
    has_ascii_header().
  • An unrelated pre-existing defect nearby (kk != ';' compares the index,
    not str[kk]) is not touched here.
Tests
  • nifti_first_test (niftilib/nifti_tester001.c) and the new
    nifti2_from_ascii_test pass " \n\t " and require NULL.
  • Both the old and the new code return NULL, so plain ctest passes either
    way. The defect is visible only to a memory checker: with the fix
    reverted, valgrind memcheck (the valgrind-gcc-linux job) reports an
    uninitialized-value use in strcmp(). That job submits memcheck defects
    to CDash but does not fail on them. ASan and UBSan do not detect
    uninitialized reads; the original report came from MemorySanitizer.
  • Local: 345/345 tests pass (Release, and Debug with ASan and UBSan).
Commits
  • BUG: Do not use sscanf's output when sscanf matched nothing

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 fix/ascii-scan-failure branch from 49c6ec7 to e210ddd Compare October 6, 2026 14:34
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
hjmjohnson force-pushed the fix/ascii-scan-failure branch from e210ddd to 08e0b39 Compare November 6, 2026 16:53

@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, 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, %n does not increment sscanf's assignment count, and when sscanf returns EOF (whitespace-only / end-of-string) or 0, neither lhs/rhs nor nn is written — so if( ii != 1 ) is the exact condition required before touching nn or lhs/rhs.
  • Verified all early break/return NULL paths: nim has not yet been allocated at the first return NULL, and the two breaks inside the attribute loop exit cleanly to nifti_image_free(nim); return NULL; when /> was not reached.

Comment thread nifti2/nifti2_io.c Resolved
@hjmjohnson
hjmjohnson merged commit bd7b01c into master Nov 14, 2026
8 checks passed
@hjmjohnson
hjmjohnson deleted the fix/ascii-scan-failure branch November 14, 2026 15:28

@seanm seanm 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.

The rest lgtm.

@@ -0,0 +1,19 @@
/* sscanf matches nothing here; run under memcheck to catch an uninitialized read */
#include <stdio.h>
#include <nifti2_io.h>

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.

This ought to be "" not <>. But seems the codebase is inconsistent about this. I'll make a followup PR.

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.

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