I did some research to get to the bottom of the issue, opposed to trying to hide the error. (it is my understanding those compiler errors are there to help us).
if the value of the pointer "COULD" be "NULL", Error:'C6387', is warning you to validate the reference prior to the call to it.
unsigned char* data = (unsigned char*)malloc(sections[i].Size);
fseek(file, sections[i].Offset, SEEK_SET);
fread(data, 1, sections[i].Size, file);
Above you can see that we allocated memory to the pointer, with value of sections[i].Size
, from the sections struct defined earlier, then in the fread(data, 1, sections[i].Size, file);
Here we try to read that data, this is where things could go wrong if there is no value within our *data pointer.
Since we deference the (*)data (if the value we are looking within the pointer doesn't exist) it could cause undefined behavior (UB), or a security vulnerability &/or just crash the program...
We solve this problem by verifying or checking the pointer value prior to the call to read within it, as follows:
unsigned char* data = (unsigned char*)malloc(sections[i].Size);
// Again we allocated the memory to data but the below if
// statement checks for NULL value within our *data,
// then handles graceful shutdown.
if (data == NULL) {
printf(stderr, "Failed to load data to memory");
fclose(file);
return 1;
}
fseek(file, sections[i].Offset, SEEK_SET);
fread(data, 1, sections[i].Size, file);
now the compiler knows to safely close down the program if something has occurred, for which the deference* data pointer is NULL.