0

I have been trying to solve RECOVER from PSET4 and I keep getting a segmentation fault. Any ideas on why this happens? Thank you so much for any help.

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


#define BLOCKSIZE 512
typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    // Check for invalid usage
    if (argc != 2)
    {
        printf("Usage: ./recover image\n");
        return 1;
    }

    // open memory card file
    FILE *file = fopen(argv[1], "r");

    if (!file)
    {
        printf("Usage: ./recover image\n");
        return 1;
    }


    // read file 512 bytes at a time
    BYTE buffer[BLOCKSIZE];

    // count number of images recoverd
    int jpgAmount = 0;

    // jpeg name storage
    char filename[8];

    // pointer
    FILE *openFile = NULL;

    while (fread(buffer, sizeof(BYTE), BLOCKSIZE, file) == 1)
    {

        //check first 4 bytes
        if (buffer[0] == 0xff &&
            buffer[1] == 0xd8 &&
            buffer[2] == 0xff &&
            (buffer[3] & 0xf0) == 0xe0)
        {

            if(openFile != NULL)
            {
                fclose(openFile);
            }

            // name file
            sprintf(filename, "%03i.jpg", jpgAmount);

            // open file
            openFile = fopen(filename, "w");

            // interate number of files collected
            jpgAmount ++;



            if (openFile != NULL)
            {
                // if not a new file, continue writing
                fwrite(buffer, sizeof(buffer), 1, openFile);
            }
        }
    }

    // close any remaining files
    fclose(openFile);
    fclose(file);

    return 0;

}

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

Asking for help...

/etc/profile.d/cli.sh: line 94: 13633 Segmentation fault      valgrind ./recover card.raw

Looks like your program is trying to access areas of memory that it isn't supposed
to access. Did you try to change a character in a hardcoded string? Are you
accessing an element of an array beyond the size of the array? Are you dereferencing
a pointer that you haven't initialized? Are you dereferencing a pointer whose value
is NULL? Are you dereferencing a pointer after you've freed it?
1
   while (fread(buffer, sizeof(BYTE), BLOCKSIZE, file) == 1)  // this is always false
    {
    }
    fclose(openFile); // openFile is NULL and causes segfault. 

You need to fix the loop condition. Your can use this link to debug any segfaults of your code.

| improve this answer | |
  • thank you so much – Alan W Jul 4 at 2:35

You must log in to answer this question.

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