From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1666) id BD044385740D; Mon, 17 Oct 2022 13:27:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BD044385740D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666013279; bh=j37B82I/Y09RYdWpEicey93yQ3hTT44WJnfiXEhuABw=; h=From:To:Subject:Date:From; b=Oa52KNI9HGii0+X+rGBANmzQiOgFPs/qdGWKmliQH4P1BCii9FbtnZDGe1Oy2CgEI ySTtzFtZ3NMQEBlG2wm4mxOs4KLkty0FCPCOgycXKI9aIiFMq8ijvsrkWGKTpSUF+J GCPaX2m/CMHSTU7bqewtPH2Q9peKeESa7oL+vN+4= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Richard Biener To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-10316] tree-optimization/106189 - avoid division by zero exception X-Act-Checkin: gcc X-Git-Author: Richard Biener X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 2ecf87a7c4b1d68a579704ab4c3efe7dbc0ed5b4 X-Git-Newrev: 9124675d2771aaf573bfbbe237ab28030bf3115d Message-Id: <20221017132759.BD044385740D@sourceware.org> Date: Mon, 17 Oct 2022 13:27:59 +0000 (GMT) List-Id: https://gcc.gnu.org/g:9124675d2771aaf573bfbbe237ab28030bf3115d commit r11-10316-g9124675d2771aaf573bfbbe237ab28030bf3115d Author: Richard Biener Date: Mon Jul 25 17:24:57 2022 +0200 tree-optimization/106189 - avoid division by zero exception 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. PR tree-optimization/106189 * gimple-array-bounds.cc (array_bounds_checker::check_mem_ref): Divide using offset_ints. * gcc.dg/pr106189.c: New testcase. (cherry picked from commit bb04f9f23ac0dee2c003118c85372ece50a52220) Diff: --- gcc/gimple-array-bounds.cc | 2 +- gcc/testsuite/gcc.dg/pr106189.c | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/gcc/gimple-array-bounds.cc b/gcc/gimple-array-bounds.cc index b1179518651..8bcc0077b8c 100644 --- a/gcc/gimple-array-bounds.cc +++ b/gcc/gimple-array-bounds.cc @@ -783,7 +783,7 @@ array_bounds_checker::check_mem_ref (location_t location, tree ref, int i = 0; if (extrema[i] < -arrbounds[1] || extrema[i = 1] > ubound) { - HOST_WIDE_INT tmpidx = extrema[i].to_shwi () / eltsize.to_shwi (); + HOST_WIDE_INT tmpidx = (extrema[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]); }