我读到的所有内容都让我相信这应该会导致 stack buffer overflow ,但事实并非如此:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
char password[8];
int correctPassword = 0;
printf("Password \n");
gets(password);
if(strcmp(password, "password"))
{
printf ("Wrong password entered, root privileges not granted... \n");
}
else
{
correctPassword = 1;
}
if(correctPassword)
{
printf ("Root privileges given to the user \n");
}
return 0;
}
但这是我的输出:
在这种情况下,testtesttesttesttest显然大于8个字符,并且根据source ,它应该会导致 stack buffer overflow ,但事实并非如此。这是为什么?
最佳答案
读取缓冲区可容纳的字节数并不总是会导致运行时错误,但这是一个非常严重且常见的错误(请阅读本文 about smashing the stack )。正如我从评论中读到的,您添加了 -fno-stack-protector 以使程序不打印 * 检测到堆栈粉碎* 但这不是一个好主意。您应该使用 scanf("%8s",password)
或类似的东西来限制您读取内容的维度。
关于c - 为什么我没有遇到缓冲区溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44320733/