TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Shortest Crashing C Program

270 pointsby cfjalmost 12 years ago

23 comments

AlexanderDhoorealmost 12 years ago
This reminds me of "A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux" [1]. The author tries to create the smallest possible elf executable possible. You would think it'd be easy... :) go read it. Very cool!<p>[1] <a href="http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html" rel="nofollow">http://www.muppetlabs.com/~breadbox/software/tiny/teensy.htm...</a>
评论 #5765019 未加载
femtoalmost 12 years ago
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:<p>$ touch a.c<p>$ gcc -c a.c<p>$ ld a.o<p>ld: warning: cannot find entry symbol _start; defaulting to 0000000000400078<p>$ ./a.out<p>Segmentation fault
评论 #5762578 未加载
评论 #5762285 未加载
评论 #5762497 未加载
评论 #5766040 未加载
评论 #5762491 未加载
评论 #5762746 未加载
to3malmost 12 years ago
The explanation is not quite correct - execution starts at &#38;main rather than the address given by the value of main. On VC++, at least - well, on my PC anyway - the process halts because the data segment doesn't have the execute bit set. It isn't trying to run code at address 0.<p>(If execution of bytes in the data segment were possible, which I'm sure it used to be, then you'd still likely get a crash, but it's not guaranteed. (uint32_t)0 is a valid sequence of instructions - it's ADD BYTE PTR [EAX],AL - and so if EAX contained a valid value then it would execute without a problem. Then, if the following byte were 0xC3 (RET) then the program would execute. OK, so that's all rather unlikely, but you have to bear these things in mind. So I think 0xCC (INT 3) would be a better choice.)
评论 #5763727 未加载
评论 #5762971 未加载
dysocoalmost 12 years ago
Seems to work really well: It even crashed the website.
评论 #5762771 未加载
评论 #5762306 未加载
评论 #5762265 未加载
Jabblesalmost 12 years ago
Who says it will crash? Could run very nicely, printing a list of prime numbers, or write poetry, or anything else that undefined behaviour encompasses.
评论 #5764719 未加载
评论 #5766846 未加载
alcuadradoalmost 12 years ago
It seems to be down, google cache: <a href="http://webcache.googleusercontent.com/search?q=cache:4FhUcns72Z4J:llbit.se/%3Fp%3D1744+&#38;cd=1&#38;hl=es-419&#38;ct=clnk&#38;gl=ar" rel="nofollow">http://webcache.googleusercontent.com/search?q=cache:4FhUcns...</a>
评论 #5762272 未加载
bbanycalmost 12 years ago
The first IOCCC winner declared main as a short[] of VAX machine code: <a href="http://www.ioccc.org/1984/mullender.c" rel="nofollow">http://www.ioccc.org/1984/mullender.c</a><p>You could probably do the same thing in x86 and it'd work on a modern compiler.
评论 #5762364 未加载
deweerdtalmost 12 years ago
&#62; Also, global variables in C are initialized to zero implicitly, so this is equivalent:<p>EDIT: <i>this</i> is wrong, see below.<p>That's wrong. 'static' variables are initialized to zero. Non-static variables are un-initialized, so they have a "random" value.<p>See:<p>$ valgrind ./a.out<p>==5118== Memcheck, a memory error detector<p>==5118== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.<p>==5118== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info<p>==5118== Command: ./a.out<p>==5118==<p>==5118==<p>==5118== Process terminating with default action of signal 11 (SIGSEGV)<p>==5118== Bad permissions for mapped region at address 0x600864<p>==5118== at 0x600864: ??? (in /home/def/a.out)<p>==5118== by 0x4E54A14: (below main) (in /usr/lib/libc-2.17.so)
评论 #5762815 未加载
评论 #5762787 未加载
评论 #5762743 未加载
themattrixalmost 12 years ago
You can go even shorter if you cheat:<p><pre><code> $ cat short.c M $ gcc -DM='main;' short.c -o short $ ./short Segmentation fault</code></pre>
danielsamuelsalmost 12 years ago
The Shortest Crashing Wordpress Site
_kushagraalmost 12 years ago
The site seems down, "Error establishing a database connection"
评论 #5762365 未加载
jstanleyalmost 12 years ago
I'm not convinced this is a C89 program. It is only an "accident" that the linker doesn't know about types.<p>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).
评论 #5762274 未加载
评论 #5762348 未加载
marshrayalmost 12 years ago
How about:<p><pre><code> main(){*(int*)0=0;} </code></pre> or:<p><pre><code> main(){*""=0;} </code></pre> or:<p><pre><code> main(){main();}</code></pre>
评论 #5762504 未加载
评论 #5762545 未加载
评论 #5762640 未加载
joeyhalmost 12 years ago
Seems appropriate that the default C program, as it were, segfaults.
stinosalmost 12 years ago
"address 0, which is not an address that we have access to"<p>if I'm not mistaken, there are platforms like TI C600 dsps for which 0 is the start of the usable address space
sfvisseralmost 12 years ago
Interesting. We tried to do the same for Haskell. The shortest we could come up with:<p>import Unsafe.Coerce;main=unsafeCoerce()1
评论 #5763473 未加载
webreacalmost 12 years ago
With visual studio V6.0 (AFAIR), I made a short program that crashed the compiler:<p>int a;::a::b();
sbanachalmost 12 years ago
It's also the shortest C program that you can link at all.
评论 #5762637 未加载
qzncalmost 12 years ago
"The shortest crashing C89 program" to be more precise. :)
Trufaalmost 12 years ago
And interesting question, what would be the shortest not crashing C program?<p>main(){}<p>??
评论 #5763114 未加载
hkmurakamialmost 12 years ago
reminds me of the recent fad of TAS (tool assisted speed run) videos of "fastest crash" of video games.
stefap2almost 12 years ago
Shortest crashing website: Error establishing a database connection
kghosealmost 12 years ago
for those who see the server crashing: The program is in C89<p>main;