Colen
Colen

Reputation: 13898

clang optimization bug?

I've been trying to track down what seems like a bug in clang, and I think I've got a reasonably minimal reproduction of it. Here's my program:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define x_Is_Digit(x)       isdigit((unsigned char) (x))

void        Odd_Behavior(char * version)
{
    char * ptr, *tmp;

    for (ptr = version; x_Is_Digit(*ptr); ptr++);
    ptr++;

    for (tmp = ptr; x_Is_Digit(*ptr); ptr++);
    if (ptr == tmp)
        printf("%08x == %08x! Really?\n", ptr, tmp);
}

int main()
{
    char buffer[100];
    strcpy(buffer, "3.8a");
    Odd_Behavior(buffer);
    return(0);
}

When I compile it with optimization, in the clang included with the Xcode download ("Apple clang 2.1"):

clang++ -Os optimizebug.cpp

And run it, it reports:

6b6f2be3 == 6b6f2be2! Really?

This strikes me as a tad odd, to say the least. If I remove the (unsigned char) cast in x_Is_Digit, it works properly.

Have I run into a bug in clang? Or am I doing something here that's causing some sort of undefined behavior? If I compile it with -O0, I don't get the problem.

Upvotes: 3

Views: 734

Answers (2)

Chris Lattner
Chris Lattner

Reputation: 829

Certainly looks like a bug to me. Clang mainline doesn't display this (at least on darwin/x86-64). Please file a bug at llvm.org/bugs with full details on how to reproduce this. Stack overflow isn't a great place to report compiler bugs :)

Upvotes: 9

Lindydancer
Lindydancer

Reputation: 26094

Definitively a bug. If the two pointers are equal at the if statement, they must also be equal in the printf statement.

Upvotes: 1

Related Questions