Live data from Hacker News

The Shortest Crashing C Program

llbit.se

41–50 of 94 posts

Re: The Shortest Crashing C Program

#42
post #5

I'm not convinced this is a C89 program. It is only an "accident" that the linker doesn't know about types. I find it hard to believe that the C89 spec states that an integer called "main" is to be considered the main function, and suspect this is undefined behaviour (though I've not checked).

It compiles and runs with gcc -std=c89 and gcc -std=c99, so even if it's not a true C89 program, it's a compilable GNUC89 program. $ gcc -std=c99 -pedantic /tmp/main.c -o /tmp/main /tmp/main.c:1:1: warning: data definition has no type or storage class [enabled by default] /tmp/main.c:1:1: warning: type defaults to ‘int’ in declaration of ‘main’ [enabled by default] /tmp/main.c:1:1: warning: ‘main’ is usually a functi…

Not on mac: a.c -> main;

    gcc -nostdlib -std=c89 a.c -o a

    a.c:1: warning: data definition has no type or storage class
    Undefined symbols for architecture x86_64:
         "start", referenced from:
         -u command line option
    ld: symbol(s) not found for architecture x86_64
    collect2: ld returned 1 exit status`

Re: The Shortest Crashing C Program

#43
post #41

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

Edit: deleted because I got to the Google cache and that's what the site was suggesting.

That's essentially where he's going with it, noting that you can even leave off the "=0". But as others here point out there's some question as to how many linkers will actually produce an executable image from that source.

Re: The Shortest Crashing C Program

#44
post #41

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

Edit: deleted because I got to the Google cache and that's what the site was suggesting.

That's essentially where he's going with it, noting that you can even leave off the "=0". But as others here point out there's some question as to how many linkers will actually produce an executable image from that source.

Re: The Shortest Crashing C Program

#45
> Also, global variables in C are initialized to zero implicitly, so this is equivalent:

EDIT: this is wrong, see below.

That's wrong. 'static' variables are initialized to zero. Non-static variables are un-initialized, so they have a "random" value.

See:

$ valgrind ./a.out

==5118== Memcheck, a memory error detector

==5118== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.

==5118== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info

==5118== Command: ./a.out

==5118==

==5118==

==5118== Process terminating with default action of signal 11 (SIGSEGV)

==5118== Bad permissions for mapped region at address 0x600864

==5118== at 0x600864: ??? (in /home/def/a.out)

==5118== by 0x4E54A14: (below main) (in /usr/lib/libc-2.17.so)

Re: The Shortest Crashing C Program

#46

> Also, global variables in C are initialized to zero implicitly, so this is equivalent: EDIT: this is wrong, see below. That's wrong. 'static' variables are initialized to zero. Non-static variables are un-initialized, so they have a "random" value. See: $ valgrind ./a.out ==5118== Memcheck, a memory error detector ==5118== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==5118== Using Valgrind-3.8.1…

But global variables are static.

Re: The Shortest Crashing C Program

#47
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

Originally I thought I'd skip mentioning compiling empty files because doing so without linking separately `gcc` will refuse to link it. I updated the article with a reference to your comment.

Re: The Shortest Crashing C Program

#48

> Also, global variables in C are initialized to zero implicitly, so this is equivalent: EDIT: this is wrong, see below. That's wrong. 'static' variables are initialized to zero. Non-static variables are un-initialized, so they have a "random" value. See: $ valgrind ./a.out ==5118== Memcheck, a memory error detector ==5118== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==5118== Using Valgrind-3.8.1…

But global variables are static.

No, they're not. In fact, if the program used 'static main;' instead, it wouldn't even compile because the 'main' symbol wouldn't be visible by the linker.

Re: The Shortest Crashing C Program

#50

> Also, global variables in C are initialized to zero implicitly, so this is equivalent: EDIT: this is wrong, see below. That's wrong. 'static' variables are initialized to zero. Non-static variables are un-initialized, so they have a "random" value. See: $ valgrind ./a.out ==5118== Memcheck, a memory error detector ==5118== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==5118== Using Valgrind-3.8.1…

Actually it is both. In C, variables with static storage duration are zero initialized.

Global(variables at file scope) and variables with static linkage (i.e. the static keyword) both of have static storage duration.

Post reply on HN