forked from rurban/safeclib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_memmove_s.c
284 lines (234 loc) · 6.75 KB
/
test_memmove_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*------------------------------------------------------------------
* test_memmove_s
* File 'mem/memmove_s.c'
* Lines executed:83.33% of 18
*
*------------------------------------------------------------------
*/
#include "test_private.h"
#include "test_expmem.h"
#if defined(TEST_MSVCRT) && defined(HAVE_MEMMOVE_S)
#undef memmove_s
EXTERN errno_t memmove_s(void *dest, rsize_t dmax, const void *src,
rsize_t slen);
#endif
#ifdef HAVE_MEMMOVE_S
#define HAVE_NATIVE 1
#else
#define HAVE_NATIVE 0
#endif
#include "test_msvcrt.h"
#define LEN (1024)
static uint8_t mem1[LEN + 1];
static uint8_t mem2[LEN];
int test_memmove_s(void);
int test_memmove_s(void) {
errno_t rc;
uint32_t i;
rsize_t len;
int errs = 0;
/*--------------------------------------------------*/
for (i = 0; i < LEN; i++) {
mem1[i] = 33;
}
for (i = 0; i < LEN; i++) {
mem2[i] = 44;
}
print_msvcrt(use_msvcrt);
/* with clang-5 compile-time checks these errors */
#ifndef HAVE_CT_BOS_OVR
EXPECT_BOS("dest overflow or empty")
rc = memmove_s(NULL, LEN, mem2, LEN);
init_msvcrt(rc == ESNULLP, &use_msvcrt);
ERR_MSVC(ESNULLP, EINVAL);
/*--------------------------------------------------*/
EXPECT_BOS("dest overflow or empty") EXPECT_BOS("slen overflow >dmax")
rc = memmove_s(mem1, 0, mem2, LEN);
ERR_MSVC(ESZEROL, ERANGE); /* and untouched */
EXPMEM(mem1, 0, LEN, 33, 1);
/*--------------------------------------------------*/
EXPECT_BOS("dest overflow or empty")
rc = memmove_s(mem1, RSIZE_MAX_MEM + 1, mem2, LEN);
ERR_MSVC(ESLEMAX, 0); /* and implementation defined */
if (!use_msvcrt)
EXPMEM(mem1, 0, LEN, 33, 1);
else
EXPMEM(mem1, 0, LEN, 44, 1);
#endif
/*--------------------------------------------------*/
for (i = 0; i < LEN; i++) {
mem1[i] = 33;
}
/* check n=0 first */
rc = memmove_s(mem1, 10, mem2, 0);
ERR(EOK); /* and untouched */
EXPMEM(mem1, 0, LEN, 33, 1);
rc = memmove_s(NULL, 10, mem2, 0);
ERR(EOK); /* and untouched */
EXPMEM(mem1, 0, LEN, 33, 1);
rc = memmove_s(mem1, 0, mem2, 0);
ERR(EOK); /* and untouched */
EXPMEM(mem1, 0, LEN, 33, 1);
rc = memmove_s(mem1, 10, NULL, 0);
ERR(EOK); /* and untouched */
EXPMEM(mem1, 0, LEN, 33, 1);
/*--------------------------------------------------*/
#ifndef HAVE_CT_BOS_OVR
EXPECT_BOS("src overflow or empty")
rc = memmove_s(mem1, LEN, NULL, LEN);
ERR_MSVC(ESNULLP, EINVAL); /* and cleared */
if (!use_msvcrt) {
EXPMEM(mem1, 0, LEN, 0, 1); /* broken with msvcrt! */
}
for (i = 0; i < LEN; i++) {
mem1[i] = 33;
}
EXPECT_BOS("src overflow or empty") EXPECT_BOS("slen overflow >dmax")
rc = memmove_s(mem1, LEN, mem2, RSIZE_MAX_MEM + 1);
ERR_MSVC(ESLEMAX, ERANGE); /* and cleared */
if (!use_msvcrt) {
EXPMEM(mem1, 0, LEN, 0, 1);
}
#endif
/*--------------------------------------------------*/
for (i = 0; i < LEN + 1; i++) {
mem1[i] = 33;
}
for (i = 0; i < LEN; i++) {
mem2[i] = 44;
}
len = LEN;
rc = memmove_s(mem1, len, mem2, len);
ERR(EOK);
EXPMEM(mem1, 0, len, 44, 1);
EXPMEM(mem1, len, LEN + 1, 33, 1);
for (i = 0; i < LEN + 1; i++) {
mem1[i] = 33;
}
for (i = 0; i < LEN; i++) {
mem2[i] = 44;
}
len = 1;
rc = memmove_s(mem1, len, mem2, len);
ERR(EOK);
EXPMEM(mem1, 0, len, 44, 1);
EXPMEM(mem1, len, LEN + 1, 33, 1);
/*--------------------------------------------------*/
for (i = 0; i < LEN + 1; i++) {
mem1[i] = 33;
}
for (i = 0; i < LEN; i++) {
mem2[i] = 44;
}
/* length error */
len = LEN / 2;
rc = memmove_s(mem1, len, mem2, LEN);
ERR_MSVC(ESNOSPC, ERANGE); /* and cleared */
if (!use_msvcrt) {
EXPMEM(mem1, 0, len, 0, 1); /* broken with msvcrt */
}
if (mem1[len] != 33) {
debug_printf("%d - %d m1=%d m2=%d \n", __LINE__, (int)len, mem1[len],
mem2[len]);
errs++;
}
/*--------------------------------------------------*/
for (i = 0; i < LEN; i++) {
mem1[i] = 33;
}
for (i = 0; i < LEN; i++) {
mem2[i] = 44;
}
/* invalid length, untouched dest */
len = LEN;
rc = memmove_s(mem1, len, mem2, 0);
ERR(EOK);
/* verify mem1[0] was not zeroed */
if (mem1[0] != 33) {
debug_printf("%d - %d m1=%d m2=%d \n", __LINE__, 0, (int)mem1[0],
(int)mem2[0]);
errs++;
}
/*--------------------------------------------------*/
for (i = 0; i < LEN; i++) {
mem1[i] = 33;
}
for (i = 0; i < LEN; i++) {
mem2[i] = 44;
}
/* invalid length - zero dest */
#ifndef HAVE_CT_BOS_OVR
len = LEN;
EXPECT_BOS("src overflow or empty")
rc = memmove_s(mem1, len, mem2, RSIZE_MAX_MEM + 1);
ERR_MSVC(ESLEMAX, ERANGE);
if (!use_msvcrt) {
/* verify mem1 was zeroed */
for (i = 0; i < len; i++) {
if (mem1[i] != 0) {
debug_printf("%d - %d m1=%d \n", __LINE__, i, mem1[i]);
errs++;
}
}
}
#endif
/*--------------------------------------------------*/
for (i = 0; i < LEN; i++) {
mem1[i] = 33;
}
for (i = 0; i < LEN; i++) {
mem2[i] = 44;
}
/* same ptr - no move */
rc = memmove_s(mem1, LEN, mem1, LEN);
ERR(EOK)
/*--------------------------------------------------*/
for (i = 0; i < LEN; i++) {
mem1[i] = 25;
}
for (i = 10; i < LEN - 10; i++) {
mem1[i] = 35;
}
/* overlap move */
len = 20;
rc = memmove_s(&mem1[0], len, &mem1[10], len);
ERR(EOK)
else {
for (i = 0; i < len; i++) {
if (mem1[i] != 35) {
debug_printf("%d - %d m1=%d \n", __LINE__, i, mem1[i]);
errs++;
}
}
}
/*--------------------------------------------------*/
for (i = 0; i < LEN; i++) {
mem1[i] = 25;
}
for (i = 10; i < LEN - 10; i++) {
mem1[i] = 35;
}
/* overlap move */
len = 20;
rc = memmove_s(&mem1[10], len, &mem1[0], len);
ERR(EOK)
for (i = 0; i < LEN; i++) {
mem2[i] = 25;
}
for (i = 10; i < LEN - 10; i++) {
mem2[i] = 35;
}
for (i = 0; i < 10; i++) {
if (mem1[i] != 25) {
debug_printf("%d - %d m1=%d \n", __LINE__, i, mem1[i]);
errs++;
}
}
/*--------------------------------------------------*/
return (errs);
}
#ifndef __KERNEL__
/* simple hack to get this to work for both userspace and Linux kernel,
until a better solution can be created. */
int main(void) { return (test_memmove_s()); }
#endif