分段错误(c语言)

segmentation fault (c language)

在 CS50 课程的第 4 周尝试从 raw 文件中恢复 jpgs 时,我遇到了这个 seg 错误,我无法理解它的原因或原因,所有我可以看出,根据 valgrind110 导致此问题

我是c语言的新手

int main(int argc, char *argv[])
{
    // check if one command arg is passed
    if (argc > 2 )
    {
        printf("Usage: ./recover image\n");
        return 1;
    }

    // try to open file to read from
    FILE *file = fopen(argv[1], "r");
    if (!file)
    {

        return 1;
    }


    BYTE bytes[512];
    int x =0 ;
    char num[10];


    FILE* output =NULL;

    while (fread(&bytes, 512, 1, file) == 1)
    {
        // first step create file name 
        sprintf(num, "%03i.jpg",x);

        // check for start of jpg file 
        if (bytes[0]==0xff && bytes[1] ==0xd8 && bytes[2]== 0xff && (bytes[3] & 0xf0) == 0xe0 )
        {   
            printf("Found file {%i}", x);
            output = fopen(num , "w");
            fwrite(&bytes ,512 ,1 ,output);
            x++;
        }
        else
        {
            fwrite(&bytes , 512 , 1 , output);
        }
    }
    return 0;
}

valgrind 的输出

==12934== Invalid read of size 4
==12934==    at 0x4A1C521: fwrite (iofwrite.c:37)
==12934==    by 0x4014CA: main (recover.c:110)
==12934==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==12934== 
==12934== 
==12934== Process terminating with default action of signal 11 (SIGSEGV)
==12934==  Access not within mapped region at address 0x0
==12934==    at 0x4A1C521: fwrite (iofwrite.c:37)
==12934==    by 0x4014CA: main (recover.c:110)
==12934==  If you believe this happened as a result of a stack
==12934==  overflow in your program's main thread (unlikely but
==12934==  possible), you can try to increase the size of the
==12934==  main thread stack using the --main-stacksize= flag.
==12934==  The main thread stack size used in this run was 8388608.
==12934== 
==12934== HEAP SUMMARY:
==12934==     in use at exit: 472 bytes in 1 blocks
==12934==   total heap usage: 2 allocs, 1 frees, 4,568 bytes allocated
==12934== 
==12934== LEAK SUMMARY:
==12934==    definitely lost: 0 bytes in 0 blocks
==12934==    indirectly lost: 0 bytes in 0 blocks
==12934==      possibly lost: 0 bytes in 0 blocks
==12934==    still reachable: 472 bytes in 1 blocks
==12934==         suppressed: 0 bytes in 0 blocks
==12934== Rerun with --leak-check=full to see details of leaked memory
==12934== 
==12934== For lists of detected and suppressed errors, rerun with: -s
==12934== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
zsh: segmentation fault  valgrind ./recover card.raw

改变

else
        {
            fwrite(&bytes , 512 , 1 , output);
        }

else if (output)
        {
            fwrite(&bytes , 512 , 1 , output);
        }

生成图片 感谢 Marek R 提到 if file is opened comment

拿这个

int main(int argc, char *argv[])
{
    // check if one command arg is passed
    if (argc > 2 )
    {
        printf("Usage: ./recover image\n");
        return 1;
    }

    // try to open file to read from
    FILE *file = fopen(argv[1], "r");
    if (!file)
    {

        return 1;
    }


    BYTE bytes[512];
    int x =0 ;
    char num[10];


    FILE* output =NULL;

    while (fread(bytes, 512, 1, file) == 1)
    { 
        // check for file start 
        if (bytes[0]==0xff && bytes[1] ==0xd8 && bytes[2]== 0xff && (bytes[3] & 0xf0) == 0xe0 )
        {   
            if (output)
            {
                fclose(output);
            }
            // create file name
            sprintf(num, "%03i.jpg",x);
            printf("Found file {%i}\n", x);
            output = fopen(num , "w");
            fwrite(&bytes ,512 ,1 ,output);
            x++;
        }
        else if (output)
        {
            fwrite(bytes , 512 , 1 , output);
        }
    }  
    if(output)
    {
        fclose(output);
    }
    return 0;
}

注意使用 fclose(output); 来关闭文件描述符,如 Marek R

所述