From patchwork Wed May 31 03:22:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 9755719 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id AB6F2602CA for ; Wed, 31 May 2017 03:22:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9E770205F8 for ; Wed, 31 May 2017 03:22:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9392128474; Wed, 31 May 2017 03:22:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2DA19205F8 for ; Wed, 31 May 2017 03:22:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751108AbdEaDWU (ORCPT ); Tue, 30 May 2017 23:22:20 -0400 Received: from mail-wr0-f193.google.com ([209.85.128.193]:34785 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751090AbdEaDWU (ORCPT ); Tue, 30 May 2017 23:22:20 -0400 Received: by mail-wr0-f193.google.com with SMTP id 6so305283wrb.1 for ; Tue, 30 May 2017 20:22:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=bIvz28fMxNsyZh6BMwy1V79/hg9TY9ISFnEbB5k94/c=; b=idNPlNf9PEkf9nKXiZjI0i+T9a5d/O5OHhy1TvpffwEGG+yir+E8acJyQ0fCanboMy LNcY8S5uU6yUeZC9yemydvFXTIKTVPxorbbS/nsD/h8gsfB+pvSLFvOm4kHaK5K6NOSM MhEppVLGrQD0AiebnF3lTAREWSmqeDwg6eph1wz1Tso2XzhgqjNP2SKyXx1ZyQGuHG7b 67pAln/3LVo4c/zrN7OuhohTRN4KYL/6zwIApmk/gctSJJ6Ic7UstwD0mjT9c76nF1n3 +11HmYH3Q6/tTUleWVuURnzSrBwE6lHXrKySRZEwbszHe1n83HsEXH5nlDWTlrNCdig6 GVrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=bIvz28fMxNsyZh6BMwy1V79/hg9TY9ISFnEbB5k94/c=; b=t5gwR0HMRRE3Wx9MF9dQ7ibpO9F/WyhHC4cEpaSNJ68c8+7UjadJtViUZg3g5hUmt1 GDY+x0ioeLCvcyS1knufwJ5cD/mba+s3WlBIWcIThM3aH91caMjUaZvpGdNNA2HegbyU 0prS3nlgWioILhPN639kiXt09d60m4+VuP46ZJpZ08cRErkEk3IHq6QOJGLCfHAXv5va Io372Rv4SHTHiYEdzFq4rtjbpADNKW+Ny0aCWa2w5DlaJS12hYpedTASYbAxm4aczUEk gTEjOEpMdpAUzxu3nn/tnJTe0W1V2BJENHz7nWfOaUAaEHIDsMSAvGFkj6MGpTt551/e FJIA== X-Gm-Message-State: AODbwcB1Wwa1Rg3gZu9hU/+1+0dz8lj/GkUrudfsNwLSBG1391moBWAf 2WbEy10F5oCKU6leUUY= X-Received: by 10.223.166.240 with SMTP id t103mr1415111wrc.52.1496200938421; Tue, 30 May 2017 20:22:18 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:89c:5400:25d0:8511:1e85:9d96]) by smtp.gmail.com with ESMTPSA id y3sm31676991wmh.21.2017.05.30.20.22.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 May 2017 20:22:17 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 3/9] div0: warn on integer divide by 0 also when the lhs is not constant Date: Wed, 31 May 2017 05:22:01 +0200 Message-Id: <20170531032207.95830-4-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20170531032207.95830-1-luc.vanoostenryck@gmail.com> References: <20170531032207.95830-1-luc.vanoostenryck@gmail.com> Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The current code detects and warns on division by zero but only when the left-hand side is a constant value. Fix that by moving up the code which detect such divisions before checking if the LHS is a constant. Signed-off-by: Luc Van Oostenryck --- expand.c | 8 ++------ validation/div-by-zero.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 validation/div-by-zero.c diff --git a/expand.c b/expand.c index 5f908c971..0b528ea5a 100644 --- a/expand.c +++ b/expand.c @@ -181,6 +181,8 @@ static int simplify_int_binop(struct expression *expr, struct symbol *ctype) if (right->type != EXPR_VALUE) return 0; r = right->value; + if (!r && (expr->op == '/' || expr->op == '%')) + goto Div; if (expr->op == SPECIAL_LEFTSHIFT || expr->op == SPECIAL_RIGHTSHIFT) { if (r >= ctype->bit_size) { if (conservative) @@ -235,28 +237,22 @@ static int simplify_int_binop(struct expression *expr, struct symbol *ctype) break; case SIGNED('/'): - if (!r) - goto Div; if (l == mask && sr == -1) goto Overflow; v = sl / sr; break; case UNSIGNED('/'): - if (!r) goto Div; v = l / r; break; case SIGNED('%'): - if (!r) - goto Div; if (l == mask && sr == -1) goto Overflow; v = sl % sr; break; case UNSIGNED('%'): - if (!r) goto Div; v = l % r; break; diff --git a/validation/div-by-zero.c b/validation/div-by-zero.c new file mode 100644 index 000000000..786e48298 --- /dev/null +++ b/validation/div-by-zero.c @@ -0,0 +1,43 @@ +int scdiv(int a) { return 2 / 0; } +int iscdiv(int a) { return 2 / (int) 0; } +int lscdiv(int a) { return 2 / (long) 0; } +int uscdiv(int a) { return 2 / (unsigned int) 0; } + +int svdiv(int a) { return a / 0; } +int isvdiv(int a) { return a / (int) 0; } +int lsvdiv(int a) { return a / (long) 0; } +int usvdiv(int a) { return a / (unsigned int) 0; } + +int scmod(int a) { return 2 % 0; } +int iscmod(int a) { return 2 % (int) 0; } +int lscmod(int a) { return 2 % (long) 0; } +int uscmod(int a) { return 2 % (unsigned int) 0; } + +int svmod(int a) { return a % 0; } +int isvmod(int a) { return a % (int) 0; } +int lsvmod(int a) { return a % (long) 0; } +int usvmod(int a) { return a % (unsigned int) 0; } + +/* + * check-name: div-by-zero.c + * check-command: sparse -Wno-decl $file + * + * check-error-start +div-by-zero.c:1:30: warning: division by zero +div-by-zero.c:2:30: warning: division by zero +div-by-zero.c:3:30: warning: division by zero +div-by-zero.c:4:30: warning: division by zero +div-by-zero.c:6:30: warning: division by zero +div-by-zero.c:7:30: warning: division by zero +div-by-zero.c:8:30: warning: division by zero +div-by-zero.c:9:30: warning: division by zero +div-by-zero.c:11:30: warning: division by zero +div-by-zero.c:12:30: warning: division by zero +div-by-zero.c:13:30: warning: division by zero +div-by-zero.c:14:30: warning: division by zero +div-by-zero.c:16:30: warning: division by zero +div-by-zero.c:17:30: warning: division by zero +div-by-zero.c:18:30: warning: division by zero +div-by-zero.c:19:30: warning: division by zero + * check-error-end + */