gpt4 book ai didi

c - 哪些工具可以捕获C中的缓冲区溢出?

转载 作者:太空狗 更新时间:2023-10-29 17:01:20 24 4
gpt4 key购买 nike

所以我有一段简单的代码,它演示了一个简单的缓冲区溢出:

#include <stdio.h>

int main(void)
{
char c[4] = { 'A', 'B', 'C', 'D' };
char d[4] = { 'W', 'X', 'Y', 'Z' };

printf("c[0] is '%c'\n", c[0]);

d[4] = 'Z'; /* Overflow that overwrites c[0] */

printf("c[0] is '%c'\n", c[0]);

return 0;
}

输出:
$ ./a.out
c[0] is 'A'
c[0] is 'Z'

我试着用下面的gcc选项编译这段代码,但它很快就通过了:
gcc -Wall -Wextra -Wformat=2 -Wswitch-default -Wcast-align -Wpointer-arith \
-Wbad-function-cast -Wstrict-prototypes -Winline -Wundef -Wnested-externs \
-Wcast-qual -Wshadow -Wwrite-strings -Wconversion -Wunreachable-code \
-Wstrict-aliasing=2 -ffloat-store -fno-common -fstrict-aliasing \
-Wstack-protector -fstack-protector-all -std=c99 -pedantic -O0 -ggdb3

我也试过放荡和英勇。我原以为libefence会通过,因为它是用来捕获堆上的越界读/写的,但是valgrind通过了我很惊讶。
这段代码不会产生segfault,因为c[4]和d[0]碰巧重叠,我认为正是这导致工具错过它。
那么,外面有什么能抓住这个?在Linux上运行的免费软件会很好。

最佳答案

尝试cppcheck。这对我有效。

关于c - 哪些工具可以捕获C中的缓冲区溢出? ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1736897/

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