[clang] [clang][Sema] Stop format size estimator upon %p to adapt to linux kernel's extension (PR #65969)

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 12 18:35:54 PDT 2023


================
@@ -851,6 +851,50 @@ class ScanfDiagnosticFormatHandler
   }
 };
 
+/// `I` points to the next character of `%p` format.
+/// This functon checks if the subsequent character can be linux kernel's
+/// extnded format specifier
+static inline constexpr bool canBeLinuxFormatExtension(const char *I,
+                                                       const char *E) {
+  assert(I < E && "format string not yet exhausted");
+  // Kernel Document: https://docs.kernel.org/core-api/printk-formats.html
+  switch (*I) {
+  default:
+    return false;
+  case 'S':
+  case 's':
+  case 'B':
+  case 'R':
+  case 'r':
+  case 'h':
+  case 'b':
+  case 'M':
+  case 'm':
+  case 'I':
+  case 'i':
+  case 'E':
+  case 'U':
+  case 'V':
+  case 'K':
+  case 'N':
+  case '4':
+  case 'a':
+  case 'd':
+  case 't':
+  case 'C':
+  case 'D':
+  case 'g':
+  case 'G':
+  case 'O':
+  case 'f':
+  case 'x':
+  case 'e':
+  case 'u':
+  case 'k':
+    return true;
+  }
+}
+
 class EstimateSizeFormatHandler
----------------
zygoloid wrote:

Here's what I'd suggest:

- Add a flag to this class to track whether we've seen any specifiers that the Linux kernel gives unusual behavior to.
- Set the flag after line 933 (`   case analyze_format_string::ConversionSpecifier::pArg:`).
- On line 1251 (`DiagID = diag::warn_fortify_source_format_overflow;`), set `DiagID` to a different value that's in a separate diagnostic group with a different `-W` flag that's nested within the `FortifySource` group.

That'll mean that no-one loses any diagnostic quality, and we have a new flag, say `-Wno-fortify-source-non-kprintf` that the Linux kernel can use to turn off these warnings.

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


More information about the cfe-commits mailing list