Weirdest way to produce a stack overflow

  • As a programmer you certainly know the error of a stack overflow due to an obvious recursion.
    But there are certainly many weird and unusual ways to get your favourite language to spit that error out.


    1. Must cause a stack overflow which is clearly visible on the error output.

    2. Not allowed to use an obvious recursion.

    Examples of invalid programs:

    // Invalid, direct obvious recursion.
    methodA(){ methodA(); }

    // Invalid, indirect, but obvious recursion.
    methodA(){ methodB(); }
    methodB(){ methodA(); }

    The most creative ways are the best as this a . I.e, avoid boring obvious answers like this:

    throw new StackOverflowError(); // Valid, but very boring and downvote-deserving.

    Even though I accepted an answer now, adding more answers is still okay :)

    I tend to produce by navigating to, though I have been known to query 'stack overflow' on my search engine of choice.

    There are many good (and short!) answers here: I hope nobody will copy those answers.

    @JasonC You don't need to advertise your posts on this question with a comment. The OP owner will get a notification, and anyone who would upvote or such will find the answer without needing to read your comment.

    @Quincunx Sure; sorry.

    Use Internet Explorer. A sure way to catch one :)

    @asgoth explain plz

    @masterX244 I mean, IE can produce javascript errors (e.g. `stackoverflow at line 0`), while the code runs fine in other browsers. My comment was meant to be sarcastic. Sorry if it failed.

    didnt knew that one thats why i asked @asgoth avoiding Internet Exploder since multiple years so the bugs didnt fgound a way into my memory

    Hey does it have to compile? I have given an example of one that would overflow, but compilers will refuse to compile it because the behavior is never intended, not because it is semantically wrong. (C++ - constructor to construct temporary to pass to constructor causes infinite loop at runtime)

    post cause some compilers could accept it; (like the TeX example which relies on certain versions

    The weirdest way to produce a stack overflow is to post a popularity-contest on asking for people to post the weirdest way to produce a stack overflow. The responders, in testing their solutions to the question, will produce a stack overflow. I haven't tested it though, so I can't be sure it works (which is why I didn't post it as an answer).

    #Quine# This is only an idea (one specific implementation in Java can be found here). A quine is a program that produces it's own code. If you take it a step further, you can make the code be saved to file and compiled during run time, then executed. This will not cause a classic StackOverFlow error, but a (much more risky...) disk memory overflow.

    @TimSeguine - that sure sound like recursion to me!

    I'm presuming that the goal is to cause an actual stack overflow, rather than to simply throw a StackOverflowError or whatever your language calls it.

    Drive a Toyota (Hey, wait a minute, my car **is** a Toyota...)

    I used to have a program bug that called a super-method of an overridden method by accident, causing a stack-overflow by the `EventDispatcher` thread, but I can't remember where nor reproduce it.

    Here is the weirdest way to produce a StackOverflow. Get a time machine. Go back to 2008. Create a vote-based, community-run question-and-answer site about programming questions, where users can post their problems and get reliable results. Come back to your current time. Edit this post with the URL of your Q&A site. This should generate a StackOverflow. EDIT: I did it, here is my site:

  • marinus

    marinus Correct answer

    7 years ago


    import sys

    This will cause the interpreter to fail immediately:

    $ cat
    import sys
    $ python
    Exception RuntimeError: 'maximum recursion depth exceeded' in <function _remove at 0x10e947b18> ignored
    Exception RuntimeError: 'maximum recursion depth exceeded' in <function _remove at 0x10e8f6050> ignored

    Instead of using recursion, it just shrinks the stack so it will overflow immediately.

    I do love that :D

    .......... cute

    Cute, but not quite what the original question was aiming for in my opinion.

    @Nit I don't see the problem. What about this solution is unsatisfactory?

    @Nit its not the usualy way of spamming stack :)

    @masterX244 Yes, the whole point of the question is "don't do it the usual way".

    @Plutor do you usually set up a StackOverFlow on purpose ?

    This was only mildly clever the first time it was posted.

    @primo 185 upvotes disagree with you. :)

    @JeffGohlke As a matter of etiquette, answers which are not original to the poster, or have been posted elsewhere on this site _verbatim_, should be marked as community wiki. Also, this question was posted just after a surge of activity from ~600 visits per day, to over 4000 per day, which I believe thoroughly explains the low quality of the highest voted answers.

    We've heard the old joke, the optimist sees a glass half-full, the pessimist sees a glass half-empty, and the programmer shrinks the glass so it gives off a glass overflow error.

    When this code is executed in an interactive Python prompt, the interpreter is unable to even give an explanation for the error. It just gives `RuntimeError` no matter what you enter, even a blank line. If the recursion limit is set to 2, all statements still fail (including blank lines), but it'll give `RuntimeError: maximum recursion limit exceeded`. If the recursion limit is set to 3, anything that indirectly calls a function will fail but a traceback is generated (and blank lines no longer generate errors).

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM