llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang][Sema] scanf format check reads past the end of a string ending in "%*2"; asserts "Invalid byte number!"

Open skaeads12 opened this issue 2 months ago • 1 comments

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 warns format string contains '\0' within the string body. fscanf(d, "%*"), fscanf(d, "%2") and fscanf(d, "a" "%2") correctly get incomplete format specifier.

Root cause

  1. In ParseScanfSpecifier (ScanfFormatString.cpp), the I == E check after the field width only runs if ParseAmount did not return NotSpecified.
  2. ParseAmount (FormatString.cpp) returns NotSpecified when the digits run to the end of the string, after it has already advanced I to E.
  3. So for a format string ending in %*<digits> the check is skipped, and ParseLengthModifier, the *I == '\0' test and the conversion switch all read *E.
  4. If that byte is not NUL (here '6'), it is consumed as an invalid conversion specifier and I ends up at E + 1. The while (I != E) loop in ParseScanfString then keeps scanning past the literal until it finds a NUL, and HandleNullChar asks getLocationOfByte for 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 -Wall only warns conversion lacks type at end of format.
  • History: not a regression. The %*2 over-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 getLocationOfByte from HandleNullChar, 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.

skaeads12 avatar Sep 30 '26 09:09 skaeads12

@llvm/issue-subscribers-clang-frontend

Author: skaeads12

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 &lt;stdio.h&gt;
void f(FILE *d) { fscanf(d, "a" "%*2", 0); }

clang --target=x86_64-unknown-linux-gnu -fsyntax-only repro.c

&lt;source&gt;: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&amp;, const clang::LangOptions&amp;, const clang::TargetInfo&amp;, unsigned int*, unsigned int*) const: Assertion `TokNo &lt; getNumConcatenated() &amp;&amp; "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 warns format string contains '\0' within the string body. fscanf(d, "%*"), fscanf(d, "%2") and fscanf(d, "a" "%2") correctly get incomplete format specifier.

Root cause

  1. In ParseScanfSpecifier (ScanfFormatString.cpp), the I == E check after the field width only runs if ParseAmount did not return NotSpecified.
  2. ParseAmount (FormatString.cpp) returns NotSpecified when the digits run to the end of the string, after it has already advanced I to E.
  3. So for a format string ending in %*&lt;digits&gt; the check is skipped, and ParseLengthModifier, the *I == '\0' test and the conversion switch all read *E.
  4. If that byte is not NUL (here '6'), it is consumed as an invalid conversion specifier and I ends up at E + 1. The while (I != E) loop in ParseScanfString then keeps scanning past the literal until it finds a NUL, and HandleNullChar asks getLocationOfByte for 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 -Wall only warns conversion lacks type at end of format.
  • History: not a regression. The %*2 over-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 getLocationOfByte from HandleNullChar, 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.