Live data from Hacker News

The Shortest Crashing C Program

llbit.se

81–90 of 94 posts

Re: The Shortest Crashing C Program

#81

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! [1] http://www.muppetlabs.com/~breadbox/software/tiny/teensy.htm...

The way he just keeps pushing further and further pleases the hacker inside me. A recommended read for sure.

Re: The Shortest Crashing C Program

#82
post #77
post #72

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.

"global variables in C are initialized to zero implicitly" NULL pointers will lead to a crash. It would be more interesting to have it as a random pointer, which could do quite anything.

> NULL pointers will lead to a crash.

Not in C. Dereferencing a NULL pointer is undefined behavior, so any of the actions described by the parent would be correct.

Re: The Shortest Crashing C Program

#83

Earlier quoted context omitted.

It can't be a valid C89 program. On many Harvard architecture based microprocessors data pointers and code pointers have differing size.

Different size maybe, but different busses^H^H^H^H^H^Haddress spaces definitely, and using an address on the wrong bus is a sure way to cause problems.

^W will delete the previous word.

Re: The Shortest Crashing C Program

#84
post #73
post #34

Earlier quoted context omitted.

A bad hosting strikes again.

I have bad hosting with good software. Ran flawlessly with 8-15ms generation times on #3 of the HN homepage for a couple hours, only the network latency went up to at peak ~1.2 seconds (got less than 1mbps upload here). The page also executes multiple database queries for each pageload, just like Wordpress. No caching needed for me, it's all about optimization.

What software do you use?

Re: The Shortest Crashing C Program

#85
post #73

Earlier quoted context omitted.

I have bad hosting with good software. Ran flawlessly with 8-15ms generation times on #3 of the HN homepage for a couple hours, only the network latency went up to at peak ~1.2 seconds (got less than 1mbps upload here). The page also executes multiple database queries for each pageload, just like Wordpress. No caching needed for me, it's all about optimization.

What software do you use?

Self written, no framework used. It's a simple blog with quite custom requirements so I figured whynot just build a custom one. It runs on an Intel Atom, 1GB RAM (and there's more to run than a wamp stack). and 832kbps uplink.

As for software, I wrote it for PHP 5.3 (nowadays upgraded to 5.4 though) with MySQL and persistent database connections. The server is Windows 7 with apache 2.4.

Re: The Shortest Crashing C Program

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

Since ANSI/ISO C, a "translation unit" (whatever is left of a file after preprocessing) has to have at least one declaration; a zero length source file won't cut it.

Re: The Shortest Crashing C Program

#87
post #73
post #34

Earlier quoted context omitted.

A bad hosting strikes again.

I have bad hosting with good software. Ran flawlessly with 8-15ms generation times on #3 of the HN homepage for a couple hours, only the network latency went up to at peak ~1.2 seconds (got less than 1mbps upload here). The page also executes multiple database queries for each pageload, just like Wordpress. No caching needed for me, it's all about optimization.

Well, the mistake in this particular case is probably allowing more web application processes than database connections from those processes, which is an easy thing to get wrong.

Re: The Shortest Crashing C Program

#88
post #82
post #77

Earlier quoted context omitted.

"global variables in C are initialized to zero implicitly" NULL pointers will lead to a crash. It would be more interesting to have it as a random pointer, which could do quite anything.

> NULL pointers will lead to a crash. Not in C. Dereferencing a NULL pointer is undefined behavior, so any of the actions described by the parent would be correct.

Is 0 really the same thing the sane thing as 'NULL' in the context of C? If you actually wanted a pointer to the begging of the memory, you would dereference 0, which has the well defined meaning of getting whatever is at memory address 0. When the programming attempts to get that, it is shut down by the system.

Re: The Shortest Crashing C Program

#89
post #82

Earlier quoted context omitted.

> NULL pointers will lead to a crash. Not in C. Dereferencing a NULL pointer is undefined behavior, so any of the actions described by the parent would be correct.

Is 0 really the same thing the sane thing as 'NULL' in the context of C? If you actually wanted a pointer to the begging of the memory, you would dereference 0, which has the well defined meaning of getting whatever is at memory address 0. When the programming attempts to get that, it is shut down by the system.

> Is 0 really the same thing the sane thing as 'NULL' in the context of C?

Yes. I don't have chapter and verse handy but it is in the standard. The bit pattern of NULL is not required to be zero (so memset(&p, 0, sizeof(p)) is not guaranteed to yield null) but it must compare equally to 0 and assigning 0 must produce NULL.

[Edit: OK, in C99 this is covered in 6.3.2.3: Pointers. "An integer constant expression with the value 0, or such an expression cast to type void * , is called a null pointer constant." Then 7.17.3 says that NULL expands to a null pointer constant.]

> If you actually wanted a pointer to the begging of the memory, you would dereference 0,

Yeah, it's really easy to set up an environment where that happens. At one point I was experimenting with writing a small/toy kernel for x86 and I mapped the virtual address 0 to a valid page, and boom, dereferencing NULL did stuff. Not a great idea to set up the page tables that way for obvious reasons, but I'm going to guess that lots of hardware out there will let you do it...

In the old days of 16-bit x86, linear address 0 had the interrupt vector, so as I recall lots of DOS (maybe even Win9x) environments had dereferencing NULL do meaningful (surely confusing) things.

Re: The Shortest Crashing C Program

#90
post #72

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.

Is there any standard-compliant way to crash in C? A call to abort maybe?

Maybe the task should have been formulated as the shortest valid C program that invokes undefined behaviour instead. (Or maybe the task isn't very interesting either way.)

Post reply on HN