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

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        fprintf(stderr, "Usage: ./recover <filename>\n");
        return 1;
    }

    char *jpg = argv[1];

    FILE *inptr = fopen(jpg, "r");

    if (inptr == NULL)
    {
        fprintf(stderr, "Couldn't open the file\n");
        return 2;
    }

    unsigned char buffer[512];
    int jcount = 0;
    FILE *img;

    while (fgetc(inptr) != EOF)
    {
        fread(&jpg, 512, 1, inptr);
        char fname[7];
        int stat = 0;

        if (buffer[0] == 0x0ff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {


            if (stat == 1)
            {
                fclose(inptr);
                sprintf(fname, "%03i.jpg", jcount);
                img = fopen(fname, "w");
                jcount++;
            }

            else
            {
                sprintf(fname, "%03i.jpg", jcount);
                img = fopen(fname, "w");
                stat = 1;
                jcount++;
            }
        }
        else
        {
            if (stat == 1)
            {
                fwrite(&buffer, 512, 1, img);
            }

            else
            {

            }
        }
    }
    fclose(inptr);
    fclose(img);

}

Its compiling but I keep getting an error

UndefinedBehaviorSanitizer:DEADLYSIGNAL
==32120==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000008 (pc 0x7f2da1d5a324 bp 0x7ffdba325610 sp 0x7ffdba3252e0 T32120)
==32120==The signal is caused by a READ memory access.
==32120==Hint: address points to the zero page.
UndefinedBehaviorSanitizer:DEADLYSIGNAL
UndefinedBehaviorSanitizer: nested bug in the same thread, aborting.

I don't understand whats going on. NEED HELP

0

The best clue on what's happening is in this line:

==32120==Hint: address points to the zero page.

Something is pointing at memory address 0.

Your next task is to figure out which line is triggering the error. Have you tried running the program through debug50? Set some strategic breakpoints and run from breakpoint to breakpoint to narrow down where the failure is happening. Or, set some printf statements to narrow it down.

Once you have identified which line is failing, then you need to figure out why it is failing. Hint: something points at address 0, or null.

Another hint: Once you figure out where the program fails, it'll suggest where to look for the underlying problem!

It's important that you figure this out. One of the most important skills of a programmer is the ability to debug programs. This can't be overstated!

Once you isolate the issue, if you can't sort it out, please create a new question with the current code.

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

4
  • yeah i debugged it went through the loop couple of times and then exit. Will have to look why is it pointing at null as u said – Varun Beluguli Sep 16 '19 at 13:06
  • otherwise is the code fine? – Varun Beluguli Sep 16 '19 at 13:22
  • yeah i found out that *img is pointing to null so what am i supposed to do? i didnt exactly understand the other hint you gave? Should i end the program if it points at NULL? – Varun Beluguli Sep 16 '19 at 14:00
  • Thanks i got it – Varun Beluguli Sep 16 '19 at 15:53

You must log in to answer this question.

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