Here is my code:

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

//remember: free malloc + fclose files

typedef uint8_t BYTE;

int main(int argc, char *argv[])
    //ensure correct usage
    if (argc != 2)
        printf("Usage: ./recover image");
        return 1;

    //open the memory card file in read mode
    FILE *p_memoryCard = fopen(argv[1], "r");
    if (p_memoryCard == NULL)
        printf("Unable to open file %s", argv[1]);
        return 1;

    //format filename
    int count = 0;
    char p_jpeg_name[8];
    int num_BYTES_read;
    FILE *jpeg = NULL;

    //allocate 512 bytes space for p_buffer
    BYTE *p_buffer = malloc(512 * sizeof(BYTE));

    do  //repeat until end of file

        sprintf(p_jpeg_name, "%03i.jpg", count);

        //read the buffer
        //if num_BYTES_read!=512, then this buffer is the end of the file
        num_BYTES_read = fread(p_buffer, sizeof(BYTE), 512, p_memoryCard);// fread returns # of BYTEs it reads

        //if it is a start of new image, close the old one and open a new one;
        //save the current buffer to new jpeg
        if (p_buffer[0] == 0xFF && p_buffer[1] == 0xD8
            && p_buffer[2] == 0xFF && (p_buffer[3] & 0xF0) == 0xE0 )
            if (jpeg != NULL)
                //close the old jpeg
            jpeg = fopen(p_jpeg_name, "w");//remember to update the file name
            fwrite(p_buffer, sizeof(BYTE), 512, jpeg);
        //if it's not, write the buffer
            fwrite(p_buffer, sizeof(BYTE), 512, jpeg);
    }while(num_BYTES_read == 512);



I have tried to look for out-of-bound referencing or any malloc to be freed, but the segfaullt is still there... Any help on why my code is generating seg-fault or how should find the problem?

*Will Valgrind be helpful? How should I read the result of it?:

~/pset4/recover/ $ valgrind ./recover card.raw
==326== Memcheck, a memory error detector
==326== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==326== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==326== Command: ./recover card.raw
==326== Invalid read of size 4
==326==    at 0x56958BE: fwrite (iofwrite.c:37)
==326==    by 0x40095B: main (recover.c:62)
==326==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==326== Process terminating with default action of signal 11 (SIGSEGV)
==326==  Access not within mapped region at address 0x0
==326==    at 0x56958BE: fwrite (iofwrite.c:37)
==326==    by 0x40095B: main (recover.c:62)
==326==  If you believe this happened as a result of a stack
==326==  overflow in your program's main thread (unlikely but
==326==  possible), you can try to increase the size of the
==326==  main thread stack using the --main-stacksize= flag.
==326==  The main thread stack size used in this run was 8388608.
==326==     in use at exit: 1,064 bytes in 2 blocks
==326==   total heap usage: 3 allocs, 1 frees, 5,160 bytes allocated
==326== 512 bytes in 1 blocks are still reachable in loss record 1 of 2
==326==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==326==    by 0x400856: main (recover.c:34)
==326== 552 bytes in 1 blocks are still reachable in loss record 2 of 2
==326==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==326==    by 0x5694E49: __fopen_internal (iofopen.c:65)
==326==    by 0x5694E49: fopen@@GLIBC_2.2.5 (iofopen.c:89)
==326==    by 0x400806: main (recover.c:19)
==326==    definitely lost: 0 bytes in 0 blocks
==326==    indirectly lost: 0 bytes in 0 blocks
==326==      possibly lost: 0 bytes in 0 blocks
==326==    still reachable: 1,064 bytes in 2 blocks
==326==         suppressed: 0 bytes in 0 blocks
==326== For counts of detected and suppressed errors, rerun with: -v
==326== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault

Think about what happens as the first data block (which has no signature) is processed. The code will drop down to the else code block and attempt to write it to the output file.

Unfortunately, the first output file has not been opened yet. Instant seg fault.

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

| improve this answer | |
  • Thank you so much!! It turns out that I just need to modify the body of else statement a little bit. Now my program works perfectly :) Sorry for the late accept.. – Chen Shujuan Aug 10 at 23:17

You must log in to answer this question.

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