Bug 27306 - while and (type *&)variable++ causes never ending loop
Summary: while and (type *&)variable++ causes never ending loop
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-04-25 11:47 UTC by kalas
Modified: 2006-06-20 06:15 UTC (History)
60 users (show)

See Also:
Host: cygwin
Target: arm-elf
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description kalas 2006-04-25 11:47:50 UTC
Following code is not compiled correctly and causes never ending loop. GCC 4.0.2 from GNUARM.org distribution was used.

    typedef unsigned short  U16;

    void * ptr = buffer;
    void * end = (U16 *)buffer + num;

    while (ptr != end)
    {
        *((U16 *&)ptr)++ = c;
    }

dissasebly with intermixed source code:

    void * ptr = buffer;
    void * end = (U16 *)buffer + num;
     924:	e2812a02 	add	r2, r1, #8192	; 0x2000

    while (ptr != end)
     928:	e1520001 	cmp	r2, r1
     92c:	e50b1038 	str	r1, [fp, #-56]
     930:	0a000250 	beq	948 <_ZN7MvbcLSA8InitChipEhh+0x228>
     934:	e3a00000 	mov	r0, #0	; 0x0
     938:	e1a03001 	mov	r3, r1
    {
        *((U16 *&)ptr)++ = c;
     93c:	e0c300b2 	strh	r0, [r3], #2
     940:	1a00024d 	bne	93c <_ZN7MvbcLSA8InitChipEhh+0x21c>
     944:	e50b3038 	str	r3, [fp, #-56]
    }

arm-elf-gcc --ver
Using built-in specs.
Target: arm-elf
Configured with: ../gcc-4.0.2/configure --target=arm-elf --prefix=/g/gnuarm-4.0.2 --enable-interwork --enable-multilib --with-newlib --with-headers=../newlib-1.14.0/newlib/libc/include --enable-languages=c,c++
Thread model: single
gcc version 4.0.2
Comment 1 Richard Biener 2006-04-25 12:15:46 UTC
This is not a valid testcase, or even expression.  I guess it's invalid anyway because of operator precedence.
Comment 2 Falk Hueffner 2006-04-25 12:19:49 UTC
It's a valid C++ fragment. However, this code accesses "ptr", which is of type
void*, via an lvalue of type U16*. This is undefined behavior, so there is no
gcc bug here.
Comment 3 Richard Biener 2006-04-25 12:20:27 UTC
Oh, this is just an aliasing issue.  Reopening...
Comment 4 Richard Biener 2006-04-25 12:21:09 UTC
.. to close as dup of PR21920.

*** This bug has been marked as a duplicate of 21920 ***