-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgwtest.c
More file actions
142 lines (126 loc) · 2.79 KB
/
gwtest.c
File metadata and controls
142 lines (126 loc) · 2.79 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* Sample program illustrating some of the features of the library */
#include <sys/stat.h>
#include <malloc.h>
#include <string.h>
#include <stdio.h>
#ifdef __MSDOS__
#include <io.h>
#endif
#include <fcntl.h>
#include <assert.h>
#include "heap.h"
#include "gwdebug.h"
#ifdef LOCAL_HEAP
typedef void far *heap_ptr;
#define MALLOC farmalloc
#define FREE farfree
#else
typedef void *heap_ptr;
#define MALLOC malloc
#define FREE free
#endif
void leakTest(void)
{
(void)MALLOC(10);
}
void badFreeTest(void)
{
FREE((void *)26304);
}
void doubleFreeTest(void)
{
heap_ptr p = MALLOC(10);
FREE(p);
FREE(p);
}
void overrunTest1(void)
{
heap_ptr p = MALLOC(10);
#ifdef LOCAL_HEAP
char far *d = (char far *)p,
*s = "Hello World";
while ((*d++ = *s++) != 0);
#else
char *d = (char *)p,
*s = "Hello World";
while ((*d++ = *s++) != 0);
#endif
FREE(p);
}
void overrunTest2(void)
{
#if defined(LOCAL_HEAP) && (defined(__SMALL__) || defined(__COMPACT__) || defined(__TINY__))
printf("This test is skipped with local heap\n");
#else
heap_ptr *p = MALLOC(10);
strcpy((char *)p, "Hello world");
FREE(p);
#endif
}
void overrunTest3(void)
{
char dest[8];
strcpy(dest,"Hello world");
strncpy(dest,"Hello world",8);
strncpy(dest,"Hello world",9);
}
void fileTest(void)
{
FILE *fp;
int h = open("junk", O_CREAT, S_IWRITE), h2;
close(h);
h2 = dup(h);
close(h);
close(h2);
fp = fopen("leak", "w");
(void)fp;
}
void initTest(void)
{
/* the tests here marked ** will only succeed is junk[] has no
zero bytes in it after being created on the stack; luck of the
draw, unfortunately */
heap_ptr *p = MALLOC(10);
char junk[10];
/**/(void)strlen(junk);
(void)strlen((char *)p);
(void)strcpy(junk, (char *)p);
(void)memcpy(junk, (char *)p, 10);
/**/(void)strcpy((char *)p, junk);
(void)memcpy((char *)p, junk, 10); /* this is undetectable */
FREE(p);
}
#ifdef LOCAL_HEAP
static char heap[24000];
#endif
void main(void)
{
int ch = 0;
#ifdef LOCAL_HEAP
setheap(heap, 24000);
#endif
while (ch != 'q')
{
printf("Enter:\n\tq - to quit\n\t1 - for leak test\n");
printf("\t2 - for overrun test 1\n\t3 - for overrun test 2\n");
printf("\t4 - for overrun test 3\n");
printf("\t5 - for bad free\n\t6 - for double free\n");
printf("\t7 - for file test\n\t8 - for uninit test\n");
ch=getchar();
switch(ch)
{
case '1': leakTest(); break;
case '2': overrunTest1(); break;
case '3': overrunTest2(); break;
case '4': overrunTest3(); break;
case '5': badFreeTest(); break;
case '6': doubleFreeTest(); break;
case '7': fileTest(); break;
case '8': initTest(); break;
}
while (getchar() != '\n');
}
#ifdef LOCAL_HEAP
heapstatus(1);
#endif
}