[clang] [clang] Catch missing format attributes (PR #70024)

Aaron Puchert via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 10 14:06:44 PST 2023


================
@@ -0,0 +1,143 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <uchar.h>
+#include <wchar.h>
+
+__attribute__((__format__ (__scanf__, 1, 4)))
+void f1(char *out, const size_t len, const char *format, ... /* args */)
+{
+    va_list args;
+    vsnprintf(out, len, format, args); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f1'}}
+                                       // CHECK-FIXES: __attribute__((format(printf, 1, 4)))
+}
+
+void f2(char *out, va_list args)
+{
+    vprintf(out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f2'}}
+                        // CHECK-FIXES: __attribute__((format(printf, 1, 0)))
+    vscanf(out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f2'}}
+                       // CHECK-FIXES: __attribute__((format(scanf, 1, 0)))
+}
+
+void f3(char* out, ... /* args */)
+{
+    va_list args;
+    vprintf("test", args); // no warning
+}
+
+void f4(char *out, ... /* args */)
+{
+    const char *ch;
+    va_list args;
+    vscanf(ch, args); // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f4'}}
+                      // CHECK-FIXES: __attribute__((format(scanf, 1, 2)))
----------------
aaronpuchert wrote:

I don't think we can propagate the attribute here, since the format string is a local variable. It doesn't come from the caller.

The `out` parameter seems unused here, and has no relation to the format string `ch`.

https://github.com/llvm/llvm-project/pull/70024


More information about the cfe-commits mailing list