Skip to content

Commit

Permalink
[Sema] Issue a warning for integer overflow in struct initializer
Browse files Browse the repository at this point in the history
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

llvm-svn: 257357
  • Loading branch information
ahatanaka committed Jan 11, 2016
1 parent 7af0562 commit f5c1361
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions clang/lib/Sema/SemaChecking.cpp
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions clang/test/Sema/integer-overflow.c
Expand Up @@ -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'}}
};

0 comments on commit f5c1361

Please sign in to comment.