Bug 26418

Summary: App with assignment to *(ptr + member_returning_zero()) segfaults
Product: gcc Reporter: fuchsia.groan
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: algorithmus, arminu, asokumar, av1474, bala, barnarr, bmead15, buergel, carpman, ceniza666, chuchunxin, devnull, d_picco, eric.mcvicker, fuchsia.groan, gaurav_har, gcc-bugs, gcc, ggs, horsh, jandres, janis, jompo, krs, lid, lindahlb, lxg8906, mayer, mikaldaz, nakkore, pierre.van.de.laar_at_philips.com, qyang, raoulgough, rglan, rjvbertin, robc, s9322036, smartmouse714, suan, super.aorta, svetozarmarkov, tczarnecki, vanveghel, vitaly, zshao
Priority: P3    
Version: 4.0.1   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description fuchsia.groan 2006-02-22 13:37:00 UTC
/*
  This code segfaults when executed on gcc 4.01 on i586/linux (mandriva 2006)

  Works fine on gcc 3.x - couldn't find a newer version to test it on.

  PS I couldn't find anything in your bug writing guidelines what to put in "host triplet" etc...

gcc -v:

Using built-in specs.
Target: i586-mandriva-linux-gnu
Configured with: ../configure --prefix=/usr --libexecdir=/usr/lib --with-slibdir=/lib --mandir=/usr$
Thread model: posix
gcc version 4.0.1 (4.0.1-5mdk for Mandriva Linux release 2006.0) 
*/  

typedef unsigned int uint;

char* dummy;

struct Array {
    char**         ptr;

    void push(char* i)       { *(ptr+alloc()) = i; }

    uint alloc() {
       ptr = &dummy;
       return 0;
    }

};

int main()
{
   Array test;
   char* arg = "fred";
   test.push(arg);
   // can you please check check *test.ptr == arg, as that was what led
   // me to finding the bug wasn't
}
Comment 1 fuchsia.groan 2006-02-22 13:41:20 UTC
No compile options just "g++ test.cpp" generates faulty code
Comment 2 Andrew Pinski 2006-02-22 14:04:55 UTC
ptr+alloc()

The C and the C++ standard does not say which of ptr and alloc() is evaluated first so GCC is producing code which evaluates ptr before calling alloc which is ok for C and C++.

*** This bug has been marked as a duplicate of 11751 ***