[-Wunsafe-buffer-usage] Suppress an assertion for visiting VarDecl twice.
authorArtem Dergachev <adergachev@apple.com>
Tue, 20 Dec 2022 22:58:35 +0000 (14:58 -0800)
committerArtem Dergachev <adergachev@apple.com>
Wed, 21 Dec 2022 00:05:13 +0000 (16:05 -0800)
The assertion doesn't seem to hold due to ASTMatchers traversing code
inside GNU StmtExpr twice. This can screw up our algorithm's invariants.
We need a further investigation to properly fix this issue, but for now
let's avoid the crash.

clang/lib/Analysis/UnsafeBufferUsage.cpp
clang/test/SemaCXX/warn-unsafe-buffer-usage-crashes.c [new file with mode: 0644]

index 85449c4..88d6ca3 100644 (file)
@@ -245,7 +245,11 @@ public:
   void discoverDecl(const DeclStmt *DS) {
     for (const Decl *D : DS->decls()) {
       if (const auto *VD = dyn_cast<VarDecl>(D)) {
-        assert(Defs.count(VD) == 0 && "Definition already discovered!");
+        // FIXME: Assertion temporarily disabled due to a bug in
+        // ASTMatcher internal behavior in presence of GNU
+        // statement-expressions. We need to properly investigate this
+        // because it can screw up our algorithm in other ways.
+        // assert(Defs.count(VD) == 0 && "Definition already discovered!");
         Defs[VD] = DS;
       }
     }
diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-crashes.c b/clang/test/SemaCXX/warn-unsafe-buffer-usage-crashes.c
new file mode 100644 (file)
index 0000000..225e67f
--- /dev/null
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -Wunsafe-buffer-usage %s -verify %s
+
+void gnu_stmtexpr_crash(void) {
+  struct A {};
+  struct B {
+    struct A a;
+  };
+
+  struct B b = {{
+    // This is a statement-expression (GNU extension).
+    ({ int x; }) // no-crash // expected-warning{{excess elements in struct initializer}}
+  }};
+}