This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Don't issue array bound warnings on zero-length arrays
- From: Meador Inge <meadori at codesourcery dot com>
- To: Richard Biener <richard dot guenther at gmail dot com>
- Cc: Jeff Law <law at redhat dot com>, GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Tue, 3 Sep 2013 10:40:16 -0500
- Subject: Re: [PATCH] Don't issue array bound warnings on zero-length arrays
- Authentication-results: sourceware.org; auth=none
- References: <1377875587-18004-1-git-send-email-meadori at codesourcery dot com> <CAFiYyc1Ny1ubzOQZC0NkpVTN5WxMONzgHLfbdkO5tBCLZXxrgA at mail dot gmail dot com>
On 09/02/2013 04:27 AM, Richard Biener wrote:
> On Fri, Aug 30, 2013 at 5:13 PM, Meador Inge <meadori@codesourcery.com> wrote:
>> Hi All,
>>
>> This patch fixes a minor issue that can occur when issuing array bounds
>> warnings. In GNU C mode we allow empty lists and their upper bound is
>> initialized to -1. This confuses the array bound analysis in VRP and
>> in some cases we end up issuing false positives. This patch fixes
>> the issue by bailing out when a zero-length array is encountered.
>>
>> OK for trunk?
>>
>> gcc/
>>
>> 2013-08-30 Meador Inge <meadori@codesourcery.com>
>>
>> * tree-vrp.c (check_array_ref): Bail out no emtpy arrays.
>>
>> gcc/testsuite/
>>
>> 2013-08-30 Meador Inge <meadori@codesourcery.com>
>>
>> * gcc.dg/Warray-bounds-11.c: New testcase.
>>
>> Index: gcc/testsuite/gcc.dg/Warray-bounds-11.c
>> ===================================================================
>> --- gcc/testsuite/gcc.dg/Warray-bounds-11.c (revision 0)
>> +++ gcc/testsuite/gcc.dg/Warray-bounds-11.c (revision 0)
>> @@ -0,0 +1,12 @@
>> +/* { dg-do compile } */
>> +/* { dg-options "-O2 -Warray-bounds -std=gnu99" } */
>> +/* Test zero-length arrays for GNU C. */
>> +
>> +unsigned int a[] = { };
>> +unsigned int size_a;
>> +
>> +int test(void)
>> +{
>> + /* This should not warn. */
>> + return size_a ? a[0] : 0;
>> +}
>> Index: gcc/tree-vrp.c
>> ===================================================================
>> --- gcc/tree-vrp.c (revision 202088)
>> +++ gcc/tree-vrp.c (working copy)
>> @@ -6137,9 +6137,10 @@ check_array_ref (location_t location, tr
>> low_sub = up_sub = TREE_OPERAND (ref, 1);
>> up_bound = array_ref_up_bound (ref);
>>
>> - /* Can not check flexible arrays. */
>> + /* Can not check flexible arrays or zero-length arrays. */
>> if (!up_bound
>> - || TREE_CODE (up_bound) != INTEGER_CST)
>> + || TREE_CODE (up_bound) != INTEGER_CST
>> + || tree_int_cst_equal (up_bound, integer_minus_one_node))
>
> That doesn't look correct - what if the lower bound is -10? That can
> easily happen
> for Ada, so please revert the patch.
OK.
> And I fail to see why the testcase should
> not warn. Clearly you have a definition of a here and it doesn't have
> an element
> so the access is out of bounds.
Not always, 'size_a' can be zero and the warning is worded such that the out of
bounds access always happens. In fact, changing the code to 'size_a = 0' still
issues a warning.
--
Meador Inge
CodeSourcery / Mentor Embedded