为什么当的strcpy()会导致缓冲区溢出没有分割的错吗? [英] Why no segmentation fault when strcpy() causes an buffer overflow?

查看:554
本文介绍了为什么当的strcpy()会导致缓冲区溢出没有分割的错吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main() {
    char *a = "aaaaaaaaaaaaaaaa";
    char b[1];
    strcpy(b, a);
    printf("%s\n", b);
}

在运行时,它打印:

When running, it prints:

aaaaaaaaaaaaaaaa

如果我做*超长,例如,* A =aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,那么就会造成一个段错误。

If I make *a super long, for example, *a="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", then it will cause a segfault.

为什么在第一种情况下没有溢出?

Why there is no overflow in the first case?

推荐答案

当你的程序试图访问一个不属于你的程序的虚拟地址空间中的内存分段故障发生;你原来的复制目标之后,如果你只是简单地覆盖了一点东西不会出现这种情况。

A segmentation fault happens when your program tries to access memory that doesn't belong to your program's virtual address space; this will not happen if you just overwrite a bit of stuff right after your original copy destination.

这篇关于为什么当的strcpy()会导致缓冲区溢出没有分割的错吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆