]> granicus.if.org Git - clang/commitdiff
[Sema] Issue a warning for integer overflow in struct initializer
authorAkira Hatanaka <ahatanaka@apple.com>
Mon, 11 Jan 2016 17:22:01 +0000 (17:22 +0000)
committerAkira Hatanaka <ahatanaka@apple.com>
Mon, 11 Jan 2016 17:22:01 +0000 (17:22 +0000)
Clang wasn't issuing a warning when compiling the following code:

struct s {
  unsigned x;
} s = {
  .x = 4 * 1024 * 1024 * 1024
};

rdar://problem/23399683

Differential Revision: http://reviews.llvm.org/D15097

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@257357 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/Sema/integer-overflow.c

index 98c9ceb1d65ef93576a22aa753c8225ea84480e5..4680bcd11d078cea7e4ebc8d2e51bcca9b76098b 100644 (file)
@@ -7853,6 +7853,10 @@ void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) {
 void Sema::CheckForIntOverflow (Expr *E) {
   if (isa<BinaryOperator>(E->IgnoreParenCasts()))
     E->IgnoreParenCasts()->EvaluateForOverflow(Context);
+  else if (auto InitList = dyn_cast<InitListExpr>(E))
+    for (Expr *E : InitList->inits())
+      if (isa<BinaryOperator>(E->IgnoreParenCasts()))
+        E->IgnoreParenCasts()->EvaluateForOverflow(Context);
 }
 
 namespace {
index 44fbcd4c818e621283591ca1cf07e6abd7bdbb8f..db5c1f4c7118db3478a328a031a28406183c68af 100644 (file)
@@ -145,3 +145,11 @@ uint64_t check_integer_overflows(int i) {
 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
   return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024)));
 }
+
+struct s {
+  unsigned x;
+  unsigned y;
+} s = {
+  .y = 5,
+  .x = 4 * 1024 * 1024 * 1024  // expected-warning {{overflow in expression; result is 0 with type 'int'}}
+};