Skip to content

Commit 9df6e7d

Browse files
committed
Add more error checking of return values.
1 parent f02a8e6 commit 9df6e7d

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

example/DllLoader/DllLoader.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#define WIN32_LEAN_AND_MEAN
22

3+
#include <assert.h>
34
#include <windows.h>
45
#include <tchar.h>
56
#include <stdio.h>
@@ -46,7 +47,8 @@ void LoadFromMemory(void)
4647
{
4748
FILE *fp;
4849
unsigned char *data=NULL;
49-
size_t size;
50+
long size;
51+
size_t read;
5052
HMEMORYMODULE handle;
5153
addNumberProc addNumber;
5254
HMEMORYRSRC resourceInfo;
@@ -63,9 +65,12 @@ void LoadFromMemory(void)
6365

6466
fseek(fp, 0, SEEK_END);
6567
size = ftell(fp);
68+
assert(size >= 0);
6669
data = (unsigned char *)malloc(size);
70+
assert(data != NULL);
6771
fseek(fp, 0, SEEK_SET);
68-
fread(data, 1, size, fp);
72+
read = fread(data, 1, size, fp);
73+
assert(read == static_cast<size_t>(size));
6974
fclose(fp);
7075

7176
handle = MemoryLoadLibrary(data);
@@ -94,8 +99,7 @@ void LoadFromMemory(void)
9499
MemoryFreeLibrary(handle);
95100

96101
exit:
97-
if (data)
98-
free(data);
102+
free(data);
99103
}
100104

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

example/DllLoader/DllLoaderLoader.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#define WIN32_LEAN_AND_MEAN
22

3+
#include <assert.h>
34
#include <windows.h>
45
#include <tchar.h>
56
#include <stdio.h>
@@ -13,7 +14,8 @@ int RunFromMemory(void)
1314
{
1415
FILE *fp;
1516
unsigned char *data=NULL;
16-
size_t size;
17+
long size;
18+
size_t read;
1719
HMEMORYMODULE handle;
1820
int result = -1;
1921

@@ -26,9 +28,12 @@ int RunFromMemory(void)
2628

2729
fseek(fp, 0, SEEK_END);
2830
size = ftell(fp);
31+
assert(size >= 0);
2932
data = (unsigned char *)malloc(size);
33+
assert(data != NULL);
3034
fseek(fp, 0, SEEK_SET);
31-
fread(data, 1, size, fp);
35+
read = fread(data, 1, size, fp);
36+
assert(read == static_cast<size_t>(size));
3237
fclose(fp);
3338

3439
handle = MemoryLoadLibrary(data);
@@ -45,8 +50,7 @@ int RunFromMemory(void)
4550
MemoryFreeLibrary(handle);
4651

4752
exit:
48-
if (data)
49-
free(data);
53+
free(data);
5054
return result;
5155
}
5256

tests/LoadDll.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#define WIN32_LEAN_AND_MEAN
22

3+
#include <assert.h>
34
#include <windows.h>
45
#include <tchar.h>
56
#include <stdio.h>
@@ -60,7 +61,7 @@ BOOL CheckResourceStrings(LPVOID data, DWORD size, const char *first, const wcha
6061
src = (const wchar_t *) (((const char *) data) + strlen(first) + 1);
6162
second_pos = swcsstr(src, second, (size - strlen(first) - 1) / sizeof(wchar_t));
6263
if (second_pos == NULL) {
63-
fprintf(stderr, "ERROR: data doesn't continue with %S\n", second);
64+
fwprintf(stderr, L"ERROR: data doesn't continue with %s\n", second);
6465
return FALSE;
6566
}
6667

@@ -71,8 +72,9 @@ BOOL LoadFromMemory(char *filename)
7172
{
7273
FILE *fp;
7374
unsigned char *data=NULL;
74-
size_t size;
75-
HMEMORYMODULE handle;
75+
long size;
76+
size_t read;
77+
HMEMORYMODULE handle = NULL;
7678
addNumberProc addNumber;
7779
addNumberProc addNumber2;
7880
HMEMORYRSRC resourceInfo;
@@ -91,9 +93,12 @@ BOOL LoadFromMemory(char *filename)
9193

9294
fseek(fp, 0, SEEK_END);
9395
size = ftell(fp);
96+
assert(size > 0);
9497
data = (unsigned char *)malloc(size);
98+
assert(data != NULL);
9599
fseek(fp, 0, SEEK_SET);
96-
fread(data, 1, size, fp);
100+
read = fread(data, 1, size, fp);
101+
assert(read == static_cast<size_t>(size));
97102
fclose(fp);
98103

99104
handle = MemoryLoadLibrary(data);
@@ -204,11 +209,9 @@ BOOL LoadFromMemory(char *filename)
204209
result = FALSE;
205210
}
206211

207-
MemoryFreeLibrary(handle);
208-
209212
exit:
210-
if (data)
211-
free(data);
213+
MemoryFreeLibrary(handle);
214+
free(data);
212215
return result;
213216
}
214217

0 commit comments

Comments
 (0)