public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* gcc cannot build correctly famous hello program, clang does it correctly
@ 2022-09-07 10:36 Alex Ernst
  2022-09-07 10:58 ` Jakub Jelinek
  0 siblings, 1 reply; 2+ messages in thread
From: Alex Ernst @ 2022-09-07 10:36 UTC (permalink / raw)
  To: gcc-bugs

[-- Attachment #1: Type: text/plain, Size: 2025 bytes --]

$ gcc -v
gcc version 10.2.1 20210110 (Debian 10.2.1-6)

$ clang -v
Debian clang version 11.0.1-2

$ cat hello.c
int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"Hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}

$ gcc -ansi -o hello hello.c
$ strace hello
. . .
write(1, 0x416c9004, 1)                 = -1 EFAULT (Bad address)
write(1, 0x416c9005, 1)                 = -1 EFAULT (Bad address)
write(1, 0x416c9006, 1)                 = -1 EFAULT (Bad address)
write(1, 0x416c9007, 1)                 = -1 EFAULT (Bad address)
write(1, 0x416c9008, 1)                 = -1 EFAULT (Bad address)
write(1, 0x416c9009, 1)                 = -1 EFAULT (Bad address)
write(1, 0x416c900a, 1)                 = -1 EFAULT (Bad address)
write(1, 0x416c900b, 1)                 = -1 EFAULT (Bad address)
write(1, 0x416c900c, 1)                 = -1 EFAULT (Bad address)
write(1, 0x416c900d, 1)                 = -1 EFAULT (Bad address)
write(1, 0x416c900e, 1)                 = -1 EFAULT (Bad address)
write(1, 0x416c900f, 1)                 = -1 EFAULT (Bad address)
write(1, 0x416c9010, 1)                 = -1 EFAULT (Bad address)
write(1, 0x416c9011, 1)                 = -1 EFAULT (Bad address)
exit_group(0)                           = ?
+++ exited with 0 +++

$ clang -ansi -o hello hello.c
$ strace hello
. . .
write(1, "H", 1H)                        = 1
write(1, "e", 1e)                        = 1
write(1, "l", 1l)                        = 1
write(1, "l", 1l)                        = 1
write(1, "o", 1o)                        = 1
write(1, ",", 1,)                        = 1
write(1, " ", 1 )                        = 1
write(1, "w", 1w)                        = 1
write(1, "o", 1o)                        = 1
write(1, "r", 1r)                        = 1
write(1, "l", 1l)                        = 1
write(1, "d", 1d)                        = 1
write(1, "!", 1!)                        = 1
write(1, "\n", 1
)                       = 1
exit_group(0)                           = ?
+++ exited with 0 +++

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

* Re: gcc cannot build correctly famous hello program, clang does it correctly
  2022-09-07 10:36 gcc cannot build correctly famous hello program, clang does it correctly Alex Ernst
@ 2022-09-07 10:58 ` Jakub Jelinek
  0 siblings, 0 replies; 2+ messages in thread
From: Jakub Jelinek @ 2022-09-07 10:58 UTC (permalink / raw)
  To: Alex Ernst; +Cc: gcc-bugs

On Wed, Sep 07, 2022 at 12:36:22PM +0200, Alex Ernst via Gcc-bugs wrote:
> $ gcc -v
> gcc version 10.2.1 20210110 (Debian 10.2.1-6)
> 
> $ clang -v
> Debian clang version 11.0.1-2
> 
> $ cat hello.c
> int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"Hell\
> o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}

Garbage in, garbage out.  This invokes UB in several spots.
One is that read has the second argument implicitly int, you pass
a const char * argument to it and because write is also unprototyped,
the second argument to it is also int.  Expecting that it passes
in the full pointer is wrong, it "happens to work" on most 32-bit
targets where pointers are the same size as integers and if they
are passed the same way.
This can be fixed by adding char*i; before {write,
but you need to change i/i to p/p or something similar because
you can't divide pointers.
But once you do that, there is another UB, i-- on the first iteration
is invalid pointer arithmetics - "Hello, world!\n"-1 .

	Jakub


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

end of thread, other threads:[~2022-09-07 10:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-07 10:36 gcc cannot build correctly famous hello program, clang does it correctly Alex Ernst
2022-09-07 10:58 ` Jakub Jelinek

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