nmh-workers
[Top] [All Lists]

Re: [Nmh-workers] strcasecmp.c and gcc -O2

2007-09-06 07:15:13

strcasecomp(a,b) segfaults if a=NULL and compiled with -O2 and gcc >= 4.1.1.

The code in question is:


int
mh_strcasecmp (const char *s1, const char *s2) 
{
    const unsigned char *us1, *us2;

    us1 = (const unsigned char *) s1,
    us2 = (const unsigned char *) s2;

    if (!us1)
        us1 = "";
    if (!us2)
        us2 = "";
 
    while (tolower(*us1) == tolower(*us2++)) 
        if (*us1++ == '\0')
            return (0);
    return (tolower(*us1) - tolower(*--us2));
}


It seems the compiler (using -O2) totally optimizes away the two if clauses. 
Looking at the assembler:


I don't see this behavior with gcc 4.1.2 on Fedora 7.  It's quite possible
you're seeing a gcc bug.  I'd suggest writing a minimalistic freestanding
testcase.  If that testcase still exhibits this behavior, submit a bug
report to the gcc folks.

-- 
    JB


_______________________________________________
Nmh-workers mailing list
Nmh-workers(_at_)nongnu(_dot_)org
http://lists.nongnu.org/mailman/listinfo/nmh-workers

<Prev in Thread] Current Thread [Next in Thread>