[clang] b776e2a - [clang] detect integer overflow through temporary values

Lucile Nihlen via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 30 16:32:49 PDT 2023


Author: Lucile Nihlen
Date: 2023-06-30T23:19:04Z
New Revision: b776e2a0b03e93c45deb92f36a391d457ae5b43f

URL: https://github.com/llvm/llvm-project/commit/b776e2a0b03e93c45deb92f36a391d457ae5b43f
DIFF: https://github.com/llvm/llvm-project/commit/b776e2a0b03e93c45deb92f36a391d457ae5b43f.diff

LOG: [clang] detect integer overflow through temporary values

Fixes #63629.

Differential Revision: https://reviews.llvm.org/D154253

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaChecking.cpp
    clang/test/SemaCXX/integer-overflow.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4a9a0305b7e140..bea609cdc08695 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -561,6 +561,8 @@ Bug Fixes in This Version
   when an immediate invocation appears as a part of an expression that produces
   temporaries.
   (`#60709 <https://github.com/llvm/llvm-project/issues/60709>`_).
+- Fixed a missed integer overflow warning with temporary values.
+  (`#63629 <https://github.com/llvm/llvm-project/issues/63629>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index ffda87ac87d7a2..09ce790f7b5ca1 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -15212,6 +15212,8 @@ void Sema::CheckForIntOverflow (Expr *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 Temporary = dyn_cast<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))

diff  --git a/clang/test/SemaCXX/integer-overflow.cpp b/clang/test/SemaCXX/integer-overflow.cpp
index 870fbeb73cc1f1..0e8ad050aa14d8 100644
--- a/clang/test/SemaCXX/integer-overflow.cpp
+++ b/clang/test/SemaCXX/integer-overflow.cpp
@@ -214,3 +214,32 @@ void f() {
   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


        


More information about the cfe-commits mailing list