public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "edulix at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/39143]  New: Incorrect compilation involving assignment by addition/substraction
Date: Mon, 09 Feb 2009 18:42:00 -0000	[thread overview]
Message-ID: <bug-39143-17288@http.gcc.gnu.org/bugzilla/> (raw)

A friend of mine have noticed an error in GCC when he was developing his own C
compiler. The problem happens when using -O0 (no optimization) and local vars.
See sample code:

#include <stdio.h>

int jj = 0, ii = 0; //global vars

int main()
{
    int j = 0, i = 0; // local vars

    i -= i += 2; // i = 0 is correct
    ii -= ii += 2; // ii = -2 => incorrect, should be 0 too

    printf("i: %d ii: %d\n", i, ii);
    return 0;
}

The output is:
$ gcc main.c -o main -O0 && ./main
i: 0 ii: -2

As you can see, the same code (using code "var -= var += value;") returns a
different value depending if the var is local or global. Also note that, oddly
enough, when we use optimizations then the compilated code turns correct in
both cases:

$ gcc main.c -o main -O1 && ./main
i: 0 ii: 0

Same happens when using C++. This is the gcc version:
$ gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2
--enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr
--enable-targets=all --enable-checking=release --build=i486-linux-gnu
--host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu3)


-- 
           Summary: Incorrect compilation involving assignment by
                    addition/substraction
           Product: gcc
           Version: 4.2.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: edulix at gmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39143


             reply	other threads:[~2009-02-09 18:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-09 18:42 edulix at gmail dot com [this message]
2009-02-09 18:45 ` [Bug c/39143] " pinskia at gcc dot gnu dot org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-39143-17288@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).