当我对 char[] 执行 strcpy 时:
char buf[100];
strcpy(buf[], largeInput);
如果 largeInput 的长度超过 100 个字节,我们就会发生缓冲区溢出。
但是我有一个问题,如果 buf
不是 char[]
而是一个 char
指针,是否会有缓冲区也溢出?
我认为,如果 largeInput
足够长,当复制到 char *buf
时,它可能会到达另一个变量的内存区域。但是我不确定这是一个漏洞。
我用了 flawfinder,它指责这样的代码是缓冲区溢出漏洞
char *buf;
strcpy(buf, largeInput);
我只是不确定这是不是误报。
最佳答案
如果我们看到只是这部分代码
char *buf;
strcpy(buf, largeInput);
是undefined behavior因为,您正在尝试写入未初始化的指针。
即使你之前给buf
分配了内存,而且largeInput
的内容比buf
分配的空间多,那么,是的,它也是 UB。 buf
无法自动调整。
但是,FWIW,您始终可以使用 strdup()
来确保安全。
关于c - char[ ] 和 char * 存在缓冲区溢出漏洞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33971414/