This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/54907] post increasing a value pointed by p in subexpression of an expression modifying p saves the increased value in the wrong place
- From: "graham.stott at btinternet dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Fri, 12 Oct 2012 10:24:30 +0000
- Subject: [Bug c/54907] post increasing a value pointed by p in subexpression of an expression modifying p saves the increased value in the wrong place
- Auto-submitted: auto-generated
- References: <bug-54907-4@http.gcc.gnu.org/bugzilla/>
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54907
--- Comment #1 from graham.stott at btinternet dot com 2012-10-12 10:24:30 UTC ---
Not a bug p is modified twice without a seqence point the result is undefined
________________________________
From: yangzhe1990 at gmail dot com <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Sent: Friday, 12 October 2012, 11:10
Subject: [Bug c/54907] New: post increasing a value pointed by p in
subexpression of an expression modifying p saves the increased value in the
wrong place
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54907
      Bug #: 54907
     Summary: post increasing a value pointed by p in subexpression
          of an expression modifying p saves the increased value
          in the wrong place
  Classification: Unclassified
     Product: gcc
     Version: 4.7.1
      Status: UNCONFIRMED
     Severity: normal
     Priority: P3
    Component: c
    AssignedTo: unassigned@gcc.gnu.org
    ReportedBy: yangzhe1990@gmail.com
#include <stdio.h>
int main() {
  char s[] = "axxxxx";
  char *p = s;
  printf("s = %s in the beginning.\n"
     "p is pointed at the %d-th char.\n", s, p - s);
  //p = p + (*p)++ * 3 + 2 - 'a' * 3; // (1)
  p += (*p)++ * 3 + 2 - 'a' * 3; // (2)
  printf("p is moved ahead by %d steps\n", p - s);
  printf("s = %s after the operation.\n", s);
  return 0;
}
The expected result is "bxxxxx". But the output is "axbxxx".
Maybe in the wrong code, when it saves the value, it lookups the address again
by *p, but p is modified in the expression.
As discussed in stackoverflow,
http://stackoverflow.com/questions/12823663/would-p-p-p-3-c-cause-an-undefined-behavior?answertab=votes#tab-top
most people think it's a bug of gcc.
Bug found in gcc 4.4.6, 4.7.1, g++ 4.4.6. g++ 4.7.1 produces the correct
result.