0

Really clueless to why I get a segmentation fault on this problem, I have a hunch it may be something regarding the creation of the outfile. #include #include

int main(int argc, char *argv[])
    {
        int filenumber =0;
        // ensure proper usage
        if (argc != 2)
        {
            fprintf(stderr, "You didn't enter two command line arguments\n");
            return 1;
        }

        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;
        }

        FILE *outptr = NULL;

        int *card = malloc(512);

        char outfile[8];

        do
        {
        // would I then do something here about freading into the buffer
        // what I just fread into card. I don't understand why I can't just
        // access each part of card like an array


            if (card[0] == 0xff &&
                card[1] == 0xd8 &&
                card[2] == 0xff &&
                (card[3] & 0xf0) == 0xe0)
                {
                    filenumber++;

                    if (filenumber == 1)
                    {

                        //char* outfile = NULL;

                        sprintf(outfile, "%03d.jpg" , filenumber);

                        outptr = fopen(outfile, "w");
                        if (outptr == NULL)
                        {
                            fclose(inptr);
                            fprintf(stderr, "Could not create %s.\n", outfile);
                            return 3;
                        }

                        fwrite(&card,1,512, outptr);
                    }

                    else if (filenumber > 1)
                    {
                        fclose (outptr);

                        //char* outfile = NULL;

                        sprintf(outfile, "%03d.jpg", filenumber);

                        outptr = fopen(outfile, "w");
                        if (outptr == NULL)
                        {
                            fclose(inptr);
                            fprintf(stderr, "Could not create %s.\n", outfile);
                            return 3;
                        }

                        fwrite(&card,1,512, outptr);
                    }
                }


            else if (card[0] ==! 0xff) //&&
            //card[1] =! 0xd8 &&
            //card[2] =! 0xff &&
            //(card[3] & 0xf0) =! 0xe0)
                {

                    if (filenumber > 0)
                    {
                        fwrite(card,1,512, outptr);
                    }
            //else if filenumber is 0 just do nothing, as I am doing
                }
        }
        while (fread(card,1,512,inptr) == 512);

        if (fread(card,1,512,inptr) < 512)
        {
            fclose (inptr);
            fclose (outptr);
        }
    }

1 Answer 1

2

You've declared int* card = malloc(512);

First, you don't want your buffer to be ints, but bytes, otherwise, card[0] will be 4 bytes (the size of an int) and you'll never find any signatures.

Because of this, your do while loop will run until you reach the end of the card. At this point, you call fclose(outptr); even though you've never opened it and you segfault. (You should never close a file unless you've confirmed it is open).

If you run valgrind ./recover card.raw you will see that you have other errors as well, but you will also see where the segfault is happening. This should be something you do to debug this yourself.

==3642== Process terminating with default action of signal 11 (SIGSEGV)
==3642==  Access not within mapped region at address 0x0
==3642==    at 0x5E27994: fclose@@GLIBC_2.2.5 (iofclose.c:54)
==3642==    by 0x420D44: main (recover.c:102)
1
  • I used an unsigned character for my buffer as well as reading and writing (because apparently an unsigned char is one byte) and now, I no longer get segmentation faults.
    – user21860
    Commented Aug 17, 2018 at 18:30

You must log in to answer this question.

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