public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [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
@ 2012-10-12 10:11 yangzhe1990 at gmail dot com
  2012-10-12 10:24 ` [Bug c/54907] " graham.stott at btinternet dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: yangzhe1990 at gmail dot com @ 2012-10-12 10:11 UTC (permalink / raw)
  To: gcc-bugs


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.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-08-12 14:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-12 10:11 [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 yangzhe1990 at gmail dot com
2012-10-12 10:24 ` [Bug c/54907] " graham.stott at btinternet dot com
2012-10-12 11:21 ` yangzhe1990 at gmail dot com
2012-10-12 11:24 ` redi at gcc dot gnu.org
2012-10-12 15:40 ` joseph at codesourcery dot com
2021-08-12 14:41 ` pinskia at gcc dot gnu.org

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).