c - 为什么我没有遇到缓冲区溢出?

标签 c stack-overflow

我读到的所有内容都让我相信这应该会导致 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;
}

但这是我的输出:

description

在这种情况下,testtesttesttesttest显然大于8个字符,并且根据source ,它应该会导致 stack buffer overflow ,但事实并非如此。这是为什么?

最佳答案

读取缓冲区可容纳的字节数并不总是会导致运行时错误,但这是一个非常严重且常见的错误(请阅读本文 about smashing the stack )。正如我从评论中读到的,您添加了 -fno-stack-protector 以使程序不打印 * 检测到堆栈粉碎* 但这不是一个好主意。您应该使用 scanf("%8s",password) 或类似的东西来限制您读取内容的维度。

关于c - 为什么我没有遇到缓冲区溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44320733/

相关文章:

c - 我向不存在的IP发送tcp syn数据包,但没有得到响应

java - 为什么这个 toString() 方法会导致 StackOverFlowError?

java - StackOverFlowError SpannableString 是什么意思?

c - 提高小数 double 变量的精度

c++ - 创建具有指定字符数的字符串

java - Android 因 java.lang.stackoverflowError 崩溃

java - 从 JavaScript 调用 Java 时出现不可恢复的 stackoverflow 错误

java - 如何将 digamma 函数重写为非递归函数

Python C-Api...如何从 C 创建 "IntEnum"对象

c - 尝试轮询 MSP430 的键盘输入