gpt4 book ai didi

c - gcc 的 -fstack-protector 选项如何防止堆栈崩溃?

转载 作者:太空宇宙 更新时间:2023-11-04 02:26:08 25 4
gpt4 key购买 nike

我一直遇到堆栈崩溃问题,而且我很难找到原因。堆栈粉碎错误只是偶尔发生,而且只发生在程序执行的最后阶段。当我使用 gcc 的“fstack-protector”选项编译它时,它也完全停止发生。我想知道使用“fstack-protector”选项是否是一个实际的解决方案,或者我是否只是在隐藏问题?我会发布代码,但它有 3000 行长,我不确定代码的哪一部分负责。

最佳答案

此选项不会防止堆栈粉碎,而是检测它并停止程序。

来自 gcc 手册页:

-fstack-protector

Emit extra code to check for buffer overflows, such as stack smashing attacks. This is done by adding a guard variable to functions with vulnerable objects. This includes functions that call "alloca", and functions with buffers larger than 8 bytes. The guards are initialized when a function is entered and then checked when the function exits. If a guard check fails, an error message is printed and the program exits.

你仍然有一个溢出问题,但是添加保护变量显然掩盖了这个问题。如果您在 valgrind 下运行您的程序它应该能够检测到正在发生的事情。

关于c - gcc 的 -fstack-protector 选项如何防止堆栈崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50955249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com