llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Shafik Yaghmour (shafik)

<details>
<summary>Changes</summary>

…rator that can not overflow

A while ago I added checking for overflow in unary operators during constant 
evaluation:

https://reviews.llvm.org/D142867

This created some new bug opportunities. I am now checking if the UnaryOperator 
can overflow before calling EvaluateForOverflow in Sema::CheckForIntOverflow.

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

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


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+1) 
- (modified) clang/lib/Sema/SemaChecking.cpp (+2-1) 
- (added) clang/test/Sema/gh170072.c (+8) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 11cce36a0906c..97f7af57bf840 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -648,6 +648,7 @@ Bug Fixes in This Version
   an array via an element-at-a-time copy loop (#GH192026)
 - Fixed an issue where certain designated initializers would be rejected for 
constexpr variables. (#GH193373)
 - Fixed a crash when ``#embed`` is used with C++ modules (#GH195350)
+- Fixed crash when checking for overflow for unary operator that can't 
overflow (#GH170072)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 2cf8221d933fd..345dfb69adbf6 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -14325,7 +14325,8 @@ void Sema::CheckForIntOverflow (const Expr *E) {
     const Expr *OriginalE = Exprs.pop_back_val();
     const Expr *E = OriginalE->IgnoreParenCasts();
 
-    if (isa<BinaryOperator, UnaryOperator>(E)) {
+    if (isa<BinaryOperator>(E) ||
+        (isa<UnaryOperator>(E) && cast<UnaryOperator>(E)->canOverflow())) {
       E->EvaluateForOverflow(Context);
       continue;
     }
diff --git a/clang/test/Sema/gh170072.c b/clang/test/Sema/gh170072.c
new file mode 100644
index 0000000000000..6e880e507fa52
--- /dev/null
+++ b/clang/test/Sema/gh170072.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wno-unused %s
+
+int a[-1]; // expected-error {{declared as an array with a negative size}}
+
+void f() {
+  extern int a[];
+  *a;
+}

``````````

</details>


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

Reply via email to