gpt4 book ai didi

arrays - Insure++ 会检测堆上 C 结构中的数组溢出吗?

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

我一直在努力寻找最好的程序来检测 C 程序中的缓冲区溢出。特别是我希望检测堆上 C 结构中存在的 char 数组的溢出。

例如:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

int main()
{
struct mystruct
{
char fill[4];
char g[4];
char h[4];
};

/* A structure on the heap */
struct mystruct *strheap =
(struct mystruct *) malloc(sizeof(struct mystruct));

memset(strheap, 0, sizeof(struct mystruct));

printf("Before overrun array but not structure\n");
sprintf(strheap->fill, "12345678901");

printf("Before overrun array and structure but only by 1 byte\n");
sprintf(strheap->g, "12345678");

printf("Before free\n");
free(strheap);
printf("Before return\n");
return 0;
}

Insure++能检测出这个测试程序中的两次数组溢出吗?

最佳答案

根据经验,是的。前提是您确实溢出了数组(末尾后的一个或两个元素),并且没有在末尾后疯狂取消引用 waaaaay。

哦,等等,我误解了这个问题,以为你问的是结构数组。

在您的情况下,我认为它会检测您是否访问越界索引,但不会检测到库函数内下一个成员的溢出(不会发生索引的额外检测)。只有当库函数写入超出对象(整个结构,甚至可能是包含该结构的结构/数组)时,才能检测到库函数缓冲区溢出。

一方面,这是完全合法合理的

sizeof mystruct one, two;
memcpy(&one, &two, sizeof (struct mystruct));

复制所有四个数组。 Insure++ 不会标记这一点。

事实上,您问题中的 memset 做同样的事情。

关于arrays - Insure++ 会检测堆上 C 结构中的数组溢出吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8670363/

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