How to debug ‘Stack smashing detected’?

If you read the website you will realize that this is a simple C++ wrapper over a C library.

A typical issue with C library are buffer overruns:

#include <cstring>
#include <cstdio>

int main(int argc, char* argv[]) {
  char buffer[16]; // ought to be sufficient

  strcpy(buffer, argv[1]);
  printf("%s", buffer);
}

Try this program:

> ./test "a"
a
> ./test "abcdefghijklmnoprqstuvwxyz"
???

Because the buffer can only contain 16 characters, the remaining characters will be written past its end. This is stack smashing, and undefined behavior.

A number of implementations of either the runtime library or your OS may detect this situation in some conditions and terminate the program.

Either you are doing something wrong or the library is.

To locate the issue, you could use Valgrind or run your program in a debugger. Alternatively, if your system allows it, you might have a memory dump at the moment the program was killed. You can also view this memory dump in a debugger.

Leave a Comment