llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: TPPPP (TPPPP72)

<details>
<summary>Changes</summary>

I added an extra check in handleBlocksAttr to ensure that illegal Decl values 
​​are not passed to downstream functions.

Fixes #<!-- -->183974

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


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+1) 
- (modified) clang/lib/Sema/SemaObjC.cpp (+7) 
- (added) clang/test/Sema/gh183974.c (+5) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index cb1010aee1edd..ca78fa296e456 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -317,6 +317,7 @@ Bug Fixes to C++ Support
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
 - Fixed a bug where explicit nullability property attributes were not stored 
in AST nodes in Objective-C. (#GH179703)
+- Fixed a crash when __block is used on global variables in C mode. (#GH183974)
 
 Miscellaneous Bug Fixes
 ^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaObjC.cpp b/clang/lib/Sema/SemaObjC.cpp
index dae30b7e941d1..582e3db98b682 100644
--- a/clang/lib/Sema/SemaObjC.cpp
+++ b/clang/lib/Sema/SemaObjC.cpp
@@ -1711,6 +1711,13 @@ void SemaObjC::handleBlocksAttr(Decl *D, const 
ParsedAttr &AL) {
     return;
   }
 
+  VarDecl *VD = dyn_cast<VarDecl>(D);
+  if (!VD || !VD->hasLocalStorage()) {
+    Diag(AL.getLoc(), diag::err_block_on_nonlocal) << AL;
+    D->setInvalidDecl();
+    return;
+  }
+
   D->addAttr(::new (getASTContext()) BlocksAttr(getASTContext(), AL, type));
 }
 
diff --git a/clang/test/Sema/gh183974.c b/clang/test/Sema/gh183974.c
new file mode 100644
index 0000000000000..0999dc18a1e03
--- /dev/null
+++ b/clang/test/Sema/gh183974.c
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -fblocks -fsyntax-only -verify %s
+
+__block int x; // expected-error {{__block attribute not allowed, only allowed 
on local variables}}
+
+int x[0];
\ No newline at end of file

``````````

</details>


https://github.com/llvm/llvm-project/pull/183988
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to