forked from rurban/safeclib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_memmove32_s.c
186 lines (150 loc) · 4.39 KB
/
test_memmove32_s.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*------------------------------------------------------------------
* test_memmove32_s
* File 'extmem/memmove32_s.c'
* Lines executed:83.33% of 18
*
*------------------------------------------------------------------
*/
#include "test_private.h"
#include "test_expmem.h"
#define LEN (1024)
#define MAX (LEN * 4)
int main(void) {
errno_t rc;
uint32_t len;
uint32_t i;
uint32_t mem1[LEN];
uint32_t mem2[LEN];
rsize_t count = LEN;
int errs = 0;
/*--------------------------------------------------*/
for (i = 0; i < LEN; i++) {
mem1[i] = 33;
}
rc = memmove32_s(NULL, MAX, mem2, count);
ERR(ESNULLP);
/*--------------------------------------------------*/
rc = memmove32_s(mem1, 0, mem2, count);
ERR(ESZEROL); /* and untouched */
EXPMEM(mem1, 0, LEN, 33, 4);
/*--------------------------------------------------*/
rc = memmove32_s(mem1, RSIZE_MAX_MEM + 1, mem2, count);
ERR(ESLEMAX); /* and untouched */
EXPMEM(mem1, 0, LEN, 33, 4);
/*--------------------------------------------------*/
for (i = 0; i < LEN; i++) {
mem1[i] = 33;
}
rc = memmove32_s(mem1, MAX, NULL, count);
ERR(ESNULLP); /* and cleared */
EXPMEM(mem1, 0, LEN, 0, 4);
/*--------------------------------------------------*/
for (i = 0; i < 10; i++) {
mem1[i] = 33;
}
rc = memmove32_s(mem1, 10, mem2, 0);
ERR(EOK); /* and untouched */
EXPMEM(mem1, 0, 10, 33, 4);
/*--------------------------------------------------*/
#ifndef HAVE_CT_BOS_OVR
EXPECT_BOS("src overflow or empty") EXPECT_BOS("slen overflow >dmax / 4")
rc = memmove32_s(mem1, MAX, mem2, RSIZE_MAX_MEM32 + 1);
ERR(ESLEMAX); /* and cleared */
EXPMEM(mem1, 0, LEN, 0, 4);
#endif
/*--------------------------------------------------*/
for (i = 0; i < LEN; i++) {
mem1[i] = 33;
}
for (i = 0; i < LEN; i++) {
mem2[i] = 44;
}
len = 1;
rc = memmove32_s(mem1, MAX, mem2, len);
ERR(EOK); /* and copied */
EXPMEM(mem1, 0, len, 44, 4);
EXPMEM(mem1, len, LEN - len, 33, 4);
for (i = 0; i < LEN; i++) {
mem1[i] = 33;
}
for (i = 0; i < LEN; i++) {
mem2[i] = 44;
}
len = 2;
rc = memmove32_s(mem1, MAX, mem2, len);
ERR(EOK); /* and copied */
EXPMEM(mem1, 0, len, 44, 4);
EXPMEM(mem1, len, LEN - len, 33, 4);
for (i = 0; i < LEN; i++) {
mem1[i] = 33;
}
for (i = 0; i < LEN; i++) {
mem2[i] = 44;
}
/* a valid move */
len = LEN;
rc = memmove32_s(mem1, MAX, mem2, len);
ERR(EOK); /* and copied */
EXPMEM(mem1, 0, len, 44, 4);
/*--------------------------------------------------*/
for (i = 0; i < LEN; i++) {
mem1[i] = 33;
}
for (i = 0; i < LEN; i++) {
mem2[i] = 44;
}
/* count*4 greater than dmax */
rc = memmove32_s(mem1, MAX, mem2, count + 1);
ERR(ESNOSPC); /* and cleared */
EXPMEM(mem1, 0, LEN, 0, 4);
/*--------------------------------------------------*/
for (i = 0; i < LEN; i++) {
mem1[i] = 33;
}
for (i = 0; i < LEN; i++) {
mem2[i] = 44;
}
/* empty count */
rc = memmove32_s(mem1, MAX, mem2, 0);
ERR(EOK); /* and untouched */
EXPMEM(mem1, 0, LEN, 33, 4);
/*--------------------------------------------------*/
for (i = 0; i < LEN; i++) {
mem1[i] = 33;
}
for (i = 0; i < LEN; i++) {
mem2[i] = 44;
}
/* same ptr - no move */
rc = memmove32_s(mem1, MAX, mem1, count);
ERR(EOK); /* and untouched */
EXPMEM(mem1, 0, LEN, 33, 4);
/*--------------------------------------------------*/
for (i = 0; i < LEN; i++) {
mem1[i] = 25;
}
for (i = 10; i < LEN - 10; i++) {
mem1[i] = 35;
}
/* overlap move + */
len = 20;
rc = memmove32_s(&mem1[0], LEN, &mem1[10], len);
ERR(EOK); /* and copied */
EXPMEM(mem1, 0, len, 35, 4);
EXPMEM(mem1, len, LEN, 35, 4);
/*--------------------------------------------------*/
for (i = 0; i < LEN; i++) {
mem1[i] = 25;
}
for (i = 10; i < LEN - 10; i++) {
mem1[i] = 35;
}
/* overlap move - */
len = 20;
rc = memmove32_s(&mem1[10], (LEN - 10) * 2, &mem1[0], len);
ERR(EOK);
EXPMEM(mem1, 0, 10, 25, 4);
EXPMEM(mem1, len, LEN, 35, 4);
/*--------------------------------------------------*/
return (errs);
}