blob: ca7c5f9279d80098c47e80af3e6feff1d808ddd3 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001
2#include <unistd.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdio.h>
6
7#define N_PTRS 1000
8#define N_ALLOCS 10000
9#define MAX_SIZE 0x10000
10
11#define random_size() (random()%MAX_SIZE)
12#define random_ptr() (random()%N_PTRS)
13
14int test1(void);
15int test2(void);
16
17int main(int argc, char *argv[])
18{
19 return test1() + test2();
20}
21
22int test1(void)
23{
24 void **ptrs;
25 int i,j;
26 int size;
27 int ret = 0;
28
29 srandom(0x19730929);
30
31 ptrs = malloc(N_PTRS*sizeof(void *));
32
33 for(i=0; i<N_PTRS; i++){
34 if ((ptrs[i] = malloc(random_size())) == NULL) {
35 printf("malloc random failed! %i\n", i);
36 ++ret;
37 }
38 }
39 for(i=0; i<N_ALLOCS; i++){
40 j = random_ptr();
41 free(ptrs[j]);
42
43 size = random_size();
44 ptrs[j] = malloc(size);
45 if (!ptrs[j]) {
46 printf("malloc failed! %d\n", i);
47 ++ret;
48 }
49 memset(ptrs[j],0,size);
50 }
51 for(i=0; i<N_PTRS; i++){
52 free(ptrs[i]);
53 }
54
55 return ret;
56}
57
58int test2(void)
59{
60 void *ptr = NULL;
61 int ret = 0;
62
63 ptr = realloc(ptr,100);
64 if (!ptr) {
65 printf("couldn't realloc() a NULL pointer\n");
66 ++ret;
67 } else {
68 free(ptr);
69 }
70
71 ptr = malloc(100);
72 ptr = realloc(ptr, 0);
73 if (ptr) {
74 printf("realloc(,0) failed\n");
75 ++ret;
76 free(ptr);
77 }
78
79 return ret;
80}
81