views:

1852

answers:

7

how to create the condition of stack overflow in linux

+3  A: 

There are code samples in the Wikipedia article. Why you'd want to cause one is beyond me...

ceejayoz
Perhaps for testing purposes, or to explore debugging tools?
joel.neely
+11  A: 

a recursive function with no way out should do the trick

pseudo-code, my c is a bit rusty

void stack_overflow()
{
   stack_overflow();
}
Jason
An optimizing compiler will "optimize" this into an infinite loop.
Adam Rosenfield
writing code and declaring it to be pseudo-code, just so you don't have to test it, nice. :)
Georg
+2  A: 

You just need to think about what uses the stack in C.

  • Dynamic memory allocation (with malloc()) uses the heap;
  • Local variables and function call stacks use the stack.

So, all you have to do is exhaust the stack. Either endless recursion on a function or large local variable creation (don't let them be cleaned up though by going out of scope) should do it.

cletus
+1  A: 

With alloca() or strdupa()

dmityugov
+6  A: 

I'd recommend reading the phrack magazine article "Smashing the stack for fun and profit". It also contains sample code.

FrozenFire
A: 

The simplest way is just declare a large enough automatic stack variable. No recursion or alloca needed. Interestingly, this is not a compile-time error. The size needed will vary by platform:

#define SIZE 10000000

int main(int argc, char **argv)
{
    char a[SIZE];
}
Matthew Flaschen
A: 

"how to create the condition of stack overflow in linux"

The same way you'd create a stack overflow on Windows.

Jason's answer might work, but some compilers by optimize it into a loop. I think adding a a parameter will do the trick:

    int Add(int num)
    {
        num += Add(num);
        return num;
    }
Giovanni Galbo