Valgrind encountered two errors in the following code. I'm not able to get why is this happening even when I close files and free allocs. Please help if you can.
#include <stdio.h>
#include <stdlib.h>
// Returns 1 if buffer is start of a new jpg file
int checkjpg(int buffer);
int main(int argc, char *argv[])
{
if (argc == 1)
{
printf("Correct usage: ./recover file.raw");
return 1;
}
FILE *f = fopen(argv[1], "r");
if (f == NULL)
{
printf("Could not open %s.\n", argv[1]);
return 1;
}
int *buffer[512];
char name[8];
int imgcount = 0;
FILE *img = NULL;
while (fread(buffer, 1, 512, f) == 512)
{
// If this is a jpg file AND its the first one
if ((checkjpg(*buffer) == 1) && imgcount == 0)
{
imgcount++;
sprintf(name, "%03i.jpg", imgcount - 1);
img = fopen(name, "w");
fwrite(&buffer, 1, 512, img);
}
else if (imgcount != 0)
{
// if new jpg file
if (checkjpg(*buffer) == 1)
{
// close previous jpg and create next one
fclose(img);
imgcount++;
sprintf(name, "%03i.jpg", imgcount - 1);
img = fopen(name, "w");
fwrite(&buffer, 1, 512, img);
}
else
{
// write to currently open jpg file
fwrite(&buffer, 1, 512, img);
}
}
}
if (img != NULL)
{
fclose(img);
}
fclose(f);
}
int checkjpg(int buffer)
{
if (buffer == 0xff && (buffer + 1) == 0x8d && (buffer + 2) == 0xff && ((buffer + 3) & 0xf0) == 0xe0)
{
return 1;
}
return 0;
}
According to Valgrind, these are the errors
==4023== Memcheck, a memory error detector
==4023== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4023== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==4023== Command: ./recover card.raw
==4023==
==4023== Invalid read of size 4
==4023== at 0x4009CC: checkjpg (recover.c:67)
==4023== by 0x400800: main (recover.c:29)
==4023== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==4023==
==4023==
==4023== Process terminating with default action of signal 11 (SIGSEGV)
==4023== Access not within mapped region at address 0x0
==4023== at 0x4009CC: checkjpg (recover.c:67)
==4023== by 0x400800: main (recover.c:29)
==4023== If you believe this happened as a result of a stack
==4023== overflow in your program's main thread (unlikely but
==4023== possible), you can try to increase the size of the
==4023== main thread stack using the --main-stacksize= flag.
==4023== The main thread stack size used in this run was 8388608.
==4023==
==4023== HEAP SUMMARY:
==4023== in use at exit: 552 bytes in 1 blocks
==4023== total heap usage: 2 allocs, 1 frees, 4,648 bytes allocated
==4023==
==4023== 552 bytes in 1 blocks are still reachable in loss record 1 of 1
==4023== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4023== by 0x5694E49: __fopen_internal (iofopen.c:65)
==4023== by 0x5694E49: fopen@@GLIBC_2.2.5 (iofopen.c:89)
==4023== by 0x40077C: main (recover.c:14)
==4023==
==4023== LEAK SUMMARY:
==4023== definitely lost: 0 bytes in 0 blocks
==4023== indirectly lost: 0 bytes in 0 blocks
==4023== possibly lost: 0 bytes in 0 blocks
==4023== still reachable: 552 bytes in 1 blocks
==4023== suppressed: 0 bytes in 0 blocks
==4023==
==4023== For counts of detected and suppressed errors, rerun with: -v
==4023== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault
I'm closing the f file and freeing the buffer memory, still this problem.