[PATCH] D154253: [clang] detect integer overflow through temporary values

Lucile Rose Nihlen via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 30 12:49:57 PDT 2023


lnihlen created this revision.
Herald added a project: All.
lnihlen requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Fixes GH63629.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154253

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


Index: clang/test/SemaCXX/integer-overflow.cpp
===================================================================
--- clang/test/SemaCXX/integer-overflow.cpp
+++ clang/test/SemaCXX/integer-overflow.cpp
@@ -214,3 +214,32 @@
   int a = -(1<<31); // expected-warning {{overflow in expression; result is -2147483648 with type 'int'}}
 }
 }
+
+#if __cplusplus >= 201103L
+namespace GH63629 {
+typedef long long int64_t;
+
+template<typename T>
+class u_ptr {
+  T *ptr;
+public:
+  u_ptr(const u_ptr&) = delete;
+  u_ptr &operator=(const u_ptr&) = delete;
+  u_ptr(u_ptr &&other) : ptr(other.ptr) { other.ptr = 0; }
+  u_ptr(T *ptr) : ptr(ptr) { }
+  ~u_ptr() { delete ptr; }
+};
+
+u_ptr<bool> Wrap(int64_t x) {
+    return nullptr;
+}
+
+int64_t Pass(int64_t x) { return x; }
+
+int m() {
+    int64_t x = Pass(30 * 24 * 60 * 59 * 1000);  // expected-warning {{overflow in expression; result is -1746167296 with type 'int'}}
+    auto r = Wrap(Pass(30 * 24 * 60 * 59 * 1000));  // expected-warning {{overflow in expression; result is -1746167296 with type 'int'}}
+    return 0;
+}
+}
+#endif
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -15212,6 +15212,8 @@
       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 Temporary = dyn_cast<clang::CXXBindTemporaryExpr>(E))
+      Exprs.push_back(Temporary->getSubExpr());
     else if (auto Array = dyn_cast<ArraySubscriptExpr>(E))
       Exprs.push_back(Array->getIdx());
     else if (auto Compound = dyn_cast<CompoundLiteralExpr>(E))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154253.536379.patch
Type: text/x-patch
Size: 1773 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230630/2db971b3/attachment.bin>


More information about the cfe-commits mailing list