From patchwork Fri May 10 21:00:35 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Wang X-Patchwork-Id: 2552191 Return-Path: X-Original-To: patchwork-linux-sparse@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id EDE293FC5A for ; Fri, 10 May 2013 21:02:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754701Ab3EJVCA (ORCPT ); Fri, 10 May 2013 17:02:00 -0400 Received: from mail-vb0-f47.google.com ([209.85.212.47]:36432 "EHLO mail-vb0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754678Ab3EJVCA (ORCPT ); Fri, 10 May 2013 17:02:00 -0400 Received: by mail-vb0-f47.google.com with SMTP id x14so3768876vbb.6 for ; Fri, 10 May 2013 14:01:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer; bh=pXU3e8bv+y2vJH/8GyAUY2YmAAd18s3q8FARg6S4mfM=; b=yLjGgkKeAU/Si6Yo+WkgwRHQqIKULKFL2QMFj1W0rW07r3a1czgrccs0uuk33A5kV9 oNdsRDMf5wJvs8DPyH2QBhHLtF3p2m6MpgPuhSD/BDiP7tZ1JovjgsqdFvCPmLa99AkL 9B2nYGMd2HeHtqop1AYAkT/iGHqgx5f6nmm5hDo5xVgU3rSRD0NNFMIMa/hWGDiVv0VL UdZ3a5FhZG/v3nqIOzUTn8d9MajWdG8x3b0RAEUylgcD3VnasaAoqvJ+rVbhqt5q1cns i0pFmBnxwEnzBFxlJCDFiFe7sZsfUjWZlwu23PF+oExR4uCHsfSQ67O2FtokNAymxlbc GL/g== X-Received: by 10.220.186.137 with SMTP id cs9mr12320916vcb.6.1368219719227; Fri, 10 May 2013 14:01:59 -0700 (PDT) Received: from hchen.csail.mit.edu (hchen.csail.mit.edu. [18.26.5.5]) by mx.google.com with ESMTPSA id v19sm3531830vek.3.2013.05.10.14.01.57 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 10 May 2013 14:01:58 -0700 (PDT) From: Xi Wang To: sparse@chrisli.org Cc: linux-sparse@vger.kernel.org, Xi Wang Subject: [PATCH] fix SIGFPE caused by signed division overflow Date: Fri, 10 May 2013 17:00:35 -0400 Message-Id: <1368219635-4524-1-git-send-email-xi.wang@gmail.com> X-Mailer: git-send-email 1.8.1.2 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Avoid evaluating INT_MIN / -1 and INT_MIN % -1, which will trap on x86 and crash sparse. Signed-off-by: Xi Wang --- expand.c | 2 ++ simplify.c | 4 ++++ validation/div.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 validation/div.c diff --git a/expand.c b/expand.c index effd27b..2dfa5e5 100644 --- a/expand.c +++ b/expand.c @@ -239,6 +239,8 @@ static int simplify_int_binop(struct expression *expr, struct symbol *ctype) case SIGNED('%'): if (!r) goto Div; + if (l == mask && sr == -1) + goto Overflow; v = sl % sr; break; diff --git a/simplify.c b/simplify.c index bda4a5b..b5cd0ea 100644 --- a/simplify.c +++ b/simplify.c @@ -406,6 +406,8 @@ static int simplify_constant_binop(struct instruction *insn) case OP_DIVS: if (!right) return 0; + if (left == mask && right == -1) + return 0; res = left / right; break; case OP_MODU: @@ -416,6 +418,8 @@ static int simplify_constant_binop(struct instruction *insn) case OP_MODS: if (!right) return 0; + if (left == mask && right == -1) + return 0; res = left % right; break; case OP_SHL: diff --git a/validation/div.c b/validation/div.c new file mode 100644 index 0000000..3dcbfd5 --- /dev/null +++ b/validation/div.c @@ -0,0 +1,29 @@ +#include + +static int xd = 1 / 0; +static int xl = 1L / 0; +static int xll = 1LL / 0; + +static int yd = INT_MIN / -1; +static long yl = LONG_MIN / -1; +static long long yll = LLONG_MIN / -1; + +static int zd = INT_MIN % -1; +static long zl = LONG_MIN % -1; +static long long zll = LLONG_MIN % -1; + +/* + * check-name: division constants + * + * check-error-start +div.c:3:19: warning: division by zero +div.c:4:20: warning: division by zero +div.c:5:22: warning: division by zero +div.c:7:25: warning: constant integer operation overflow +div.c:8:27: warning: constant integer operation overflow +div.c:9:34: warning: constant integer operation overflow +div.c:11:25: warning: constant integer operation overflow +div.c:12:27: warning: constant integer operation overflow +div.c:13:34: warning: constant integer operation overflow + * check-error-end + */