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.


-- 
           Summary: spurious warning "array subscript is below array bounds"
                    with void* function argument plus -O2
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: niemayer at isg dot de
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35593

Reply via email to