The following code:

void bug_example_2(const char** format_address, int* ip) {
        char* p1=(char*)format_address;
        char* p2=(char*)ip;
        int dif=p2-p1;
        if (dif!=sizeof(char*)) {
                // crash
                char* p=0; *p=0;
        }
}

void bug_example(const char* strp, int i) {
        char buffer[1000]; buffer[0]=0;
        bug_example_2(&strp, &i);
}

int main(void) {
        bug_example("GCC has a bug", 10);
        return 0;
}

... is incorrectly compiled by GCC. As you can see there are no variable
parameters in this code, so there is nothing here out of the ordinary.

Possibility 1) GCC is not cdecl-ABI compliant, so the "dif" can have values
other than 4 on x86-32. In this case GCC should not claim to be
cdecl-compliant.

Possibility 2) GCC is not conformant to C99 but it is cdecl-ABI compliant. C99
states in section 6.5.3.2 paragraph 3 that "The unary & operator yields the
address of its operand.", but GCC is not doing that, as the "if" in
bug_example_2 is occasionally entered. Thus "dif" is not 4 (and with cdecl ABI
it should be 4 on x86-32).

If line "char buffer[1000]; buffer[0]=0;" GCC then compiles the code as
expected and "dif" will be 4.

This proves GCC is not conforming to C99 recommendations or that is not
cdecl-ABI compliant (or possibly both).

Don't bother trying to understand why I need the & operand to work as stated in
C99, or why I need the code to be cdecl compliant, that is too complicated for
you and it would just confuse you. For the purpose of this bug you may simply
consider that I'm performing conformity tests on GCC against C99 and cdecl, and
that GCC failed the test.

Next I will send you the preprocessed file and the compilation script.


-- 
           Summary: GCC has an intermittent bug when computing the address
                    of function parameters
           Product: gcc
           Version: 4.3.3
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: rogerio at rilhas dot com
 GCC build triplet: i686-virtualboxvm-ubuntu?
  GCC host triplet: i686-virtualboxvm-ubuntu?
GCC target triplet: i686-virtualboxvm-ubuntu?


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

Reply via email to