Errors From Another Planet–GCC vs. Clang Illegal Instruction


Sometimes when I get difficult-to-read output from GCC, I’ll switch to Clang for a little more human-readable output. In rare instances, Clang will give me different results at runtime. For instance, a function with a non-void return type that doesn’t have a return statement will crash a program built with Clang, but not GCC.

If you compile the following program with Clang…

bool crash()
{
    int x=0;
    x=1;
    x++;
}

int main()
{
    crash();
    return 0;
}

…it will abort with a SIGILL instruction on the call to crash(). I was about to write off Clang as simply being moody whereas GCC wasn’t, but a little research showed that Clang was actually catching a small problem that could quickly turn into a big one.

Reference: http://stackoverflow.com/questions/25147667/clang-produces-illegal-instruction-where-gcc-doesnt


Leave a Reply