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

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值