From patchwork Tue Jul 26 06:36:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 178 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:b5d6:b0:2b9:3548:2db5 with SMTP id v22csp2079789pxt; Mon, 25 Jul 2022 23:37:33 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sGQz9vQrx7/u36wXyuN6MlikHxXrXDk5fpG4qpxq0CYLxM+Al+vXkJgDR0K5RQ+d/o4lDC X-Received: by 2002:a05:6402:378b:b0:43c:fed:89ce with SMTP id et11-20020a056402378b00b0043c0fed89cemr6786399edb.242.1658817453531; Mon, 25 Jul 2022 23:37:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658817453; cv=none; d=google.com; s=arc-20160816; b=0sAGDiVjoWwnYZisvPhP6R3yhq95eo/qYTYBDD+sBl5u5ZWAc97ePLsdgdg3F4STmA meTpCWipo5H8u7dmr8I2ARYmrTwolCyPBOcmH2CgtOD26YbeCAyXmW2IlnQG1rwzegxf rQm++M7uz2vnoELFxvbmfKZy0tGPjh+eXjU5sZBx+KxEhJCV0azj+XcL2jAZ4+gCyeIF +swHHFRek2T9d6w0JRZbrOkD7OAkJqrCHvmAk2zNQ+vT7MX+zSqCd9AyAS4EBZ5nzejz I0s3NHGpxtrSvRTbnTzAtNCLuFgFIeM7EHy43QiWcbiLc+DMr34OgbkPsNCjwsQDYGyP SiZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:sender:errors-to:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mime-version:user-agent:subject:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=GKQougK5BoqaPxNWwS1YjltBW6OuISHyLyz6T9FEVjE=; b=YBIsP+QmyqrrmdrPG3yZkmXEG4WlamVoGlpPV1kDHbPAZUhjzAd4U6ZKlJTdH4yiUp P4a9riXPzkCe10tF52/gXNLcEoG2Kx61llBZY4MRLQN3DEiCPC8OktE+SgnriapZKtOb cdvqD0a2nQeQhG6X4W3uU4DXWd0popl/cnwyovWxiy5st6+ZwWbI/VWyWMNxO54QL4Yp JnA42PUei9NUs+nWooVIBPpIVDZQWul+djj8efNSooh7j8vA+YwrfkgQYoTkd2Eb0im6 4ONn2986wlKxrQciyhzMHKZh+3O+3EWJN97gRofNicdLU0AjO/HRukBM3C7lgPtRyBVY pMCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=KQ7FzIf6; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id x3-20020a05640226c300b0043c1ab7956bsi3872343edd.104.2022.07.25.23.37.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 23:37:33 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=KQ7FzIf6; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 775043858421 for ; Tue, 26 Jul 2022 06:37:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 775043858421 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1658817433; bh=GKQougK5BoqaPxNWwS1YjltBW6OuISHyLyz6T9FEVjE=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=KQ7FzIf6u2LA5dVTfUPmdGiQkj6TJuTjiHGr1gqarKYfXMaWefcgaVx3AqlLoVKNX wNZkVByAXAWHyTkPgv4KZErT4vORAJUB4Dt2O3Awh8s5i7FylzLJC66cGcznc9xyJW N0xNsol8NGCz9uMqpy84xlu4jkPno5UBa/OPVVPk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 3F4863858D32 for ; Tue, 26 Jul 2022 06:36:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3F4863858D32 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 252701F8E8 for ; Tue, 26 Jul 2022 06:36:29 +0000 (UTC) Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 1F9872C16E for ; Tue, 26 Jul 2022 06:36:29 +0000 (UTC) Date: Tue, 26 Jul 2022 06:36:28 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/106189 - avoid division by zero exception User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" Message-Id: <20220726063713.775043858421@sourceware.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1739396170127681135?= X-GMAIL-MSGID: =?utf-8?q?1739396170127681135?= The diagnostic code can end up with zero sized array elements with T[][0] and the wide-int code nicely avoids exceptions when dividing by zero in one codepath but not in another. The following fixes the exception by using wide-int in both paths. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/106189 * gimple-array-bounds.cc (array_bounds_checker::check_mem_ref): Divide using offset_ints. * gcc.dg/pr106189.c: New testcase. --- gcc/gimple-array-bounds.cc | 2 +- gcc/testsuite/gcc.dg/pr106189.c | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/pr106189.c diff --git a/gcc/gimple-array-bounds.cc b/gcc/gimple-array-bounds.cc index 0b48bdb0ae5..e190b93aa85 100644 --- a/gcc/gimple-array-bounds.cc +++ b/gcc/gimple-array-bounds.cc @@ -534,7 +534,7 @@ array_bounds_checker::check_mem_ref (location_t location, tree ref, int i = 0; if (aref.offmax[i] < -aref.sizrng[1] || aref.offmax[i = 1] > ubound) { - HOST_WIDE_INT tmpidx = aref.offmax[i].to_shwi () / eltsize.to_shwi (); + HOST_WIDE_INT tmpidx = (aref.offmax[i] / eltsize).to_shwi (); if (warning_at (location, OPT_Warray_bounds, "intermediate array offset %wi is outside array bounds " diff --git a/gcc/testsuite/gcc.dg/pr106189.c b/gcc/testsuite/gcc.dg/pr106189.c new file mode 100644 index 00000000000..0eca8343c56 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr106189.c @@ -0,0 +1,5 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -Warray-bounds=2 -w" } */ + +int a_n_0_0_a[][0]; +void a_n_0_0() { T(((char *)a_n_0_0_a)[1]); }