[clang] 9332ddf - [Clang] Extend the number of case Sema::CheckForIntOverflow covers
Shafik Yaghmour via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 15 12:07:10 PST 2022
Author: Shafik Yaghmour
Date: 2022-11-15T12:07:03-08:00
New Revision: 9332ddfba69c38f9ceef4175b042fba0bb8e67bb
URL: https://github.com/llvm/llvm-project/commit/9332ddfba69c38f9ceef4175b042fba0bb8e67bb
DIFF: https://github.com/llvm/llvm-project/commit/9332ddfba69c38f9ceef4175b042fba0bb8e67bb.diff
LOG: [Clang] Extend the number of case Sema::CheckForIntOverflow covers
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
Differential Revision: https://reviews.llvm.org/D137897
Added:
clang/test/Sema/integer-overflow.cpp
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaChecking.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 89f1548d86b02..8e32a69f69353 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -388,6 +388,8 @@ Improvements to Clang's diagnostics
- Clang now diagnoses use of invalid or reserved module names in a module
export declaration. Both are diagnosed as an error, but the diagnostic is
suppressed for use of reserved names in a system header.
+- ``-Winteger-overflow`` will diagnose overflow in more cases. This fixes
+ `Issue 58944 <https://github.com/llvm/llvm-project/issues/58944>`_.
Non-comprehensive list of changes in this release
-------------------------------------------------
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index f46a4d3efc64a..5a9c1750d8b32 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -14660,6 +14660,17 @@ void Sema::CheckForIntOverflow (Expr *E) {
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());
}
diff --git a/clang/test/Sema/integer-overflow.cpp b/clang/test/Sema/integer-overflow.cpp
new file mode 100644
index 0000000000000..be9397e3d170e
--- /dev/null
+++ b/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'}}
+}
+}
More information about the cfe-commits
mailing list