]> granicus.if.org Git - clang/commitdiff
Sema: Emit a warning for non-null terminated format strings and other pathological...
authorBenjamin Kramer <benny.kra@googlemail.com>
Thu, 20 Feb 2014 17:05:38 +0000 (17:05 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Thu, 20 Feb 2014 17:05:38 +0000 (17:05 +0000)
PR18905.

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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaChecking.cpp
test/Sema/format-strings.c

index a2415f7a956f5911ed81bf7957c659a36d3e9a3a..62ae84595cb5a40170159a2f59ee290f5b45871b 100644 (file)
@@ -6223,6 +6223,8 @@ def warn_format_string_is_wide_literal : Warning<
   "format string should not be a wide string">, InGroup<Format>;
 def warn_printf_format_string_contains_null_char : Warning<
   "format string contains '\\0' within the string body">, InGroup<Format>;
+def warn_printf_format_string_not_null_terminated : Warning<
+  "format string is not null-terminated">, InGroup<Format>;
 def warn_printf_asterisk_missing_arg : Warning<
   "'%select{*|.*}0' specified field %select{width|precision}0 is missing a matching 'int' argument">,
   InGroup<Format>;
index cfd042f94597123eaba04baf969f7e36c664693e..f3f08dec97fa851a34b9d563d62411cb63cec4c5 100644 (file)
@@ -3493,9 +3493,25 @@ void Sema::CheckFormatString(const StringLiteral *FExpr,
   // Str - The format string.  NOTE: this is NOT null-terminated!
   StringRef StrRef = FExpr->getString();
   const char *Str = StrRef.data();
-  unsigned StrLen = StrRef.size();
+  // Account for cases where the string literal is truncated in a declaration.
+  const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType());
+  assert(T && "String literal not of constant array type!");
+  size_t TypeSize = T->getSize().getZExtValue();
+  size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size());
   const unsigned numDataArgs = Args.size() - firstDataArg;
-  
+
+  // Emit a warning if the string literal is truncated and does not contain an
+  // embedded null character.
+  if (TypeSize <= StrRef.size() &&
+      StrRef.substr(0, TypeSize).find('\0') == StringRef::npos) {
+    CheckFormatHandler::EmitFormatDiagnostic(
+        *this, inFunctionCall, Args[format_idx],
+        PDiag(diag::warn_printf_format_string_not_null_terminated),
+        FExpr->getLocStart(),
+        /*IsStringLocation=*/true, OrigFormatExpr->getSourceRange());
+    return;
+  }
+
   // CHECK: empty format string?
   if (StrLen == 0 && numDataArgs > 0) {
     CheckFormatHandler::EmitFormatDiagnostic(
index 6ed665c700390c99ae949eac1397f6503c3d8d0a..ad7b37c3e11be2a571e42e83eb2b6906c06dd35c 100644 (file)
@@ -535,6 +535,21 @@ void pr9751() {
          0.0); // expected-warning{{format specifies}}
 }
 
+void pr18905() {
+  const char s1[] = "s\0%s"; // expected-note{{format string is defined here}}
+  const char s2[1] = "s"; // expected-note{{format string is defined here}}
+  const char s3[2] = "s\0%s"; // expected-warning{{initializer-string for char array is too long}}
+  const char s4[10] = "s";
+  const char s5[0] = "%s"; // expected-warning{{initializer-string for char array is too long}}
+                           // expected-note@-1{{format string is defined here}}
+
+  printf(s1); // expected-warning{{format string contains '\0' within the string body}}
+  printf(s2); // expected-warning{{format string is not null-terminated}}
+  printf(s3); // no-warning
+  printf(s4); // no-warning
+  printf(s5); // expected-warning{{format string is not null-terminated}}
+}
+
 void __attribute__((format(strfmon,1,2))) monformat(const char *fmt, ...);
 void __attribute__((format(strftime,1,0))) dateformat(const char *fmt);