[clang] Fix crash when an attribute is applied to pragma attribute/pragma dump (PR #137880)

via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 29 14:36:31 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Erich Keane (erichkeane)

<details>
<summary>Changes</summary>

These two don't result in a statement, so the attempt to apply the attributes to them was crashing.  This patch correctly prohibits the use of attributes on these clauses.

Fixes: #<!-- -->137861

---
Full diff: https://github.com/llvm/llvm-project/pull/137880.diff


2 Files Affected:

- (modified) clang/lib/Parse/ParseStmt.cpp (+4) 
- (added) clang/test/Parser/gh137861.cpp (+33) 


``````````diff
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index 4e801f4ef890f..97924f093240f 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -526,10 +526,14 @@ StmtResult Parser::ParseStatementOrDeclarationAfterAttributes(
     return ParsePragmaLoopHint(Stmts, StmtCtx, TrailingElseLoc, CXX11Attrs);
 
   case tok::annot_pragma_dump:
+    ProhibitAttributes(CXX11Attrs);
+    ProhibitAttributes(GNUAttrs);
     HandlePragmaDump();
     return StmtEmpty();
 
   case tok::annot_pragma_attribute:
+    ProhibitAttributes(CXX11Attrs);
+    ProhibitAttributes(GNUAttrs);
     HandlePragmaAttribute();
     return StmtEmpty();
   }
diff --git a/clang/test/Parser/gh137861.cpp b/clang/test/Parser/gh137861.cpp
new file mode 100644
index 0000000000000..9354aef919650
--- /dev/null
+++ b/clang/test/Parser/gh137861.cpp
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 %s -verify 
+
+void foo() {
+  // expected-error at +1{{an attribute list cannot appear here}}
+__attribute__((aligned(64)))
+#pragma clang attribute push(__attribute__((uninitialized)), apply_to = any(variable(is_local)))
+{
+  int f;
+}
+#pragma clang attribute pop
+}
+
+void foo2() {
+  // expected-error at +1{{an attribute list cannot appear here}}
+__attribute__((aligned(64)))
+#pragma clang __debug dump foo
+}
+
+void foo3() {
+  // expected-error at +1{{an attribute list cannot appear here}}
+  [[nodiscard]]
+#pragma clang attribute push(__attribute__((uninitialized)), apply_to = any(variable(is_local)))
+{
+  int f;
+}
+#pragma clang attribute pop
+}
+
+void foo4() {
+  // expected-error at +1{{an attribute list cannot appear here}}
+  [[nodiscard]]
+#pragma clang __debug dump foo
+}

``````````

</details>


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


More information about the cfe-commits mailing list