简体   繁体   English

我的代码出现分段错误

[英]I am getting a segmentation fault in my code

I am writing a code that reads information from a memory card (card.raw is the one we are provided but the code uses user input) and extracts the jpegs from it using the signatures that jpegs have of (0xff,0xd8,0xff,0x00 - 0xff).我正在编写一个代码,该代码从 memory 卡中读取信息(card.raw 是我们提供的,但代码使用用户输入)并使用 jpeg 具有的签名(0xff,0xd8,0xff,0x00)从中提取 jpeg - 0xff)。 I am getting a segmentation fault because i am using malloc, but i dont see where i went wrong.我遇到了分段错误,因为我使用的是 malloc,但我看不出哪里出错了。 I am pasting my code here any help would be appreciated.我在这里粘贴我的代码,任何帮助将不胜感激。

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


typedef uint8_t BYTE;

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

    //open inputted file and check for valid file
    FILE *file = fopen(argv[1], "r");
    if (!file)
    {
        printf("Invalid or missing file.");
        return 1;
    }

    BYTE *buff = malloc(512 * sizeof(BYTE));
    int counter = 0;
    FILE *image = NULL;
    char *name = malloc(8 * sizeof(char));

    //loop till end of file reached and read a block of input
    while(fread(buff, sizeof(BYTE), 512, file) == 1 && !feof(file))
    {
        bool foundJPEG = buff[0] == 0xff && buff[1] == 0xd8 && buff[2] == 0xff && ((buff[3] & 0xf0) == 0xe0);

        //check if found jpeg, and open file for writing
        if (foundJPEG)
        {
            sprintf(name, "%03i.jpg", counter);
            image = fopen(name, "w");
        }
        //if image file open, write to it
        if (image != NULL)
        {
            fwrite(buff, sizeof(BYTE), 512, image);
        }
        //if found a jpeg already, close it so new one can be written
        if (foundJPEG && image != NULL)
        {
            fclose(image);
            counter++;
        }

    }

    free(name);
    free(buff);
    fclose(image);
    fclose(file);
    return 0;

}

There are three issues with the code above which are not mentioned in the comments:上面的代码存在三个问题,注释中没有提到:

The return value of fread is not 1 but 512, upon successful read.读取成功后, fread的返回值不是 1 而是 512。 You exchanged the parameters for the blocksize and the blockcount -> fread definition .您交换了 blocksize 和 blockcount -> fread 定义的参数。 Therefore the while loop is not entered.因此不进入while循环。 Don't try to save space with packing to much code into one statement.不要试图通过将大量代码打包到一个语句中来节省空间。 If would be more clever to separate the checks for the fread return value and the EOF and use a do... while() loop, instead.如果将fread返回值和 EOF 的检查分开并使用do... while()循环会更聪明。 Then you had the chance of seeing this issue in the debugger.然后你有机会在调试器中看到这个问题。 This was exactly what i have done and how i found this out.这正是我所做的以及我是如何发现这一点的。

The second issue is that you close the image after rescuing the first 512 bytes, but you do not reset the file pointer image back to NULL along with the fclose statement.第二个问题是您在抢救前 512 个字节后关闭了图像,但您没有将文件指针image重置回NULL以及fclose语句。 As a consequence, the code would repeatedly write to an a file which is closed until a new block with a jpg header is found.结果,代码将重复写入一个关闭的文件,直到找到一个带有 jpg header 的新块。

The third issue is that you only rescue the first 512 bytes of the jpg but not the whole jpg.第三个问题是你只拯救了 jpg 的前 512 个字节,而不是整个 jpg。 You need to scan the input stream for the jpg end indicator FF D9 and copy bytes until it is found.您需要扫描输入 stream 以查找 jpg 结束指示符FF D9并复制字节,直到找到为止。 -> jpg format -> jpg 格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM