0

The code for recover (pset4) works fine, and has passed check50, except for the last check - no memory leaks. Valgrind shoots back:

==5482== Memcheck, a memory error detector
==5482== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5482== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==5482== Command: ./recover card.raw
==5482== 
==5482== 
==5482== HEAP SUMMARY:
==5482==     in use at exit: 472 bytes in 1 blocks
==5482==   total heap usage: 103 allocs, 102 frees, 232,976 bytes allocated
==5482== 
==5482== 472 bytes in 1 blocks are still reachable in loss record 1 of 1
==5482==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==5482==    by 0x4A086CD: __fopen_internal (iofopen.c:65)
==5482==    by 0x4A086CD: fopen@@GLIBC_2.2.5 (iofopen.c:86)
==5482==    by 0x10931E: main (recover.c:41)
==5482== 
==5482== LEAK SUMMARY:
==5482==    definitely lost: 0 bytes in 0 blocks
==5482==    indirectly lost: 0 bytes in 0 blocks
==5482==      possibly lost: 0 bytes in 0 blocks
==5482==    still reachable: 472 bytes in 1 blocks
==5482==         suppressed: 0 bytes in 0 blocks
==5482== 
==5482== For lists of detected and suppressed errors, rerun with: -s
==5482== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

I understand that the issue is on line 41, but not sure I understand what it is. Would really appreciate some help!

Code:

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

typedef uint8_t BYTE;
int BLOCK_SIZE = 512;

int main(int argc, char *argv[])
{

    if (argc != 2)
    {
        printf("./recover [name of a forensic image]\n");
        return 1;
    }

    FILE *file = fopen(argv[1], "r");

    if (file == NULL)
    {
        printf("forensic image cannot be opened for reading\n");
        return 1;
    }

    BYTE buffer[512];
    char *filename = malloc(8*(sizeof(char)));

    int counter = -1;
    FILE *img = NULL;

    while (fread(buffer, 1, BLOCK_SIZE, file) == BLOCK_SIZE)
    {
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            counter++;
            if (counter > 1)
            {
                fclose(img);
            }
            sprintf(filename, "%03i.jpg", counter);
            img = fopen(filename, "w");
        }

        if (img != NULL)
        {
            fwrite(buffer, sizeof(BYTE), 512, img);
        }
    }

    fclose(img);
    fclose(file);
    free(filename);
}

1 Answer 1

1

Whenever valgrind points to an fopen as the source of a memory leak, it almost always means that a file wasn't closed. For each block, it's one file still open.

Looking at your code, and knowing the history, here's a question. When does the code close 000.jpg? See line 36. ;-)

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

1
  • Ah, another silly mistake then. Thank you very much for your help! Commented Jan 21, 2023 at 10:37

You must log in to answer this question.

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