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

Bug#707118: gcc-4.7: incorrect code with -O2



Package: gcc-4.7
Version: 4.7.2-5
Severity: grave

Dear Maintainer,

gcc produces incorrect code with -O2 for this simple example:

zaphod@eddie:~$ cat a.c

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

int foo(int);

int main(int argc, char** argv)
{
    char pstring[256];
    memset(pstring, 0, 256*sizeof(char));

    *pstring = ' ';
    strncat(pstring, "hello world", sizeof(pstring));

    *pstring = (char) (strlen(pstring) - 1);

    if (foo(strlen(pstring)) <= 0)
        return (-1);
    return 0;
}

int foo(int count)
{
    printf("%d\n", count);
    return count;
}

zaphod@eddie:~$ gcc -O0 a.c && ./a.out
12
zaphod@eddie:~$ gcc -O2 a.c && ./a.out
0

This could cause all kinds of data loss and security holes.

-- System Information:
Debian Release: 7.0
  APT prefers stable
  APT policy: (500, 'stable'), (500, 'oldstable')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 3.8.7 (SMP w/8 CPU cores)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash

Versions of packages gcc-4.7 depends on:
ii  binutils      2.22-8
ii  cpp-4.7       4.7.2-5
ii  gcc-4.7-base  4.7.2-5
ii  libc6         2.13-38
ii  libgcc1       1:4.7.2-5
ii  libgmp10      2:5.0.5+dfsg-2
ii  libgomp1      4.7.2-5
ii  libitm1       4.7.2-5
ii  libmpc2       0.9-4
ii  libmpfr4      3.1.0-5
ii  libquadmath0  4.7.2-5
ii  zlib1g        1:1.2.7.dfsg-13

Versions of packages gcc-4.7 recommends:
ii  libc6-dev  2.13-38

Versions of packages gcc-4.7 suggests:
pn  binutils-gold        <none>
pn  gcc-4.7-doc          <none>
pn  gcc-4.7-locales      <none>
pn  gcc-4.7-multilib     <none>
pn  libcloog-ppl0        <none>
pn  libgcc1-dbg          <none>
pn  libgomp1-dbg         <none>
pn  libitm1-dbg          <none>
pn  libmudflap0-4.7-dev  <none>
pn  libmudflap0-dbg      <none>
pn  libppl-c2            <none>
pn  libppl7              <none>
pn  libquadmath0-dbg     <none>

-- no debconf information


Reply to: