[PATCH] D159279: [clang] Emit `Wformat` for bool value and char specifier confusion in scanf

Mariya Podchishchaeva via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 31 06:10:33 PDT 2023


Fznamznon created this revision.
Herald added a project: All.
Fznamznon requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Fixes https://github.com/llvm/llvm-project/issues/64987


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159279

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/AST/FormatString.cpp
  clang/test/SemaCXX/format-strings-scanf.cpp


Index: clang/test/SemaCXX/format-strings-scanf.cpp
===================================================================
--- clang/test/SemaCXX/format-strings-scanf.cpp
+++ clang/test/SemaCXX/format-strings-scanf.cpp
@@ -29,6 +29,8 @@
 
 void test(void) {
     bag b;
+    // expected-warning at +2 {{format specifies type 'char *' but the argument has type 'bool *'}}
+    // expected-warning at +1 {{format specifies type 'unsigned char *' but the argument has type 'bool *'}}
     scan("%hhi %hhu %hhi %hhu", &b.sc, &b.uc, &b.b, &b.b);
     scan("%hi %hu", &b.ss, &b.us);
     scan("%i %u", &b.si, &b.ui);
Index: clang/lib/AST/FormatString.cpp
===================================================================
--- clang/lib/AST/FormatString.cpp
+++ clang/lib/AST/FormatString.cpp
@@ -368,8 +368,11 @@
         case BuiltinType::SChar:
         case BuiltinType::UChar:
         case BuiltinType::Char_U:
+            return Match;
         case BuiltinType::Bool:
-          return Match;
+          if (!Ptr)
+            return Match;
+          break;
         }
         // "Partially matched" because of promotions?
         if (!Ptr) {
@@ -410,11 +413,14 @@
         switch (BT->getKind()) {
           default:
             break;
+          case BuiltinType::Bool:
+            if (Ptr && (T == C.UnsignedCharTy || T == C.SignedCharTy))
+              return NoMatch;
+            [[fallthrough]];
           case BuiltinType::Char_S:
           case BuiltinType::SChar:
           case BuiltinType::Char_U:
           case BuiltinType::UChar:
-          case BuiltinType::Bool:
             if (T == C.UnsignedShortTy || T == C.ShortTy)
               return NoMatchTypeConfusion;
             if (T == C.UnsignedCharTy || T == C.SignedCharTy)
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -208,6 +208,9 @@
 - For function multi-versioning using the ``target`` or ``target_clones``
   attributes, remove comdat for internal linkage functions.
   (`#65114 <https://github.com/llvm/llvm-project/issues/65114>`_)
+- Clang now reports ``-Wformat`` for bool value and char specifier confusion
+  in scanf. Fixes
+  (`#64987 <https://github.com/llvm/llvm-project/issues/64987>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159279.555009.patch
Type: text/x-patch
Size: 2370 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230831/b0b521bb/attachment-0001.bin>


More information about the cfe-commits mailing list