Add more error checking of return values.

This commit is contained in:
Joachim Bauch
2015-12-20 18:53:18 +01:00
parent f02a8e6a38
commit 9df6e7dc93
3 changed files with 27 additions and 16 deletions
+8 -4
View File
@@ -1,5 +1,6 @@
#define WIN32_LEAN_AND_MEAN
#include <assert.h>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
@@ -46,7 +47,8 @@ void LoadFromMemory(void)
{
FILE *fp;
unsigned char *data=NULL;
size_t size;
long size;
size_t read;
HMEMORYMODULE handle;
addNumberProc addNumber;
HMEMORYRSRC resourceInfo;
@@ -63,9 +65,12 @@ void LoadFromMemory(void)
fseek(fp, 0, SEEK_END);
size = ftell(fp);
assert(size >= 0);
data = (unsigned char *)malloc(size);
assert(data != NULL);
fseek(fp, 0, SEEK_SET);
fread(data, 1, size, fp);
read = fread(data, 1, size, fp);
assert(read == static_cast<size_t>(size));
fclose(fp);
handle = MemoryLoadLibrary(data);
@@ -94,8 +99,7 @@ void LoadFromMemory(void)
MemoryFreeLibrary(handle);
exit:
if (data)
free(data);
free(data);
}
int main(int argc, char* argv[])
+8 -4
View File
@@ -1,5 +1,6 @@
#define WIN32_LEAN_AND_MEAN
#include <assert.h>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
@@ -13,7 +14,8 @@ int RunFromMemory(void)
{
FILE *fp;
unsigned char *data=NULL;
size_t size;
long size;
size_t read;
HMEMORYMODULE handle;
int result = -1;
@@ -26,9 +28,12 @@ int RunFromMemory(void)
fseek(fp, 0, SEEK_END);
size = ftell(fp);
assert(size >= 0);
data = (unsigned char *)malloc(size);
assert(data != NULL);
fseek(fp, 0, SEEK_SET);
fread(data, 1, size, fp);
read = fread(data, 1, size, fp);
assert(read == static_cast<size_t>(size));
fclose(fp);
handle = MemoryLoadLibrary(data);
@@ -45,8 +50,7 @@ int RunFromMemory(void)
MemoryFreeLibrary(handle);
exit:
if (data)
free(data);
free(data);
return result;
}
+11 -8
View File
@@ -1,5 +1,6 @@
#define WIN32_LEAN_AND_MEAN
#include <assert.h>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
@@ -60,7 +61,7 @@ BOOL CheckResourceStrings(LPVOID data, DWORD size, const char *first, const wcha
src = (const wchar_t *) (((const char *) data) + strlen(first) + 1);
second_pos = swcsstr(src, second, (size - strlen(first) - 1) / sizeof(wchar_t));
if (second_pos == NULL) {
fprintf(stderr, "ERROR: data doesn't continue with %S\n", second);
fwprintf(stderr, L"ERROR: data doesn't continue with %s\n", second);
return FALSE;
}
@@ -71,8 +72,9 @@ BOOL LoadFromMemory(char *filename)
{
FILE *fp;
unsigned char *data=NULL;
size_t size;
HMEMORYMODULE handle;
long size;
size_t read;
HMEMORYMODULE handle = NULL;
addNumberProc addNumber;
addNumberProc addNumber2;
HMEMORYRSRC resourceInfo;
@@ -91,9 +93,12 @@ BOOL LoadFromMemory(char *filename)
fseek(fp, 0, SEEK_END);
size = ftell(fp);
assert(size > 0);
data = (unsigned char *)malloc(size);
assert(data != NULL);
fseek(fp, 0, SEEK_SET);
fread(data, 1, size, fp);
read = fread(data, 1, size, fp);
assert(read == static_cast<size_t>(size));
fclose(fp);
handle = MemoryLoadLibrary(data);
@@ -204,11 +209,9 @@ BOOL LoadFromMemory(char *filename)
result = FALSE;
}
MemoryFreeLibrary(handle);
exit:
if (data)
free(data);
MemoryFreeLibrary(handle);
free(data);
return result;
}