分割故障,甚至没有运行第一行代码

发布于 2025-02-04 05:37:51 字数 1488 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尹雨沫 2025-02-11 05:37:51

以下是一些不确定行为的好候选人:

    scanf("%d", &size);
    int array[size];
    for (int i = 0; i < size; i++) {
        array[i] = rand() % 1001;
    }

如果大小太大,则说明数百万,用自动存储分配的阵列可能会导致 stack Overflow

    char str[512];
    int i = 0;
    int index = 0;
    for (i = 0; i < size; i++) {
        index += sprintf(&str[index], "%d,", array[i]);
    }

如果用户输入的size大于128,则将数组转换为字符串的代码可能会导致缓冲区溢出,从而导致不确定的行为。

在子进程中:

    puts(addr);

addr指向内存映射的文件内容。该文件不包含零件终结器,因此,如果偶然的大小是页面大小的倍数,则内存映射块将不包含null终结器,puts(addr)将读取块,导致不确定的行为。

请注意,以上都不会导致该过程在第一个printf调用之前失败。

Here are a few good candidates for undefined behavior:

    scanf("%d", &size);
    int array[size];
    for (int i = 0; i < size; i++) {
        array[i] = rand() % 1001;
    }

If size is too large, say greater than a few millions, the array allocated with automatic storage will likely cause a stack overflow.

    char str[512];
    int i = 0;
    int index = 0;
    for (i = 0; i < size; i++) {
        index += sprintf(&str[index], "%d,", array[i]);
    }

If size entered by the user is larger than 128, the code converting the array to a string will likely cause a buffer overflow causing undefined behavior.

In the child process:

    puts(addr);

addr points to the memory mapped file contents. This file does not contain a null terminator, hence if by chance its size is a multiple of the page size, the memory mapped block will not contain a null terminator and puts(addr) will read beyond the end of the block, causing undefined behavior.

Note that none of the above may cause the process to fail before the first printf call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文