Write a perfectly legal code in a decent language of your choice whose compiling will either crash the compiler or send it into an infinite loop (infinite compile time).

Restrictions:

• Use a standard language that is used in real world.
• Use a standard, well-developed compiler (no answers like "I wrote my C compiler that crashes on everything").
• The code must be legal in the language (so most likely you'll have to exploit a compiler or a language bug).
• Give your compiler version and options used so that others can replicate it.
• Explain why the compiler crashed, if possible.

Have fun :)

-
Could you elaborate on what you mean by "crash"? –  Mr. Llama Sep 6 '12 at 19:45
@GigaWatt I mean that the compiler stops in an uninteded way. Neither by successfully compiling the input nor by issuing an error message. It has to really crash, like segfault, eating up all the memory, throwing an unchecked exception etc. –  Petr Pudlák Sep 6 '12 at 19:50

I'm pretty sure it's been fixed now, but it used to be that you could crash the Java compiler (or, crash Eclipse) by writing

class Foo {
static double d = 2.2250738585072012e-308;
}

http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/

Actually, according to that page, the compiler will just hang, not crash. Still, I thought that was pretty fun.

-

My favorite solution for GHC:

xx :: Bad a -> a
xx (x@(C x')) = x' x

omega :: a
omega = xx (C xx)

main = omega

For GHC 6.12.1 both ghci Bad.hs and ghc Bad.hs loop infinitely. GHC 7.4.1 loops infinitely when ghc -O2 Bad.hs is executed.

Explanation: omega is defined using an infinite recursion (the only way it can inhabit any type). Compiler's inliner sees xx as a simple, non-recursive function, so it tries to inline it in the definition of omega. It results in (\x@(C x') -> x' x) (C xx). Seeing a pattern match on a constructor the compiler tries to reduce it, getting xx (C xx) again and loops. The trick is that xx is actually recursive, but the recursion is hidden within the data type.

Note: While writing the puzzle, I forgot I left GHC running in the infinite loop. It took all my memory, crashed Firefox and I barely managed to kill it without hard reset.

-
+1 just for the trouble you went through for the answer :P –  UnkwnTech Sep 6 '12 at 21:13
@UnkwnTech :-) Actually I discovered this by an accident when trying to implement recursion using a recursive data type only. –  Petr Pudlák Sep 6 '12 at 21:17

how about if you can crash the IDE by typing in code?

in any Microsoft Office application, try this:

ALT+F11 to get to the VBA window, then try the following code

sub foo()
dim v(1 to 3, 1 to 3)
redim preserve v(,1 to 5)

and behold:

You can simply type redim preserve v(,1 to 5) into the immediate window, and it will crash after you press ENTER !

-
nice, but more like "crash your favourite interpreter" –  mbx Sep 7 '12 at 20:16
Could I get a quick rundown on why this works? –  Mr. Llama Jan 7 '13 at 16:23
@GigaWatt, it's discussed in a little more depth here, but it appears that the IDE cannot cope with errors (unexpected symbol , and expected ,) –  Sean Cheshire Jan 7 '13 at 20:28

This is easy in any dependently-typed language. Type-checking general dependent types is undecidable since it may require arbitrarily complex computations (Turing-complete). You can simply encode in a dependent type a too-large value. Then the type-checker will use all available memory and crash. For instance, in Coq, ReyCharles gives the example of Compute 70000., which causes the type-checker to construct a giant Peano numeral and crash.

In more common languages that support some sort of macro expansion or metaprogramming, you can do something similar. For example, you can use all available memory in C:

#include <stdio.h>
#define a printf("%s", "Hello, world!\n");
#define b a a a a a a a a a a a a a a a a
#define c b b b b b b b b b b b b b b b b
#define d c c c c c c c c c c c c c c c c
#define e d d d d d d d d d d d d d d d d
#define f e e e e e e e e e e e e e e e e
// ...
#define z y y y y y y y y y y y y y y y y
int main() { z }

The D programming language allows compile-time function execution. This can be used to compute something at compile time that is too large to fit in memory. Something similar can be achieved using C++ template metaprogramming.

In XML (not a compiled programming language, but an XML processor is analogous to a compiler), expanding entities can make the processor run out of memory:

<?xml version="1.0"?>
<!DOCTYPE lolz [
<!ENTITY lol "lol">
<!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
<!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
...
]>
<lolz>&lol999;</lolz>

This is called the billion laughs attack.

-
+1 for the "billion laughs attack" - what a name –  Bojangles Apr 21 '13 at 6:57

## Perl (15)

BEGIN{1while 1}

This creates an infinite loop at compile time:

A BEGIN code block is executed as soon as possible, that is, the moment it is completely defined, even before the rest of the containing file (or string) is parsed.

(from perlmod)

And that's why Perl isn't able to complete parsing the code. This doesn't terminate:

$perl -MO=Deparse -e 'BEGIN{1while 1}' - ## J This segfaults the J interpreter (at least on Linux): 15!:1[3#2 It tries to read from memory address 2. Interestingly, if you try it with 0 or 1, you get domain error. - TeX \def\x{\x}\x TeX is a macro-expansion language. Here we define the expansion of the macro \x to be \x again, and then we add afterwards an invocation of \x. TeX gets stuck endlessly replacing \x with \x. - Note: this isn't the shortest way to achieve this. TeX has a notion of "active characters", which essentially are characters that are treated as macro names. So you can shave off 3 characters from this. – Hammerite May 7 '14 at 7:54 ### C# Found this on a stackoverflow question: using System; using System.Linq; public class Test { public static void Main() { Enumerable.Range(0, 1).Sum(a => Enumerable.Range(0, 1).Sum(b => Enumerable.Range(0, 1).Sum(c => Enumerable.Range(0, 1).Sum(d => Enumerable.Range(0, 1).Sum(e => Enumerable.Range(0, 1).Sum(f => Enumerable.Range(0, 1).Count(g => true))))))); } } The compiler eventually will crash. The issue seems related to type inference and/or lambda generation combined with overload resolution. - ### Common Lisp Macros make it easy: (defmacro loop-forever () (loop for x from 0 collecting x)) (defun compile-me () (loop-forever)) Compiling compile-me calls loop-forever, which exhausts heap memory during its expansion and crashes the compiler. If you just want to make the compiler hang indefinitely, then this definition of loop-forever will do it: (defmacro loop-forever () (loop)) This should work using any CL implementation, unless yours is extremely clever and can detect simple infinite loops, but I seriously doubt any do this. Full protection against this is impossible, of course. - meh. Lisp makes writing compile-time infinite loops too easy. Now if you had actually crashed the compiler... – Jan Dvorak May 3 '14 at 15:57 Smalltalk (Squeak dialect, version 4.x) Very easy, just evaluate this, or accept a method with this literal 1.0e99999999999999999999 It will try to evaluate the power of 10 in Large Integer arithmetic, just for correctly rounding inf Tsss ;) - Do you need more than four 9s? – Joe Z. May 3 '14 at 17:57 PHP 5.3.1 (Segfaults interpreter) (Bug 50261, fixed in 5.3.3) class testClass { function testClass () { echo 'Output string!'; } } class testClass2 extends testClass { function __construct () { call_user_func(array('parent', '__construct')); } } new testClass2; This one was a bit of a problem, because the code above was common in a lot of the code I was working with, making this a fairly widespread issue for us. (If I recall correctly, at one point this was the only way to call parent constructors in PHP.) - # Perl This defines operator overloading at compile time, and runs code at compile time which adds the instances of class together. package MyAmazingClass; use 5.010; use overload '+' => sub { my ($first, $second) = @_; return$first + $second; }; sub new { my$self = shift;
return bless {}, $self; } # BEGIN runs code at compile time BEGIN { my$instance = MyAmazingClass->new;
my $sum =$instance + $instance; say$sum;
}

Output:

fish: Job 1, 'perl' terminated by signal SIGSEGV (Address boundary error)
-

This is my original and concise method to crash GolfScript:

{1.}do

What this does is set up a forever loop that keeps on pushing 1 onto the stack until memory runs out.

In C/C++, I believe that this original piece of code would crash the compiler:

#define a bb
#define b aa
int main(){a}

This would get the compiler stuck doubling the amount of a's and turning them into b's and vice versa, so the compiler would pretty soon run out of memory and crash.

Another one is for batch on Windows, if completely freezing the computer rather than just the batch script itself counts. You should type in the following:

:a
start %0
goto a

This gets into an infinite loop of making copies of itself, which make copies of themselves and so on. This would most likely eventually crash your computer if you ran this little bit of code.

One last one is a VBS bomb. It is another bomb, like the last one, but it instead opens an infinite amount of dialog boxes.

set oshell = wscript.createobject("wscript.shell")
do
oshell.run "wscript " & wscript.scriptname
msgbox "blah"
loop

This continuously creates a copy of itself and opens up a message box in an infinite loop, which the clones do as well. Running these last two programs is not recommended, as they can freeze your computer and cause you to have to hard boot your computer.

Note that I came up with all of these programs myself.

-