Live data from Hacker News

The Shortest Crashing C Program

llbit.se

31–40 of 94 posts

Re: The Shortest Crashing C Program

#31
post #28
post #16

Earlier quoted context omitted.

It still is, provided you follow the build procedure prescribed by the author of that quine (check the Makefile from the contest): $ rm -rf a $ cp a.c a $ chmod +x a $ ./a $

I find it interesting that displays the error from exec, but not bash: zsh: exec format error: ./a

Why?

The file is marked as executable, so the shell very reasonably tries to execute it by calling some well-chosen member of the exec() family (http://linux.die.net/man/3/exec).

The exec() function then needs to open and parse the file according to the formats it supports, which of course fails since the file is empty.

Do you simply mean that you expected the shell to validate this, and not try to execute empty files?

Re: The Shortest Crashing C Program

#36
post #7

It depends on the definition. You can do better than this if you define a valid C program as anything that passes though the C compiler and generates an executable. Behold the zero length program: $ touch a.c $ gcc -c a.c $ ld a.o ld: warning: cannot find entry symbol _start; defaulting to 0000000000400078 $ ./a.out Segmentation fault

You can build it with a single command:

gcc -nostdlib ./empty.c -o ./empty

Edit: This one actually runs correctly:

    $ touch empty.c
    $ gcc -static -nostartfiles ./empty.c -e_exit -o ./empty
    $ ./empty && echo $?
    > 0

Re: The Shortest Crashing C Program

#37
post #35

How about: main(){*(int*)0=0;} or: main(){*""=0;} or: main(){main();}

All substantially longer than the example given.

The site was down when I made my suggestions. Still, I think mine may be a bit more language compliant than the shortest variants in the article.

Re: The Shortest Crashing C Program

#39
post #30
post #7

It depends on the definition. You can do better than this if you define a valid C program as anything that passes though the C compiler and generates an executable. Behold the zero length program: $ touch a.c $ gcc -c a.c $ ld a.o ld: warning: cannot find entry symbol _start; defaulting to 0000000000400078 $ ./a.out Segmentation fault

Does not compute. At least on OS X: $ ld a.o ld: warning: -macosx_version_min not specified, assuming 10.7 Undefined symbols for architecture x86_64: "start", referenced from: implicit entry/start for main executable ld: symbol(s) not found for inferred architecture x86_64

Does it work if you specify an entry address? Something like this:

gcc -nostdlib ./empty.c -e0 -o ./empty

Re: The Shortest Crashing C Program

#40
post #11

It's also the shortest C program that you can link at all.

$ echo "m;" > short.c

$ gcc -O0 -c short.c

short.c:1: warning: data definition has no type or storage class

$ ld -e _m -o short short.o

ld: warning: -macosx_version_min not specified, assuming 10.7

$ ./short

[1] 2040 segmentation fault ./short

Post reply on HN