Bug 35593 - [4.3 Regression] spurious warning "array subscript is below array bounds" with void* function argument plus -O2
Summary: [4.3 Regression] spurious warning "array subscript is below array bounds" wit...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.3.0
: P2 normal
Target Milestone: 4.3.1
Assignee: Richard Biener
URL:
Keywords: diagnostic, wrong-code
Depends on:
Blocks:
 
Reported: 2008-03-14 20:11 UTC by niemayer
Modified: 2009-06-03 05:07 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work: 4.4.0
Known to fail: 4.3.0
Last reconfirmed: 2008-03-14 22:44:50


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description niemayer 2008-03-14 20:11:09 UTC
When compiling the following tiny example .cxx source with "-Wall -O2", I get spurious warnings:

------------- cut example test.cxx here --------------
extern void function(void * x);

struct A {
	long x;
	char d[0];
};


void test(A * a) {
	function((char *)a - 4); // gcc emits warning, here - why?
}

// ------------

struct B {
	char d[0];
};

void test(B * b) {
	char * c = (char *)b;
	void * v = c-1;
	function(v);  // gcc emits warning, here - why?
}
------------- cut here --------------

The output of
> gcc -Wall -c test.cxx -O2
is:
/tmp/test.cxx: In function ‘void test(B*)’:
/tmp/test.cxx:22: warning: array subscript is below array bounds
/tmp/test.cxx: In function ‘void test(A*)’:
/tmp/test.cxx:10: warning: array subscript is below array bounds

I have not found a sane way in my non-example, real-world code to avoid those warnings.
Comment 1 Richard Biener 2008-03-14 22:44:34 UTC
Because we fold the pointer addition to &a->d[-8] ;)
Comment 2 Richard Biener 2008-03-14 22:44:50 UTC
I'll have a look.
Comment 3 Richard Biener 2008-03-15 14:28:39 UTC
Subject: Bug 35593

Author: rguenth
Date: Sat Mar 15 14:27:55 2008
New Revision: 133249

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=133249
Log:
2008-03-15  Richard Guenther  <rguenther@suse.de>

	PR middle-end/35593
	* tree-ssa-ccp.c (maybe_fold_offset_to_array_ref): Make sure
	to not produce negative array indices if not allowed.  Add
	parameter to indicate that.
	(maybe_fold_offset_to_component_ref): Allow negative array
	indices only for the first member of a structure.
	(maybe_fold_offset_to_reference): Allow negative array indices.
	(maybe_fold_stmt_addition): Likewise.

	* g++.dg/warn/Warray-bounds-3.C: New testcase.

Added:
    trunk/gcc/testsuite/g++.dg/warn/Warray-bounds-3.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-ssa-ccp.c

Comment 4 Richard Biener 2008-03-15 16:09:44 UTC
Fixed on the trunk.
Comment 5 Richard Biener 2008-03-18 17:24:25 UTC
Subject: Bug 35593

Author: rguenth
Date: Tue Mar 18 17:23:35 2008
New Revision: 133318

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=133318
Log:
2008-03-18  Richard Guenther  <rguenther@suse.de>

        Backport from mainline:
        2008-03-15  Richard Guenther  <rguenther@suse.de>

        PR middle-end/35593
        * tree-ssa-ccp.c (maybe_fold_offset_to_array_ref): Make sure
        to not produce negative array indices if not allowed.  Add
        parameter to indicate that.
        (maybe_fold_offset_to_component_ref): Allow negative array
        indices only for the first member of a structure.
        (maybe_fold_offset_to_reference): Allow negative array indices.
        (maybe_fold_stmt_addition): Likewise.

        * g++.dg/warn/Warray-bounds-3.C: New testcase.

Added:
    branches/gcc-4_3-branch/gcc/testsuite/g++.dg/warn/Warray-bounds-3.C
      - copied unchanged from r133249, trunk/gcc/testsuite/g++.dg/warn/Warray-bounds-3.C
Modified:
    branches/gcc-4_3-branch/gcc/ChangeLog
    branches/gcc-4_3-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_3-branch/gcc/tree-ssa-ccp.c

Comment 6 Richard Biener 2008-03-18 17:28:02 UTC
And the branch.
Comment 7 wufei 2009-06-03 05:07:26 UTC
Did the patch fixes the second case (struct B)? It still warns on 4.3 branch, but no problem on 4.4.