stack smashing detected,程序段错误

stack smashing detected,程序段错误



今天在写程序的时候,突然出现"stack smashing detected"的错误.用经常用的调试方法,GDB,语句输出等均不能定位问题的出处.

先说解决方案:

出现这个问题的原因是:在程序函数中,数组越界访问,在程序运行时没出现问题,但当函数return的时候就会出现上面的错误.

解决办法是:gdb, where命令定位到出错的函数,然后检查函数中数组的长度,使其满足程序的要求.

参考了文档(http://stackoverflow.com/questions/1345670/stack-smashing-detected)上面的描述:

Stack Smashing here is actually caused due to a protection mechanism used by gcc to detect buffer overflow errors. For example in the following snippet:

#include 

void func()
{
    char array[10];
    gets(array);
}

int main(int argc, char **argv)
{
    func();
}

The compiler, (in this case gcc) adds protection variables (called canaries) which have known values. An input string of size greater than 10 causes corruption of this variable resulting in SIGABRT to terminate the program.

To get some insight, you can try disabling this protection of gcc using option  -fno-stack-protector  while compiling. In that case you will get a different error, most likely a segmentation fault as you are trying to access an illegal memory location. Note that -fstack-protector should always be turned on for release builds as it is a security feature.

You can get some information about the point of overflow by running the program with a debugger. Valgrind doesn't work well with stack-related errors, but like a debugger, it may help you pin-point the location and reason for the crash.

Stack Smashing is actually a protection mechanism used by gcc to detect buffer overflow attacks.

An input of string greater than size 10 causes corruption of gcc inbuilt protection canary variable followed by SIGABRT to terminate the program.You can disable this protection of gcc using option即:stack smashing是GCC的一种检测“缓存溢出”的保护机制.当分配的内存不够时,会继续执行;但是在程序结束返回时才出现错误提示



参考 http://blog.csdn.net/haidonglin/article/details/53672208

你可能感兴趣的:(ubuntu/inux,编程)