forked from rurban/safeclib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mbstowcs_s.c
275 lines (229 loc) · 6.81 KB
/
test_mbstowcs_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
/*------------------------------------------------------------------
* test_mbstowcs_s
* File 'wchar/mbstowcs_s.c'
* Lines executed:91.43% of 35
* locale specific, sets it to C and UTF-8
*
*------------------------------------------------------------------
*/
#include "test_private.h"
#include "safe_str_lib.h"
#ifdef HAVE_MBSTOWCS_S
#define HAVE_NATIVE 1
#else
#define HAVE_NATIVE 0
#endif
#include "test_msvcrt.h"
#define LEN (128)
static wchar_t dest[LEN];
static char src[LEN];
#ifdef HAVE_WCHAR_H
#include <stdlib.h>
#include <locale.h>
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif
int test_mbstowcs_s(void);
int test_mbstowcs_s(void) {
errno_t rc;
size_t ind;
const char *cs;
const char *lang;
const char *lc_cat;
int errs = 0;
/*--------------------------------------------------*/
cs = "a";
strcpy(src, "abc");
print_msvcrt(use_msvcrt);
#ifndef HAVE_CT_BOS_OVR
EXPECT_BOS("empty retvalp") EXPECT_BOS("empty src or len")
rc = mbstowcs_s(NULL, NULL, LEN, cs, 0);
init_msvcrt(rc == ESNULLP, &use_msvcrt);
ERR_MSVC(ESNULLP, EINVAL);
EXPECT_BOS("empty src") EXPECT_BOS("empty src or len")
rc = mbstowcs_s(&ind, dest, LEN, NULL, 0);
ERR_MSVC(ESNULLP, EINVAL);
src[0] = '\0';
EXPECT_BOS("empty src or len")
rc = mbstowcs_s(&ind, NULL, 0, cs, 0);
#ifdef BSD_LIKE
if (rc != 2) { /* BSD's return 2 */
printf("%s %u wrong mbstowcs(NULL,\"\\0\"): %d\n", __FUNCTION__,
__LINE__, (int)rc);
}
#else
ERR(0);
#endif
EXPECT_BOS("empty dmax")
rc = mbstowcs_s(&ind, dest, 0, cs, 3);
ERR_MSVC(ESZEROL, EINVAL);
EXPECT_BOS("dest overflow")
rc = mbstowcs_s(&ind, dest, RSIZE_MAX_STR + 1, cs, 3);
ERR_MSVC(ESLEMAX, 0);
#ifdef HAVE___BUILTIN_OBJECT_SIZE
EXPECT_BOS("dest overflow")
rc = mbstowcs_s(&ind, dest, LEN + 1, cs, 3);
ERR(EOVERFLOW);
#endif
dest[0] = L'a';
GCC_PUSH_WARN_RESTRICT
EXPECT_BOS("dest overlap")
rc = mbstowcs_s(&ind, dest, LEN, (const char *)dest, 1);
GCC_POP_WARN_RESTRICT
ERR_MSVC(ESOVRLP, ERANGE);
{
void *p1;
cs = "abcdef";
GCC_PUSH_WARN_RESTRICT
EXPECT_BOS("dest overlap")
rc = mbstowcs_s(&ind, (wchar_t *)&p1, 1, (const char *)&p1, 1);
GCC_POP_WARN_RESTRICT
ERR_MSVC(ESOVRLP, ERANGE);
}
#endif
/*--------------------------------------------------*/
rc = mbstowcs_s(&ind, dest, LEN, (cs = "abcdef", cs), 3);
ERR(EOK);
INDCMP(!= 3);
WCHECK_SLACK(&dest[3], LEN - 3);
rc = mbstowcs_s(&ind, dest, LEN, (cs = "abcdef", cs), 8);
ERR(EOK);
INDCMP(!= 6);
WCHECK_SLACK(&dest[6], LEN - 6);
rc = mbstowcs_s(&ind, NULL, LEN, (cs = "abcdef", cs), 2);
ERR(EOK);
INDCMP(!= 6);
SETLOCALE_C;
SETLANG("C");
CHKLOCALE_C;
rc = mbstowcs_s(&ind, dest, LEN, (cs = "\x7f", cs), 1);
ERR(EOK);
INDCMP(!= 1);
SETLOCALE_UTF8;
SETLANG("default");
REQLOCALE("UTF-8")
rc = mbstowcs_s(&ind, dest, LEN,
(cs = "\xe2\x86\x92"
"abc",
cs),
32);
{
/* TODO: EILSEQ with cygwin64 */
#if defined(HAVE_CYGWIN64)
int saveerrs = errs;
const char *todo = "Todo";
#else
const char *todo = "Error";
#endif
if (rc != EOK) {
debug_printf("%s %u %s rc=%d ind=%d\n", __FUNCTION__, __LINE__,
todo, (int)rc, (int)ind);
errs++;
}
if (ind != 4) {
printf("%s %u %s ind=%d rc=%d \n", __FUNCTION__, __LINE__, todo,
(int)ind, rc);
errs++;
}
/* WCHECK_SLACK(&dest[4], LEN-4); */
if (dest[0] != 0x2192) {
printf("%s %u %s ind=%d rc=%d %ld 0x%lx\n", __FUNCTION__,
__LINE__, todo, (int)ind, rc, (long)dest[0], (long)dest[0]);
errs++;
}
if (dest[1] != 'a') {
printf("%s %u %s ind=%d rc=%d 0x%lx\n", __FUNCTION__, __LINE__,
todo, (int)ind, rc, (long)dest[1]);
errs++;
}
#if defined(HAVE_CYGWIN64)
if (errs)
printf("TODO cygwin64 EILSEQ of \"\\xe2\\x86\\x92\"\n");
errs = saveerrs;
#endif
}
/* illegal sequences (locale dependent) */
#ifdef HAVE_CYGWIN64
return (errs);
#endif
/* illegal initial */
rc = mbstowcs_s(&ind, dest, LEN, (cs = "\xc0", cs), 1);
/* TODO and this is legal on cygwin64 */
ERR(EILSEQ);
INDCMP(!= -1);
WCHECK_SLACK(dest, LEN);
rc = mbstowcs_s(&ind, dest, LEN, (cs = "\xc2", cs), 1);
ERR(EILSEQ);
INDCMP(!= -1);
/* aliasing nul */
rc = mbstowcs_s(&ind, dest, LEN, (cs = "\xc0\x80", cs), 2);
ERR(EILSEQ);
INDCMP(!= -1);
/* aliasing slashes */
rc = mbstowcs_s(&ind, dest, LEN, (cs = "\xc0\xaf", cs), 2);
ERR(EILSEQ);
INDCMP(!= -1);
WCHECK_SLACK(dest, LEN);
rc = mbstowcs_s(&ind, dest, LEN, (cs = "\xe0\x80\xaf", cs), 3);
ERR(EILSEQ);
INDCMP(!= -1);
WCHECK_SLACK(dest, LEN);
rc = mbstowcs_s(&ind, dest, LEN, (cs = "\xf0\x80\x80\xaf", cs), 4);
ERR(EILSEQ);
INDCMP(!= -1);
WCHECK_SLACK(dest, LEN);
rc = mbstowcs_s(&ind, dest, LEN, (cs = "\xf8\x80\x80\x80\xaf", cs), 5);
ERR(EILSEQ);
INDCMP(!= -1);
WCHECK_SLACK(dest, LEN);
rc = mbstowcs_s(&ind, dest, LEN, (cs = "\xfc\x80\x80\x80\x80\xaf", cs), 6);
ERR(EILSEQ);
INDCMP(!= -1);
WCHECK_SLACK(dest, LEN);
/* aliasing U+0080 */
rc = mbstowcs_s(&ind, dest, LEN, (cs = "\xe0\x82\x80", cs), 3);
ERR(EILSEQ);
INDCMP(!= -1);
WCHECK_SLACK(dest, LEN);
/* aliasing U+07FF */
rc = mbstowcs_s(&ind, dest, LEN, (cs = "\xe0\x9f\xbf", cs), 3);
ERR(EILSEQ);
INDCMP(!= -1);
WCHECK_SLACK(dest, LEN);
/* aliasing U+0800 */
rc = mbstowcs_s(&ind, dest, LEN, (cs = "\xf0\x80\xa0\x80", cs), 4);
ERR(EILSEQ);
INDCMP(!= -1);
WCHECK_SLACK(dest, LEN);
/* aliasing U+FFFD */
rc = mbstowcs_s(&ind, dest, LEN, (cs = "\xf0\x8f\xbf\xbd", cs), 4);
ERR(EILSEQ);
INDCMP(!= -1);
WCHECK_SLACK(dest, LEN);
/* check enough space for src and conversion errors */
rc = mbstowcs_s(&ind, dest, 6, (cs = "abcdef", cs), 6);
ERR_MSVC(ESNOSPC, ERANGE);
WCHECK_SLACK(dest, 6);
rc = mbstowcs_s(&ind, dest, 3, (cs = "\xf0\x8f\xbf\xbd", cs), 4);
ERR(EILSEQ);
INDCMP(!= -1);
WCHECK_SLACK(dest, LEN);
rc = mbstowcs_s(&ind, dest, LEN, (cs = "\x80\xbf\x80", cs), 3);
ERR(EILSEQ);
INDCMP(!= -1);
WCHECK_SLACK(dest, LEN);
rc = mbstowcs_s(&ind, dest, LEN, (cs = "\xfc\x80\x80\x80\x80\x80", cs), 6);
ERR(EILSEQ);
INDCMP(!= -1);
WCHECK_SLACK(dest, LEN);
/*--------------------------------------------------*/
return (errs);
}
#endif
int main(void) {
#ifdef HAVE_WCHAR_H
return (test_mbstowcs_s());
#else
return 0;
#endif
}