bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: GCC optimizes integer overflow: bug or feature?


From: Denis Vlasenko
Subject: Re: GCC optimizes integer overflow: bug or feature?
Date: Sat, 23 Dec 2006 17:29:00 +0100
User-agent: KMail/1.8.2

On Saturday 23 December 2006 10:06, Rask Ingemann Lambertsen wrote:
>    No, because you'd read past the end of the array:
> 
> #include <stdlib.h>
> 
> int main (int argc, char *argv[])
> {
>   char *a;
>   if ((a == malloc (sizeof (char))))
>     {
>       int r;
> 
>       a[0] = 1;
>       r = f (a);
>       free (a);
>       return (r);
>     }
>   return (0);
> }

Good spotting.

We can use & instead of &&. C standard doesn't
aloow lazy execution of (a & b) IIRC...

int f(char *p)
{
    if ((p[0] == 1) & (p[1] == 2)) return 1;
    return 0;
}

Currently it does this:

        .file   "t.c"
        .text
        .p2align 2,,3
.globl f
        .type   f, @function
f:
        movl    4(%esp), %edx
        cmpb    $1, (%edx)
        sete    %al
        cmpb    $2, 1(%edx)
        sete    %dl
        andl    %edx, %eax
        movzbl  %al, %eax
        ret
        .size   f, .-f
        .ident  "GCC: (GNU) 4.2.0 20061128 (prerelease)"
        .section        .note.GNU-stack,"",@progbits

--
vda




reply via email to

[Prev in Thread] Current Thread [Next in Thread]