检测到堆栈粉碎 [英] Stack smashing detected

查看:33
本文介绍了检测到堆栈粉碎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行我的 a.out 文件.执行后程序运行一段时间然后退出并显示消息:

I am executing my a.out file. After execution the program runs for some time then exits with the message:

**** stack smashing detected ***: ./a.out terminated*
*======= Backtrace: =========*
*/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x48)Aborted*

造成这种情况的可能原因是什么,我该如何纠正?

What could be the possible reasons for this and how do I rectify it?

推荐答案

这里的 Stack Smashing 其实是 gcc 用来检测缓冲区溢出错误的保护机制造成的.例如在以下代码段中:

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();
}

编译器(在本例中为 gcc)添加具有已知值的保护变量(称为金丝雀).大小大于 10 的输入字符串会导致此变量损坏,从而导致 SIGABRT 终止程序.

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.

为了深入了解,您可以在编译时尝试使用选项 -fno-stack-protector 禁用 gcc 的这种保护.在这种情况下,您将收到不同的错误,很可能是在您尝试访问非法内存位置时出现分段错误.请注意,-fstack-protector 应始终为发布版本打开,因为它是一项安全功能.

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.

您可以通过使用调试器运行程序来获取有关溢出点的一些信息.Valgrind 不能很好地处理与堆栈相关的错误,但就像调试器一样,它可以帮助您确定崩溃的位置和原因.

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.

这篇关于检测到堆栈粉碎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆