From 1c150a0d0d02b6541f3ac4857af9923ee8195938 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Fri, 15 Mar 2013 17:03:56 +0000 Subject: [PATCH] c: Also chek for integer overflow for '%' operator. llvm-svn: 177163 --- clang/lib/Sema/SemaChecking.cpp | 12 ++++++++++-- clang/test/Sema/switch-1.c | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 3926f01..2f8d857 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -5188,8 +5188,16 @@ void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { void Sema::CheckForIntOverflow (Expr *E) { if (const BinaryOperator *BExpr = dyn_cast(E->IgnoreParens())) { unsigned Opc = BExpr->getOpcode(); - if (Opc != BO_Add && Opc != BO_Sub && Opc != BO_Mul && Opc != BO_Div) - return; + switch (Opc) { + case BO_Add: + case BO_Sub: + case BO_Mul: + case BO_Div: + case BO_Rem: + break; + default: + return; + } llvm::SmallVector Diags; E->EvaluateForOverflow(Context, &Diags); } diff --git a/clang/test/Sema/switch-1.c b/clang/test/Sema/switch-1.c index 944048d..ce1e7dc9 100644 --- a/clang/test/Sema/switch-1.c +++ b/clang/test/Sema/switch-1.c @@ -12,6 +12,7 @@ int f(int i) { case (123456 *789012) + 1: // expected-warning {{overflow in expression; result is -1375982336 with type 'int'}} return 3; case (2147483647*4)/4: // expected-warning {{overflow in expression; result is -4 with type 'int'}} + case (2147483647*4)%4: // expected-warning {{overflow in expression; result is -4 with type 'int'}} return 4; case 2147483647: return 0; -- 2.7.4