]> granicus.if.org Git - clang/commitdiff
Fix <rdar://problem/6880975> [format string] Assertion failed: (Arg < NumArgs &&...
authorTed Kremenek <kremenek@apple.com>
Wed, 13 May 2009 16:06:05 +0000 (16:06 +0000)
committerTed Kremenek <kremenek@apple.com>
Wed, 13 May 2009 16:06:05 +0000 (16:06 +0000)
For format string checking, only check the type of the format
specifier for non-vararg functions.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@71672 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 67d9a1acbf6d876e8d2b42374c25b6b0daa6517c..22dcc49b55458556ff350fb0eb04f686cf33d9ac 100644 (file)
@@ -917,36 +917,38 @@ void Sema::CheckPrintfString(const StringLiteral *FExpr,
     case '*': {
       ++numConversions;
       
-      if (!HasVAListArg && numConversions > numDataArgs) {
+      if (!HasVAListArg) {
+        if (numConversions > numDataArgs) {
+          SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, StrIdx);
+
+          if (Str[StrIdx-1] == '.')
+            Diag(Loc, diag::warn_printf_asterisk_precision_missing_arg)
+              << OrigFormatExpr->getSourceRange();
+          else
+            Diag(Loc, diag::warn_printf_asterisk_width_missing_arg)
+              << OrigFormatExpr->getSourceRange();
+          
+          // Don't do any more checking.  We'll just emit spurious errors.
+          return;
+        }
+      
+        // Perform type checking on width/precision specifier.
+        const Expr *E = TheCall->getArg(format_idx+numConversions);
+        if (const BuiltinType *BT = E->getType()->getAsBuiltinType())
+          if (BT->getKind() == BuiltinType::Int)
+            break;
+        
         SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, StrIdx);
-
+        
         if (Str[StrIdx-1] == '.')
-          Diag(Loc, diag::warn_printf_asterisk_precision_missing_arg)
-            << OrigFormatExpr->getSourceRange();
+          Diag(Loc, diag::warn_printf_asterisk_precision_wrong_type)
+          << E->getType() << E->getSourceRange();
         else
-          Diag(Loc, diag::warn_printf_asterisk_width_missing_arg)
-            << OrigFormatExpr->getSourceRange();
+          Diag(Loc, diag::warn_printf_asterisk_width_wrong_type)
+          << E->getType() << E->getSourceRange();
         
-        // Don't do any more checking.  We'll just emit spurious errors.
-        return;
+        break;        
       }
-      
-      // Perform type checking on width/precision specifier.
-      const Expr *E = TheCall->getArg(format_idx+numConversions);
-      if (const BuiltinType *BT = E->getType()->getAsBuiltinType())
-        if (BT->getKind() == BuiltinType::Int)
-          break;
-
-      SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, StrIdx);
-      
-      if (Str[StrIdx-1] == '.')
-        Diag(Loc, diag::warn_printf_asterisk_precision_wrong_type)
-          << E->getType() << E->getSourceRange();
-      else
-        Diag(Loc, diag::warn_printf_asterisk_width_wrong_type)
-          << E->getType() << E->getSourceRange();
-      
-      break;
     }
       
     // Characters which can terminate a format conversion
index 50903b0cf83673e0c08bfefaca3da1fed12a1b34..1826c7457e3071c213a7c5cebb699282bff62ca5 100644 (file)
@@ -125,3 +125,8 @@ void test9(char *P) {
   printf(P, 42);
   printf("%n", &x); // expected-warning {{use of '%n' in format string discouraged }}
 }
+
+void torture(va_list v8) {
+  vprintf ("%*.*d", v8);  // no-warning
+}
+