CS50恢复-为什么我不断出现细分错误?

时间:2020-09-09 18:38:08

标签: c segmentation-fault cs50 recover

我是编程的完整入门者,我真的不明白自己哪里出了问题。我查看了别人的代码并更改了许多内容,但没有任何效果:/任何帮助将不胜感激!

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

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("Usage ./recover image\n");
        return 1;
    }

    FILE *card = fopen(argv[1], "r");

    if (card == NULL)
    {
        printf("ERROR\n");
        return 1;
    }

    int jpeg_num = 0;
    typedef uint8_t BYTE;
    BYTE buffer[512];
    char filename[10];
    FILE *image = NULL;

    while (fread(buffer, sizeof(BYTE), 1, card) == 1)
    {
        if (buffer[0] == 0xff && buffer[1] == 0xd0 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {

            if (jpeg_num > 0)
            {
                fclose(image);
            }

            sprintf(filename, "%03i.jpeg", jpeg_num);
            image = fopen(filename, "w");
            fwrite(buffer, sizeof(BYTE), 1, image);
            jpeg_num++;
        }

        else
        {
            fwrite(buffer, sizeof(BYTE), 1, image);
        }
    }

    fclose(image);
    fclose(card);
    return 0;
}

1 个答案:

答案 0 :(得分:1)

CS50导致使用地址清理器(-fsanitize=address)编译程序,对吗?如果是这样,您应该已经收到以下内容:

ASAN:DEADLYSIGNAL
=================================================================
==4179==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7f280eabf92e bp 0x000000000001 sp 0x7fffc6e02bd0 T0)
==4179==The signal is caused by a READ memory access.
==4179==Hint: address points to the zero page.
    #0 0x7f280eabf92d in _IO_fwrite (/lib/x86_64-linux-gnu/libc.so.6+0x7f92d)
    #1 0x7f281020107f in main /.../a.c:45
    #2 0x7f280ea61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #3 0x7f2810200c79 in _start (/.../a+0xc79)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/lib/x86_64-linux-gnu/libc.so.6+0x7f92d) in _IO_fwrite
==4179==ABORTING

(这是来自gcc而不是clang,但是clang的地址清理器应该提供类似的内容。)

因此,您尝试从地址零读取。这可以是NULL或初始化的指针。我们甚至知道它发生在第45行对fwrite的调用中!

有两个指针传递给fwritebufferimagebuffer是一个自动分配的数组,因此它是一个有效的指针。另一方面,image的初始化如下:

FILE *image = NULL;

是否有可能在到达失败的fwrite调用之前从未更改过?是。不好。