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 <stdio.h>
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