From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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) From: Richard Biener 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 Content-Type: text/plain; charset=US-ASCII 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-List-Received-Date: Tue, 26 Jul 2022 06:36:31 -0000 Message-ID: <20220726063628.osCA1KecEcosNohejeQwP9_wQvFPH9JsgFLypfEvU3I@z> 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]); } -- 2.35.3