0

Finished the problem but when running check50 gets segmentantion faultand running ./recover card.raw .

The error code is the following:

:) recover.c exists.

:) recover.c compiles.

:) handles lack of forensic image
:( recovers 000.jpg correctly

    failed to execute program due to segmentation fault

:( recovers middle images correctly

    failed to execute program due to segmentation fault

:( recovers 049.jpg correctly

    failed to execute program due to segmentation fault

The code is the following:

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    //Check command-line argument correct
    if (argc != 2)
    {
        printf("Usage: ./record image\n");
        return 1;
    }

    // Open input file
    FILE *inputfile = fopen(argv[1], "r");
    if (inputfile == NULL)
    {
        printf("Could not open %s.\n", argv[1]);
        return 1;
    }

    // Creat variable to count the number of jpegs
    int numberofjpeg = 0;

    // Create buffer and allocating name for jpeg, create FILE *img
    BYTE buffer[512];
    char *namejpeg = malloc(8 * sizeof(char));
    FILE *img = NULL;

    // Read the a 512 Bytes chunk of memory and allocate in the buffer
    while (fread(buffer, sizeof(BYTE), 512, inputfile))
    {
        // Check the 4 first signatures Bytes of header
        if (buffer[0] == 0xff && buffer[1] == 0xff && buffer[3] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            // Close the image file finished if there is one open
            if (numberofjpeg != 0)
            {
                fclose(img);
            }
            else
            {
            // Creating the new file jpeg file with correct name
            sprintf(namejpeg, "%03i.jpeg", numberofjpeg);
            img = fopen(namejpeg, "w");

            // Iterate the number of jpegs
            numberofjpeg++;

            // Writing the atual block of the image
            fwrite(buffer, sizeof(BYTE), 512, img);
            }
        }
        else
        {
            // Writing the atual block of the image
            fwrite(buffer, sizeof(BYTE), 512, img);
        }
    }
    // Close the files and free the allocated spaces
    fclose(inputfile);
    fclose(img);
    free(namejpeg);
    free(buffer);
}

If someone could help me, I would be very please, thank you.

2

The sprintf() function always inserts a trailing NULL, which needs to be accounted for during the malloc. Overflowing the allocated memory can be the cause of a segfault. With 3 digits, a dot, a 4 char extension and a NULL, try 9 bytes.

1

@juls is correct in saying that there's not enough memory allocated but that's not the real root problem.

The root problem here is that the program spec hasn't been followed. The output files are supposed to have the file extension of "jpg", not "jpeg" That extra "e" is causing a spillover that will cause problems.

However, none of that has anything to do with the seg fault.

The seg fault is caused by a logic error. If the first block processed doesn't have a signature (it doesn't), then the code will drop past the signature block code and go directly to the fwrite call in the else clause. Since no output file has been opened (img is NULL), trying to use img causes a seg fault.

BTW, there's a typo in your signature test too.

Attention to detail is the mark of a good programmer. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • I correct the typo in the signature test. I corrected the jpeg file extension. I changed the else that direct called fwrite for a else if (! (image==NULL)). But I am still getting segmentation fault. What seems to be the problem? – João Victor Schiavon Mar 3 at 20:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .