0

I find this behaviour really strange, please help!

My code shows a segmentation fault when I compile and run it and just opens a blank 000.jpg, recovering no other image.

But when I run debug50 to check which line of my code is buggy, I am able to recover and view image 001.jpg - 049.jpg but still not 000.jpg.

debug50 doesn't locate the segmentation fault, but the command line tells there is one, what change could I make?

Here's my code:

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

typedef uint8_t BYTE;

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

    // remember filenames
    char *infile = argv[1];

    // open input file
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    BYTE *buffer = malloc(512 * sizeof(BYTE));

    char filename[6];
    int imgcount = 0;
    FILE *outptr = NULL;
    bool scanstart = false;

    while(1) {
        int check = fread(buffer, 1, 512, inptr);

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

                if(scanstart == false) {
                    scanstart = true;
                    sprintf(filename,"%03d.jpg" , imgcount);
                    outptr = fopen(filename, "w");
                    if (outptr == NULL)
                    {
                        fclose(inptr);
                        fprintf(stderr, "Could not create %s.\n", filename);
                        return 3;
                    }

                    fwrite(buffer, 512, 1, outptr);
                } else {

                    fclose(outptr);
                    imgcount++;
                    sprintf(filename,"%03d.jpg" , imgcount);
                    outptr = fopen(filename, "w");
                    if (outptr == NULL)
                    {
                        fclose(inptr);
                        fprintf(stderr, "Could not create %s.\n", filename);
                        return 3;
                    }
                    fwrite(buffer, 512, 1, outptr);
                }
            } else {

                //if first image is not found, i.e., scanstart is false, go to start of loop
                if(scanstart == true)
                    fwrite(buffer, 512, 1, outptr);
            }
        } else {
            fclose(outptr);
            imgcount++;
            fclose(inptr);
            break;
        }
    }

    // success
    return 0; }
1

Ok, I did find out the answer to my problem, I allocated size 6 to array filename but in the sprintf() function, memory has to be allocated to the "%" operator and the null value as the copied string should have a "\0" at the end, making the total array size to be minimum 8, the program compiled and ran as expected on making the change.

1
  • Great! Glad you found your own answer. Now, can you please accept your own answer? Otherwise, it'll sit in the unanswered question pool forever. – Cliff B Dec 23 '17 at 6:05

You must log in to answer this question.

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