[clang] Fix crash when parsing scanf format string with missing arguments
authorserge-sans-paille <sguelton@redhat.com>
Fri, 2 Sep 2022 11:36:08 +0000 (13:36 +0200)
committerserge-sans-paille <sguelton@redhat.com>
Mon, 5 Sep 2022 08:54:18 +0000 (10:54 +0200)
When parsing a format string with less argument than specified, one should check
argument access because there may be no such argument.

This fixes #57517

Differential Revision: https://reviews.llvm.org/D133197

clang/lib/Sema/SemaChecking.cpp
clang/test/Sema/format-strings-scanf.c

index 897ab70..afe99ad 100644 (file)
@@ -1066,6 +1066,9 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD,
       return llvm::None;
     unsigned NewIndex = *IndexOptional;
 
+    if (NewIndex >= TheCall->getNumArgs())
+      return llvm::None;
+
     const Expr *ObjArg = TheCall->getArg(NewIndex);
     uint64_t Result;
     if (!ObjArg->tryEvaluateObjectSize(Result, getASTContext(), BOSType))
index aebb68c..eb5b8ec 100644 (file)
@@ -69,6 +69,11 @@ void bad_length_modifiers(char *s, void *p, wchar_t *ws, long double *ld) {
   scanf("%#.2Lf", ld); // expected-warning{{invalid conversion specifier '#'}}
 }
 
+void missing_argument_with_length_modifier() {
+  char buf[30];
+  scanf("%s:%900s", buf); // expected-warning{{more '%' conversions than data arguments}}
+}
+
 // Test that the scanf call site is where the warning is attached.  If the
 // format string is somewhere else, point to it in a note.
 void pr9751(void) {