Bug 27306

Summary: while and (type *&)variable++ causes never ending loop
Product: gcc Reporter: kalas <kalas>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: adruab, Andreas.Glowatz, andreg, bobm75, boris, c.pop, cdfrey, charles, christophe.guillon, cxl, czang, d.bonekaemper, danfuzz, davids, davmac, devin, djk, dmeggy, duraid, evgeny, ezharkov, fabdouze, fm, gcc-bugs, gcc, ghouston, gino, gopalv82, grigory_zagorodnev, hayim, horst.lehser, hurbain, ja2morri, jason.elbaum, jfran, jochang, larschri, lindsayd, linuxadmin, lucho, l_heldt, micis, mike, mor_gopal, noaml, pgonzalez, pierre.chatelier, pjh, rarob, renzo, rick.ju, spelis, steffen.zimmermann, steger, strasbur, sumii, thomas.anders, warp, zengpan, zybi
Priority: P3    
Version: 4.0.2   
Target Milestone: ---   
Host: cygwin Target: arm-elf
Build: Known to work:
Known to fail: Last reconfirmed:

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 ***