[PATCH] D137897: Extend the number of case Sema::CheckForIntOverflow covers

Shafik Yaghmour via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 12 12:48:23 PST 2022


shafik created this revision.
shafik added reviewers: aaron.ballman, erichkeane.
Herald added a project: All.
shafik requested review of this revision.

Currently `Sema::CheckForIntOverflow` misses several case that other compilers diagnose for overflow in integral constant expressions. This includes the arguments of a `CXXConstructExpr` as well as the expressions used in an `ArraySubscriptExpr`, `CXXNewExpr` and `CompoundLiteralExpr`.

This fixes https://github.com/llvm/llvm-project/issues/58944


https://reviews.llvm.org/D137897

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/Sema/integer-overflow.cpp


Index: clang/test/Sema/integer-overflow.cpp
===================================================================
--- /dev/null
+++ clang/test/Sema/integer-overflow.cpp
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 %s -Wno-unused-value -verify -fsyntax-only
+
+namespace GH58944 {
+struct A {
+  A(unsigned long) ;
+};
+
+A a(1024 * 1024 * 1024 * 1024* 1024ull); // expected-warning {{overflow in expression; result is 0 with type 'int'}}
+
+void f() {
+  new int[1024 * 1024 * 1024 * 1024* 1024ull]; // expected-warning {{overflow in expression; result is 0 with type 'int'}}
+
+  int arr[]{1,2,3};
+  arr[1024 * 1024 * 1024 * 1024* 1024ull]; // expected-warning {{overflow in expression; result is 0 with type 'int'}}
+
+  (int){1024 * 1024 * 1024 * 1024* 1024}; // expected-warning {{overflow in expression; result is 0 with type 'int'}}
+}
+}
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -14660,6 +14660,17 @@
       Exprs.append(Call->arg_begin(), Call->arg_end());
     else if (auto Message = dyn_cast<ObjCMessageExpr>(E))
       Exprs.append(Message->arg_begin(), Message->arg_end());
+    else if (auto Construct = dyn_cast<CXXConstructExpr>(E))
+      Exprs.append(Construct->arg_begin(), Construct->arg_end());
+    else if (auto Array = dyn_cast<ArraySubscriptExpr>(E))
+      Exprs.push_back(Array->getIdx());
+    else if (auto Compound = dyn_cast<CompoundLiteralExpr>(E))
+      Exprs.push_back(Compound->getInitializer());
+    else if (auto New = dyn_cast<CXXNewExpr>(E)) {
+      if (New->isArray())
+        if (auto ArraySize = New->getArraySize())
+          Exprs.push_back(ArraySize.value());
+    }
   } while (!Exprs.empty());
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137897.474955.patch
Type: text/x-patch
Size: 1783 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221112/079e03e4/attachment.bin>


More information about the cfe-commits mailing list