Efi Weiss
Efi Weiss

Reputation: 650

GCC Optimizes away an if clause which can't be optimized

I created a small code showing the error I encountered

#include<stdlib.h>
#include<stdio.h>
int test(char * flag)
{
    char flagger = *flag;
    printf("test value %d", (int) flagger);
    if (flagger != 0x82)
    {
        exit(3);
    }
    else 
    {
        return 0;
    }
}


int main(void)
{
    char flag = 0x82, flag1 = 0x12, flag2 = 0x45;
    //char buf[256];
    test(&flag);
    test(&flag1);
    test(&flag2);
}

When compiling the code: gcc -o tester test.c or gcc -o tester test.c -O0

The resulting disassembly code for the function test in gdb is:  
Dump of assembler code for function test:
0x0804849b <+0>:    push   ebp
0x0804849c <+1>:    mov    ebp,esp
0x0804849e <+3>:    sub    esp,0x18
0x080484a1 <+6>:    mov    eax,DWORD PTR [ebp+0x8]
0x080484a4 <+9>:    movzx  eax,BYTE PTR [eax]
0x080484a7 <+12>:   mov    BYTE PTR [ebp-0x9],al
0x080484aa <+15>:   movsx  eax,BYTE PTR [ebp-0x9]
0x080484ae <+19>:   sub    esp,0x8
0x080484b1 <+22>:   push   eax
0x080484b2 <+23>:   push   0x80485c0
0x080484b7 <+28>:   call   0x8048350 <printf@plt>
0x080484bc <+33>:   add    esp,0x10
0x080484bf <+36>:   sub    esp,0xc
0x080484c2 <+39>:   push   0x3
0x080484c4 <+41>:   call   0x8048370 <exit@plt>
End of assembler dump.

As you can see, the if gets optimized to a version in which exit is always called.

I tried many things (reverting the conditional, using volatiles, etc.), but I run out of ideas to why this happen. Help please?

Upvotes: 4

Views: 123

Answers (1)

M.M
M.M

Reputation: 141534

On your system, the range of char is -128 to +127. But 0x82 is 130 in decimal. Since 130 > 127, this test can never succeed.

To fix the code you could use:

if ( flagger != '\x82' )

or

if ( (unsigned char)flagger != 0x82 )

Note that the earlier code char flag = 0x82 is an out-of-range assignment, which is implementation-defined behaviour. You could consider using unsigned char or uint8_t for all of these variables.

Upvotes: 12

Related Questions