Kwantuum
Kwantuum

Reputation: 163

Would linux tell me if there was a stack overflow?

Bear in mind I'm relatively new to C and linux.

For one of my classes I have a project in which we are supposed to find which cities are located in a certain geographical box, we have to use binary search trees though the implementation is up to us. In my particular implementation, when inserting a new element into the tree, I recursively call the insertion function on the appropriate sub-tree.

We've also been told to check our programs using Valgrind, as any memory leaks or errors it would throw would negatively impact our grade. My program runs fine with the cities files we've been given up to 100,000, but at 1,000,000 cities Valgrind throws me over a million errors caused by invalid read/writes, the stack has overflown. It doesn't happen if I run Valgrind with a higher stack size.

When I run the program directly without Valgrind however I get no errors. Would linux tell me if there was a stack overflow? What would be the consequences of such an overflow?

Upvotes: 3

Views: 148

Answers (2)

rici
rici

Reputation: 241721

Would linux tell me if there was a stack overflow?

No, Linux doesn't care if you overflow your stack. However, it does make some attempt to ensure that the memory addresses beyond the end of the stack are unmapped memory, so that a stock overflow will probably segfault. (That depends on the size of each function's stack frame; allocating large arrays on the stack can produce other as symptoms if you are unlucky.)

The C runtime environment probably won't tell you, either, because it would require inserting extra code, which would slow down execution, and then programmers who had taken care to ensure that their stacks won't overflow would complain about having to pay the cost of protecting your code against your bugs. That might sound harsh, but it is basically the C design philosophy; if you don't like it, there are other languages. However, some compilers do allow you to request that extra code be inserted (with GCC, see the -fstack-check option; also see -fstack-limit-* and -fsplit-stack.)

Upvotes: 2

Why not test it? With the following Program, i get sometimes a SIGSEGV and sometimes not:

#include <stdint.h>

uint64_t pos=261950;

int main(void)
  {
    volatile int a; //just some variables to use the stack
    volatile int b; //and avoid too much optimizations
    a=b; b=a;       
    if(pos) 
      {   
        pos--;
        main();
      }   
    return 0;
  }

Valgrind shows a error in all my tests. The value 261950 was found with testing, and should most likely be different on a other installation.

This was tested on GNU/Linux AMD64, Debian 8, without any special settings (i did not disable anything like ASLR or stack smashing protection). The build command was:

gcc -Wall -Wextra 001.c

When the variable pos is bigger, i see always a SIGSEGV-message.

Of course, here nothing bad happen, but you can not be sure how it end up in a more complex program, so avoid a uncontrolled recursion deep.

Upvotes: 2

Related Questions