-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgid_malloc_example.c
50 lines (38 loc) · 1.12 KB
/
gid_malloc_example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "../gidunit.h"
BEGIN_TEST_SUITE(MyMallocTests)
Test(CanWriteToAllocatedMemory,
EnumParam(size, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 16, 32, 64, 128))
{
uint8_t* mem = gid_malloc(size);
//Write to the allocated memory
for(int64_t i = 0; i < size; i++)
mem[i]++;
//Free the memory. This will scan for corruption out of bounds, but we stayed in the requested size+bounds.
gid_free(mem);
}
Test(WriteOutOfLeftBoundsFails,
EnumParam(size, 0, 1, 10, 128)
EnumParam(offset, 1, 2, 3, 5, 10, 25))
{
uint8_t* mem = gid_malloc(size);
//Corrupt one byte out of bounds
mem[-offset]++;
//Free the memory, causing assert to fail when corruption is detected
gid_free(mem);
}
Test(WriteOutOfRightBoundsFails,
EnumParam(size, 0, 1, 2, 3, 10, 128)
EnumParam(offset, 1, 2, 3, 5, 10, 25))
{
uint8_t* mem = gid_malloc(size);
//Corrupt one byte out of bounds
mem[size+offset]++;
//Free the memory, causing assert to fail when corruption is detected
gid_free(mem);
}
END_TEST_SUITE()
int main()
{
ADD_TEST_SUITE(MyMallocTests);
return gidunit();
}