[PATCH] D52750: [Diagnostics] Check for integer overflow in array size expressions

Dávid Bolvanský via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 1 15:07:37 PDT 2018


xbolva00 updated this revision to Diff 167840.
xbolva00 added a comment.

- Move code as suggested


https://reviews.llvm.org/D52750

Files:
  lib/Sema/SemaType.cpp
  test/Sema/integer-overflow.c


Index: test/Sema/integer-overflow.c
===================================================================
--- test/Sema/integer-overflow.c
+++ test/Sema/integer-overflow.c
@@ -172,6 +172,9 @@
 // expected-warning at +1 {{overflow in expression; result is 536870912 with type 'int'}}
   (void)f2(0, f0(4608 * 1024 * 1024));
 }
+void check_integer_overflows_in_array_size() {
+  int arr[4608 * 1024 * 1024]; // expected-warning {{overflow in expression; result is 536870912 with type 'int'}}
+}
 
 struct s {
   unsigned x;
Index: lib/Sema/SemaType.cpp
===================================================================
--- lib/Sema/SemaType.cpp
+++ lib/Sema/SemaType.cpp
@@ -2044,7 +2044,10 @@
 
 /// Check whether the specified array size makes the array type a VLA.  If so,
 /// return true, if not, return the size of the array in SizeVal.
-static bool isArraySizeVLA(Sema &S, Expr *ArraySize, llvm::APSInt &SizeVal) {
+static bool isArraySizeVLA(Sema &S, Expr *ArraySize, llvm::APSInt &SizeVal,
+                           const ASTContext &Context) {
+  if (isa<BinaryOperator>(ArraySize))
+    ArraySize->EvaluateForOverflow(Context);
   // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode
   // (like gnu99, but not c99) accept any evaluatable value as an extension.
   class VLADiagnoser : public Sema::VerifyICEDiagnoser {
@@ -2178,7 +2181,7 @@
     T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
   } else if ((!T->isDependentType() && !T->isIncompleteType() &&
               !T->isConstantSizeType()) ||
-             isArraySizeVLA(*this, ArraySize, ConstVal)) {
+             isArraySizeVLA(*this, ArraySize, ConstVal, Context)) {
     // Even in C++11, don't allow contextual conversions in the array bound
     // of a VLA.
     if (getLangOpts().CPlusPlus11 &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52750.167840.patch
Type: text/x-patch
Size: 1825 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181001/7ca85c70/attachment.bin>


More information about the cfe-commits mailing list