From patchwork Wed May 31 03:22:02 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: 9755721 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 BAE2E602CA for ; Wed, 31 May 2017 03:22:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AD99A205F8 for ; Wed, 31 May 2017 03:22:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A27D128474; Wed, 31 May 2017 03:22:22 +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 43113205F8 for ; Wed, 31 May 2017 03:22:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751109AbdEaDWV (ORCPT ); Tue, 30 May 2017 23:22:21 -0400 Received: from mail-wr0-f194.google.com ([209.85.128.194]:36520 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751088AbdEaDWV (ORCPT ); Tue, 30 May 2017 23:22:21 -0400 Received: by mail-wr0-f194.google.com with SMTP id n104so301227wrb.3 for ; Tue, 30 May 2017 20:22:20 -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=LJPLGyEoRfU5XoijCAyKU1ojo+VALRXyRbglsI/+laY=; b=jNXhBB8pPPIxmK0iOYivlMhpGY4u0qw8X7qafddzwgsokyIn9RDIl6NTJV5M6EXVwX p43BtGHl8qUDe5Yjt/rJ72ZMzLBK5P6/+86dTfezNEmTbj+t78DWRrXq0Mldvip0/VfB ACtbeWO8qDQ9NpEv/oStV6AXsKcYQcTGAEtkpDdwf310um9sXRNlloYWCF+4GiKDzAoW RzN3h3jks+U+bDRi1QtiWWK23vtL9/gUXoUOKF7LqoMzsxiXd4L1NJ1KqY6DybSrMmIC 3T5KnQuaILQ0XaBcg69oGBCAQD4eSG1qj2ArLUB3KM9YvwVGwnlmCZSzvFdZqW/e4iqR fnpQ== 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=LJPLGyEoRfU5XoijCAyKU1ojo+VALRXyRbglsI/+laY=; b=dafpmSVynOCktiigBGrXvp21wWPkHi6+TodJGUGCd7IGKiVq30RcRqNrIrE0ag/6R3 VjXA+VjlyrDeifIodwEruJVwH32pCbvWB8aaG0tFGaFFeRM9u1Ayvceysc6yZozAeXnm xgcLMT6PQ/tJ8ny3VceEAI92lVswPGmQUO9BcoDU2I9xulaXOZVfNsK5IbmX+aI+rQGf Vy0dPTB/K4fZhbbhmWNxipE4NRxI4JoUYbt98rDXDJndH8RldQ6x+1LXZjAM3uaky0Iq nADDQSoMlnLDT0+AFfKxBDCS7PyKxonpewUI5EJimGH3CeG7h0WkpVGOCFSrb2nief/2 nzVQ== X-Gm-Message-State: AODbwcD6iynly2JDMk/x/Q6odDVHrFTROE8RQ/qQAN7X+QxdjG1GJtew efZ2bSwBNmzZ+ROeAsM= X-Received: by 10.223.138.188 with SMTP id y57mr13359415wry.93.1496200939442; Tue, 30 May 2017 20:22:19 -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.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 May 2017 20:22:18 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 4/9] div0: warn on division by zero - general case Date: Wed, 31 May 2017 05:22:02 +0200 Message-Id: <20170531032207.95830-5-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 sparse warn about division by zero but only in plain expression: no checks are done when the division is part of a compound assignement. Change this by adding the appropriate check and warning during linearization and take care to avoid warning twice. Note: if should be simpler to drop the checks done at expansion time but some expressions are discarded before reaching linearization and we want to check and warn for those too. Signed-off-by: Luc Van Oostenryck --- linearize.c | 19 +++++++++++++++++++ validation/div-by-zero.c | 12 ++++++++++++ 2 files changed, 31 insertions(+) diff --git a/linearize.c b/linearize.c index 7313e72d8..7760ea996 100644 --- a/linearize.c +++ b/linearize.c @@ -959,6 +959,23 @@ static pseudo_t linearize_store_gen(struct entrypoint *ep, return value; } +static void warn_undef_insn(struct instruction *insn, int warn) +{ + switch (insn->opcode) { + case OP_DIVU: + case OP_DIVS: + case OP_MODU: + case OP_MODS: + if (is_pseudo_value(insn->src2, 0)) { + if (warn) + warning(insn->pos, "division by zero"); + } + break; + default: + break; + } +} + static pseudo_t add_binary_op(struct entrypoint *ep, struct symbol *ctype, int op, pseudo_t left, pseudo_t right) { struct instruction *insn = alloc_typed_instruction(op, ctype); @@ -1208,6 +1225,7 @@ static pseudo_t linearize_assignment(struct entrypoint *ep, struct expression *e oldvalue = cast_pseudo(ep, oldvalue, target->ctype, ctype); opcode = opcode_sign(op_trans[expr->op - SPECIAL_BASE], ctype); dst = add_binary_op(ep, ctype, opcode, oldvalue, value); + warn_undef_insn(dst->def, 1); value = cast_pseudo(ep, dst, ctype, expr->ctype); } value = linearize_store_gen(ep, value, &ad); @@ -1323,6 +1341,7 @@ static pseudo_t linearize_binop(struct entrypoint *ep, struct expression *expr) src2 = linearize_expression(ep, expr->right); op = opcode_sign(opcode[expr->op], expr->ctype); dst = add_binary_op(ep, expr->ctype, op, src1, src2); + warn_undef_insn(dst->def, 0); return dst; } diff --git a/validation/div-by-zero.c b/validation/div-by-zero.c index 786e48298..a06944a58 100644 --- a/validation/div-by-zero.c +++ b/validation/div-by-zero.c @@ -18,6 +18,14 @@ 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; } +int xsvdiv(int a) { if (a && 0) return a / 0; return 0; } +int asvdiv(int a) { return a /= 0; } +int osvdiv(int a) { return a / (a && 0); } + +int xsvmod(int a) { if (a && 0) return a % 0; return 0; } +int asvmod(int a) { return a %= 0; } +int osvmod(int a) { return a % (a && 0); } + /* * check-name: div-by-zero.c * check-command: sparse -Wno-decl $file @@ -39,5 +47,9 @@ 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 +div-by-zero.c:21:42: warning: division by zero +div-by-zero.c:22:33: warning: division by zero +div-by-zero.c:25:42: warning: division by zero +div-by-zero.c:26:33: warning: division by zero * check-error-end */