[clang][Sema] scanf format check reads past the end of a string ending in "%*2"; asserts "Invalid byte number!"
The scanf format checker reads past the end of a format string that ends in %*<width>. When the stray byte is not NUL, the parser keeps scanning past the literal, and computing a diagnostic location for it asserts. Valid code.
Reproducer
https://godbolt.org/z/PqvoPoEq7 (assertions trunk and release trunk)
#include <stdio.h>
void f(FILE *d) { fscanf(d, "a" "%*2", 0); }
clang --target=x86_64-unknown-linux-gnu -fsyntax-only repro.c
<source>:2:37: warning: invalid conversion specifier '6' [-Wformat-invalid-specifier]
clang: clang/lib/AST/Expr.cpp:1357: clang::SourceLocation clang::StringLiteral::getLocationOfByte(unsigned int, const clang::SourceManager&, const clang::LangOptions&, const clang::TargetInfo&, unsigned int*, unsigned int*) const: Assertion `TokNo < getNumConcatenated() && "Invalid byte number!"' failed.
#10 clang::StringLiteral::getLocationOfByte(...) const
#11 (anonymous namespace)::CheckFormatHandler::getLocationOfByte(char const*)
#12 (anonymous namespace)::CheckFormatHandler::HandleNullChar(char const*)
#13 clang::analyze_format_string::ParseScanfString(...)
- There is no
'6'in the format string; it is a byte from past the end of the literal. - Release builds do not crash, but print a second, bogus
warning: format string contains '\0' within the string body [-Wformat]with no source location. fscanf(d, "%*2")shows the same over-read without crashing (https://godbolt.org/z/snW3oGzP7): it warnsformat string contains '\0' within the string body.fscanf(d, "%*"),fscanf(d, "%2")andfscanf(d, "a" "%2")correctly getincomplete format specifier.
Root cause
- In
ParseScanfSpecifier(ScanfFormatString.cpp), theI == Echeck after the field width only runs ifParseAmountdid not returnNotSpecified. ParseAmount(FormatString.cpp) returnsNotSpecifiedwhen the digits run to the end of the string, after it has already advancedItoE.- So for a format string ending in
%*<digits>the check is skipped, andParseLengthModifier, the*I == '\0'test and the conversion switch all read*E. - If that byte is not NUL (here
'6'), it is consumed as an invalid conversion specifier andIends up atE + 1. Thewhile (I != E)loop inParseScanfStringthen keeps scanning past the literal until it finds a NUL, andHandleNullCharasksgetLocationOfBytefor an offset beyond the literal.
Without *, ParseArgPosition checks I == E unconditionally, which is why %2 is diagnosed.
Suggested fix (untested)
Do the I == E check after the width unconditionally, or have ParseAmount return Constant when it stops at E after digits.
Details
- Classification: crash on valid code. The file is a valid translation unit (the incomplete specifier only matters at run time); GCC 16.2 accepts it and with
-Wallonly warnsconversion lacks type at end of format. - History: not a regression. The
%*2over-read already shows in clang 3.4. The reproducer asserts on every assertions build tried from 10.0 to trunk; 3.4 and 3.9 read a NUL there and only warn. The stray byte differs between builds, so on another build the reproducer may need a different string to crash. Not bisected. - Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29); release trunk f4085066128a gives only the bogus warning.
- Related: #102218 (fixed in 2024; #130939 was closed as its duplicate) also reached
getLocationOfBytefromHandleNullChar, but through an invalid escape sequence in a printf string ("\o{}"), not the scanf field width. - Found by a fuzzer (generated C program), reduced by hand.
@llvm/issue-subscribers-clang-frontend
Author: skaeads12
Reproducer
https://godbolt.org/z/PqvoPoEq7 (assertions trunk and release trunk)
#include <stdio.h>
void f(FILE *d) { fscanf(d, "a" "%*2", 0); }
clang --target=x86_64-unknown-linux-gnu -fsyntax-only repro.c
<source>:2:37: warning: invalid conversion specifier '6' [-Wformat-invalid-specifier]
clang: clang/lib/AST/Expr.cpp:1357: clang::SourceLocation clang::StringLiteral::getLocationOfByte(unsigned int, const clang::SourceManager&, const clang::LangOptions&, const clang::TargetInfo&, unsigned int*, unsigned int*) const: Assertion `TokNo < getNumConcatenated() && "Invalid byte number!"' failed.
#<!-- -->10 clang::StringLiteral::getLocationOfByte(...) const
#<!-- -->11 (anonymous namespace)::CheckFormatHandler::getLocationOfByte(char const*)
#<!-- -->12 (anonymous namespace)::CheckFormatHandler::HandleNullChar(char const*)
#<!-- -->13 clang::analyze_format_string::ParseScanfString(...)
- There is no
'6'in the format string; it is a byte from past the end of the literal. - Release builds do not crash, but print a second, bogus
warning: format string contains '\0' within the string body [-Wformat]with no source location. fscanf(d, "%*2")shows the same over-read without crashing (https://godbolt.org/z/snW3oGzP7): it warnsformat string contains '\0' within the string body.fscanf(d, "%*"),fscanf(d, "%2")andfscanf(d, "a" "%2")correctly getincomplete format specifier.
Root cause
- In
ParseScanfSpecifier(ScanfFormatString.cpp), theI == Echeck after the field width only runs ifParseAmountdid not returnNotSpecified. ParseAmount(FormatString.cpp) returnsNotSpecifiedwhen the digits run to the end of the string, after it has already advancedItoE.- So for a format string ending in
%*<digits>the check is skipped, andParseLengthModifier, the*I == '\0'test and the conversion switch all read*E. - If that byte is not NUL (here
'6'), it is consumed as an invalid conversion specifier andIends up atE + 1. Thewhile (I != E)loop inParseScanfStringthen keeps scanning past the literal until it finds a NUL, andHandleNullCharasksgetLocationOfBytefor an offset beyond the literal.
Without *, ParseArgPosition checks I == E unconditionally, which is why %2 is diagnosed.
Suggested fix (untested)
Do the I == E check after the width unconditionally, or have ParseAmount return Constant when it stops at E after digits.
Details
- Classification: crash on valid code. The file is a valid translation unit (the incomplete specifier only matters at run time); GCC 16.2 accepts it and with
-Wallonly warnsconversion lacks type at end of format. - History: not a regression. The
%*2over-read already shows in clang 3.4. The reproducer asserts on every assertions build tried from 10.0 to trunk; 3.4 and 3.9 read a NUL there and only warn. The stray byte differs between builds, so on another build the reproducer may need a different string to crash. Not bisected. - Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29); release trunk f4085066128a gives only the bogus warning.
- Related: #102218 (fixed in 2024; #130939 was closed as its duplicate) also reached
getLocationOfBytefromHandleNullChar, but through an invalid escape sequence in a printf string ("\o{}"), not the scanf field width. - Found by a fuzzer (generated C program), reduced by hand.