NO

Author Topic: Compiler code-gen bug in pointer subtraction - simple example  (Read 1633 times)

RichieL

  • Guest
Compiler code-gen bug in pointer subtraction - simple example
« on: February 12, 2010, 08:11:54 am »
If you compile the following program with Optimizations set to None,

    struct foo_s { int i;} *b[9], *c;
    int main(int argc, char *argv[]) { int x = c - b[c->i];}

set a breakpoint on  the 2d line and Show Dissasembly in the debugger, you see the compiler generated the following code:

   ...
   mov  eax, dword ptr [c]
   mov  eax, dword ptr [eax]
   mov  eax, dword ptr [eax*4+b]   <=== Oops, this should be a "mov edx, ..."
   sub   eax, eax                            <=== Oops, this is always 0, should be "sub edx, eax"
   sar    eax, 2
   mov   dword ptr
  • , eax

   ...

Breaking the expression up and storing c->i in a temp is a workaround, but compiler bugs are scary...