From: Akira Hatanaka Date: Mon, 11 Jan 2016 17:22:01 +0000 (+0000) Subject: [Sema] Issue a warning for integer overflow in struct initializer X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6b93d1766157865d50fd3bd11d49aa260dfc9a8c;p=clang [Sema] Issue a warning for integer overflow in struct initializer 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 --- diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 98c9ceb1d6..4680bcd11d 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -7853,6 +7853,10 @@ void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) { void Sema::CheckForIntOverflow (Expr *E) { if (isa(E->IgnoreParenCasts())) E->IgnoreParenCasts()->EvaluateForOverflow(Context); + else if (auto InitList = dyn_cast(E)) + for (Expr *E : InitList->inits()) + if (isa(E->IgnoreParenCasts())) + E->IgnoreParenCasts()->EvaluateForOverflow(Context); } namespace { diff --git a/test/Sema/integer-overflow.c b/test/Sema/integer-overflow.c index 44fbcd4c81..db5c1f4c71 100644 --- a/test/Sema/integer-overflow.c +++ b/test/Sema/integer-overflow.c @@ -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'}} +};