0

I am getting segmentation fault and i was searching on the internet, comparing with other codes, reading this forum, etc. and i still can not fix mine.

Explaining my code from the while:

read the first 512 bytes, then compare the first 4th elements. If the file is different to NULL, then close the previous file and create a new file to write and write 512 elements on it. If the file is open, and the first 4th elements are not a header of jpeg then keep writing on the file. On the case where the first 4th characters are not header of the jpeg and the file is not open, the read the next 512 bytes. Or at least that's how i can understand the behavior.

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

int main(int argc, char *argv[])
{
    if(argc !=2)
    {
        printf("incorrect usage, should be: ./recover imagefile\n");
        return 1;
    }

    FILE *raw_file = fopen(argv[1],"r");
    if(raw_file == NULL)
    {
        printf("could not open %s.\n", argv[1]);
        fclose(raw_file);
        return 2;
    }

    FILE *img = NULL;
    uint8_t buffer[512];
    int counter = 0;
    char filename[8]= {0};


    while(fread(buffer,sizeof(uint8_t),512,raw_file)==512)
    {
        if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3]>= 0xe0 && buffer[0]<= 0xef) )
        {
            if(img != NULL)
            {
                fclose(img);
            }
            sprintf(filename, "%03i.jpg", counter);
            counter++;
            img = fopen(filename,"w");

            fwrite(buffer,sizeof(uint8_t),512,img);

        }else
        {
            if(img != NULL)
            {
                fwrite(buffer,sizeof(uint8_t),512,img);
            }

        }
    }

    fclose(img);
    fclose(raw_file);
}

1 Answer 1

0

Hmmm.... let's examine the indicators here. ;-)

First, no files are being created. That's a big hint.

Now, the seg fault occurs on line 51:

    fclose(img);

It seg faults trying to close this file pointer, but no file was ever created, so this is likely NULL. In fact, it is.

So it's time to look at the code that opens the files. It seems that the code to open a file never gets executed. (Confirmed by putting a printf statement inside the block of code following the if statement.)

But why doesn't that code ever execute? clearly the if statement is never true. Let's look carefully at that statement (reformatted to make it easier to see the issue, ignore the automatic color formatting):

    if
    (
       buffer[0] == 0xff && 
       buffer[1] == 0xd8 && 
       buffer[2] == 0xff && 
      (buffer[3] >= 0xe0 && 
       buffer[0] <= 0xef) 
    )
   

See anything wrong in this statement? ;-)

Spoiler:

There's a typ0.

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

2
  • I am terribly sorry for my question, i solved it thank you very much. Apr 27, 2023 at 13:19
  • No reason to be sorry! Most programmers have this happen to them sooner or later (and hope nobody's looking over their shoulder.) ;-) One gets too close to it and just can't see it. Just accept it and learn from it. Sometimes it helps to just set it aside for a while (overnight is better) and come back to it later with fresh eyes. By the way, the programmers that say they've never done this are just lying!!! :-D Welcome to the club!
    – Cliff B
    Apr 27, 2023 at 21:09

You must log in to answer this question.

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