public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* optimization/6330: tiny C++ prog broken under -O1
@ 2002-04-17  2:16 dh
  0 siblings, 0 replies; 4+ messages in thread
From: dh @ 2002-04-17  2:16 UTC (permalink / raw)
  To: gcc-gnats


>Number:         6330
>Category:       optimization
>Synopsis:       tiny C++ prog broken under -O1
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Apr 17 02:16:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     dh@digitalbrain.com
>Release:        2.95.4 20011002 (Debian prerelease)
>Organization:
>Environment:
gcc version 2.95.4 20011002 (Debian prerelease)

Linux fee 2.4.18-p7-rmap12c-r #267 SMP Mon Feb 4 12:05:56 GMT 2002 i686 unknown
>Description:
Taking the address of an automatic variable says to the compiler "don't use a register for this!".

But I suspect that casting an automatic variable into another type and then taking the address of That
doesn't say anything of the kind
(even though it should)
>How-To-Repeat:
this linux console output shows a tiny shell script
being run that duplicates the problem ..

5750:fee:~: cat ./show_gcc_bug.sh
#!/bin/sh

# what compiler/OS version is it?
gcc -v
uname -a

# create a program
echo '#include <stdio.h>' > x.cpp
echo 'typedef unsigned char byte;' >> x.cpp;
echo 'void change(byte **z) { *z = (byte*)"correct"; }' >> x.cpp
echo 'int main() {' >> x.cpp
echo '    char *s = "incorrect";' >> x.cpp
echo '    change(&(byte*)s);' >> x.cpp
echo '    printf("%s\n", s);' >> x.cpp
echo '    exit(0);' >> x.cpp
echo '}' >> x.cpp

# compile and run it
gcc x.cpp
./a.out

# compile and run The Same Program optimized
gcc -O1 x.cpp
./a.out

5751:fee:~: ./show_gcc_bug.sh
Reading specs from /usr/lib/gcc-lib/i386-linux/2.95.4/specs
gcc version 2.95.4 20011002 (Debian prerelease)
Linux fee 2.4.18-p7-rmap12c-r #267 SMP Mon Feb 4 12:05:56 GMT 2002 i686 unknown
correct
incorrect
5752:fee:~:
>Fix:
no fix known ..

work arounds:

1)
change the x.cpp from &(byte*)s
to (byte**)&s

2)
rename x.cpp to x.c
because the C front end will give the error
"x.c:6: invalid lvalue in unary `&'"
rather than make incorrect code

>Release-Note:
>Audit-Trail:
>Unformatted:


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

* Re: optimization/6330: tiny C++ prog broken under -O1
@ 2002-04-22  9:36 Richard Henderson
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Henderson @ 2002-04-22  9:36 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR optimization/6330; it has been noted by GNATS.

From: Richard Henderson <rth@redhat.com>
To: David Hanney <dh@digitalbrain.com>
Cc: rth@gcc.gnu.org, gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org,
   nobody@gcc.gnu.org, gcc-gnats@gcc.gnu.org
Subject: Re: optimization/6330: tiny C++ prog broken under -O1
Date: Mon, 22 Apr 2002 09:34:33 -0700

 On Mon, Apr 22, 2002 at 10:02:00AM +0100, David Hanney wrote:
 > .. would a compiler warning be useful here?
 
 Yes.  There's already an open PR about this.
 
 
 r~


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

* Re: optimization/6330: tiny C++ prog broken under -O1
@ 2002-04-22  2:06 David Hanney
  0 siblings, 0 replies; 4+ messages in thread
From: David Hanney @ 2002-04-22  2:06 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR optimization/6330; it has been noted by GNATS.

From: David Hanney <dh@digitalbrain.com>
To: rth@gcc.gnu.org, gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org,
	nobody@gcc.gnu.org, gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org,
	gcc-prs@gcc.gnu.org, nobody@gcc.gnu.org
Cc:  
Subject: Re: optimization/6330: tiny C++ prog broken under -O1
Date: Mon, 22 Apr 2002 10:02:00 +0100

 Many thanks for your prompt response!
 
 I assumed (wrongly, I know) that because "the cast-output is the same four 
 bytes
 as the cast-input" that it would be at the same address (union-style). My bad.
 
 But, given this behavior-set ..
 
     gcc with plain     C++ works.            (no compiler warning)
     gcc with optimized C++ doesn't work.     (no compiler warning)
     gcc with           C   doesn't compile.
    (under gcc 2.95.4 and gcc 3)
 
 .. would a compiler warning be useful here?
 
 Especially as the pointer may be formed implicitly via a reference
 (as shown in the code quoted below)
 
 cheers,
 
 DH
 
 -------------------------------------------
 #!/bin/sh
 
 # what compiler/OS version is it?
 gcc-3.0 -v
 uname -a
 
 # create a program
 echo '#include <stdio.h>' > x.cpp
 echo '#include <stdlib.h>' >> x.cpp
 echo 'typedef unsigned char byte;' >> x.cpp;
 echo 'typedef byte *pbyte;' >> x.cpp;
 echo 'void change(pbyte &z) { z = (pbyte)"correct"; }' >> x.cpp
 echo 'int main() {' >> x.cpp
 echo '    char *s = "incorrect";' >> x.cpp
 echo '    change((byte*)s);' >> x.cpp
 echo '    printf("%s\n", s);' >> x.cpp
 echo '    exit(0);' >> x.cpp
 echo '}' >> x.cpp
 
 # compile and run it
 gcc-3.0 x.cpp
 ./a.out
 
 # compile and run The Same Program optimized
 gcc-3.0 -O1 x.cpp
 ./a.out
 -------------------------------------------
 
 
 
 
 
 At 00:41 18/04/02 +0000, rth@gcc.gnu.org wrote:
 >Synopsis: tiny C++ prog broken under -O1
 >
 >State-Changed-From-To: open->closed
 >State-Changed-By: rth
 >State-Changed-When: Wed Apr 17 17:41:35 2002
 >State-Changed-Why:
 >     Not a bug.  You have taken the address of a temporary.
 >     You wanted "change((byte**)&s)".
 >
 >http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=6330 
 >
 


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

* Re: optimization/6330: tiny C++ prog broken under -O1
@ 2002-04-17 17:41 rth
  0 siblings, 0 replies; 4+ messages in thread
From: rth @ 2002-04-17 17:41 UTC (permalink / raw)
  To: dh, gcc-bugs, gcc-prs, nobody

Synopsis: tiny C++ prog broken under -O1

State-Changed-From-To: open->closed
State-Changed-By: rth
State-Changed-When: Wed Apr 17 17:41:35 2002
State-Changed-Why:
    Not a bug.  You have taken the address of a temporary.
    You wanted "change((byte**)&s)".

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=6330


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

end of thread, other threads:[~2002-04-22 16:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-17  2:16 optimization/6330: tiny C++ prog broken under -O1 dh
2002-04-17 17:41 rth
2002-04-22  2:06 David Hanney
2002-04-22  9:36 Richard Henderson

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