File: test_mbsrtowcs_s.c

package info (click to toggle)
safeclib 3.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,832 kB
  • sloc: ansic: 52,639; makefile: 1,271; perl: 528; sh: 518
file content (303 lines) | stat: -rw-r--r-- 7,601 bytes parent folder | download | duplicates (2)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*------------------------------------------------------------------
 * test_mbsrtowcs_s
 * File 'wchar/mbsrtowcs_s.c'
 * Lines executed:91.89% of 37
 * locale specific, sets it to C and UTF-8
 *
 *------------------------------------------------------------------
 */

#include "test_private.h"
#include "safe_str_lib.h"

#ifdef HAVE_MBSRTOWCS_S
#define HAVE_NATIVE 1
#else
#define HAVE_NATIVE 0
#endif
#include "test_msvcrt.h"

#define MAX (128)
#define LEN (128)

static wchar_t dest[LEN];
#ifndef HAVE_CT_BOS_OVR
static char src[LEN];
#endif

#ifdef HAVE_WCHAR_H
#include <stdlib.h>
#include <locale.h>
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif

#define CLRPS memset(&ps, '\0', sizeof(mbstate_t))
int test_mbsrtowcs_s(void);

int test_mbsrtowcs_s(void) {
    errno_t rc;
    size_t ind;
    /*uint32_t i;*/
    const char *cs;
    mbstate_t ps;
    const char *lang;
    const char *lc_cat;
    int errs = 0;

    /*--------------------------------------------------*/

    cs = "a";
#ifndef HAVE_CT_BOS_OVR
    print_msvcrt(use_msvcrt);
    EXPECT_BOS("empty retvalp") EXPECT_BOS("empty *srcp or len")
    rc = mbsrtowcs_s(NULL, NULL, LEN, &cs, 0, &ps);
    init_msvcrt(rc == ESNULLP, &use_msvcrt);
    ERR_MSVC(ESNULLP, EINVAL);
    CLRPS;

    EXPECT_BOS("empty ps") EXPECT_BOS("empty *srcp or len")
    rc = mbsrtowcs_s(&ind, NULL, LEN, &cs, 0, NULL);
    ERR_MSVC(ESNULLP, EINVAL);

    EXPECT_BOS("empty srcp") EXPECT_BOS("empty *srcp or len")
    rc = mbsrtowcs_s(&ind, dest, LEN, NULL, 0, &ps);
    ERR_MSVC(ESNULLP, EINVAL);
    CLRPS;

    {
        const char **srcp = (const char **)(void *)&src;
        *src = 0;
        /*EXPECT_BOS_TODO("empty *srcp or len")*/
        rc = mbsrtowcs_s(&ind, dest, LEN, srcp, 1, &ps);
        ERR_MSVC(ESNULLP, EINVAL);
        CLRPS;
    }

    cs = "a";
    EXPECT_BOS("empty dmax")
    rc = mbsrtowcs_s(&ind, dest, 0, &cs, 3, &ps);
    ERR_MSVC(ESZEROL, EINVAL);
    CLRPS;

    EXPECT_BOS("dest overflow")
    rc = mbsrtowcs_s(&ind, dest, RSIZE_MAX_STR + 1, &cs, 3, &ps);
    ERR_MSVC(ESLEMAX, 0);
    CLRPS;

#ifdef HAVE___BUILTIN_OBJECT_SIZE
    EXPECT_BOS("dest overflow")
    rc = mbsrtowcs_s(&ind, dest, LEN + 1, &cs, 3, &ps);
    ERR(EOVERFLOW);
    CLRPS;
#endif

    {
        void *p1;
        cs = "abcdef";
        p1 = &cs;
        EXPECT_BOS_TODO("dest overlap")
        rc = mbsrtowcs_s(&ind, (wchar_t *)p1, 1, &cs, 1, &ps);
        ERR_MSVC(ESOVRLP, ERANGE);
        CLRPS;
    }
#endif

    /*--------------------------------------------------*/

    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "abcdef", &cs), 3, &ps);
    ERR(EOK);
    INDCMP(!= 3);
    WCHECK_SLACK(&dest[3], LEN - 3);
    CLRPS;

    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "abcdef", &cs), 8, &ps);
    ERR(EOK);
    INDCMP(!= 6);
    WCHECK_SLACK(&dest[6], LEN - 6);
    CLRPS;

    rc = mbsrtowcs_s(&ind, NULL, LEN, (cs = "abcdef", &cs), 2, &ps);
    ERR(EOK);
#ifndef HAVE_MINGW32 /* ind=2 */
    INDCMP(!= 6);
#endif
    CLRPS;

    SETLOCALE_C;
    SETLANG("C");
    CHKLOCALE_C;

    /* no-breaking space illegal in ASCII, but legal in C */
    rc = mbsrtowcs_s(&ind, dest, LEN,
                     (cs = "\xa0"
                           "abc",
                      &cs),
                     32, &ps);
    if (rc == 0) { /* legal */
        ERR(EOK);
        INDCMP(!= 4);
        WCHECK_SLACK(&dest[4], LEN - 4);
        /* musl on ASCII converts \xa0 to \xdfa0 */
        if ((int)dest[0] != 0xa0 && (int)dest[0] != 0xdfa0) {
            printf("%s %u  Error  ind=%d rc=%d 0x%lx\n", __FUNCTION__, __LINE__,
                   (int)ind, rc, (long)dest[0]);
            errs++;
        }
        if (dest[1] != L'a') {
            printf("%s %u  Error  ind=%d rc=%d 0x%lx\n", __FUNCTION__, __LINE__,
                   (int)ind, rc, (long)dest[1]);
            errs++;
        }
        if (cs) { /* needs to be at the end */
            printf("%s %u  Error  ind=%d rc=%d %s\n", __FUNCTION__, __LINE__,
                   (int)ind, rc, cs);
            errs++;
        }
    } else {
        ERR(EILSEQ); /* or illegal */
        INDCMP(!= -1);
        WCHECK_SLACK(dest, LEN);
        if (dest[0] != L'\0') {
            printf("%s %u  Error  ind=%d rc=%d 0x%lx\n", __FUNCTION__, __LINE__,
                   (int)ind, rc, (long)dest[0]);
            errs++;
        }
    }
    CLRPS;

    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "\x78", &cs), 1, &ps);
    ERR(EOK);
    INDCMP(!= 1);
    WCHECK_SLACK(&dest[1], LEN - 1);
    CLRPS;

    SETLOCALE_UTF8;
    SETLANG("default");
    REQLOCALE("UTF-8")

    /* illegal sequences (locale dependent) */

    /* illegal initial */
    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "\xc0", &cs), 1, &ps);
    ERR(EILSEQ);
    INDCMP(!= -1);
    WCHECK_SLACK(dest, LEN);
    CLRPS;

    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "\xc2", &cs), 1, &ps);
    ERR(EILSEQ);
    INDCMP(!= -1);
    WCHECK_SLACK(dest, LEN);
    CLRPS;

    /* aliasing nul */
    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "\xc0\x80", &cs), 2, &ps);
    ERR(EILSEQ);
    INDCMP(!= -1);
    WCHECK_SLACK(dest, LEN);
    CLRPS;

    /* aliasing slashes */
    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "\xc0\xaf", &cs), 2, &ps);
    ERR(EILSEQ);
    INDCMP(!= -1);
    WCHECK_SLACK(dest, LEN);
    CLRPS;

    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "\xe0\x80\xaf", &cs), 3, &ps);
    ERR(EILSEQ);
    INDCMP(!= -1);
    WCHECK_SLACK(dest, LEN);
    CLRPS;

    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "\xf0\x80\x80\xaf", &cs), 4, &ps);
    ERR(EILSEQ);
    INDCMP(!= -1);
    WCHECK_SLACK(dest, LEN);
    CLRPS;

    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "\xf8\x80\x80\x80\xaf", &cs), 5,
                     &ps);
    ERR(EILSEQ);
    INDCMP(!= -1);
    WCHECK_SLACK(dest, LEN);
    CLRPS;

    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "\xfc\x80\x80\x80\x80\xaf", &cs), 6,
                     &ps);
    ERR(EILSEQ);
    INDCMP(!= -1);
    WCHECK_SLACK(dest, LEN);
    CLRPS;

    /* aliasing U+0080 */
    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "\xe0\x82\x80", &cs), 3, &ps);
    ERR(EILSEQ);
    INDCMP(!= -1);
    WCHECK_SLACK(dest, LEN);
    CLRPS;

    /* aliasing U+07FF */
    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "\xe0\x9f\xbf", &cs), 3, &ps);
    ERR(EILSEQ);
    INDCMP(!= -1);
    WCHECK_SLACK(dest, LEN);
    CLRPS;

    /* aliasing U+0800 */
    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "\xf0\x80\xa0\x80", &cs), 4, &ps);
    ERR(EILSEQ);
    INDCMP(!= -1);
    WCHECK_SLACK(dest, LEN);
    CLRPS;

    /* aliasing U+FFFD */
    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "\xf0\x8f\xbf\xbd", &cs), 4, &ps);
    ERR(EILSEQ);
    INDCMP(!= -1);
    WCHECK_SLACK(dest, LEN);
    CLRPS;

    /* check enough space for src and conversion errors */

    rc = mbsrtowcs_s(&ind, dest, 6, (cs = "abcdef", &cs), 6, &ps);
    ERR_MSVC(ESNOSPC, ERANGE);
    WCHECK_SLACK(dest, 6);
    CLRPS;

#ifndef HAVE_CT_BOS_OVR
    EXPECT_BOS("len overflow >dmax")
    rc = mbsrtowcs_s(&ind, dest, 3, (cs = "\xf0\x8f\xbf\xbd", &cs), 4, &ps);
    ERR(EILSEQ);
    INDCMP(!= -1);
    WCHECK_SLACK(dest, LEN);
    CLRPS;
#endif

    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "\x80\xbf\x80", &cs), 3, &ps);
    ERR(EILSEQ);
    INDCMP(!= -1);
    WCHECK_SLACK(dest, LEN);
    CLRPS;

    rc = mbsrtowcs_s(&ind, dest, LEN, (cs = "\xfc\x80\x80\x80\x80\x80", &cs), 6,
                     &ps);
    ERR(EILSEQ);
    INDCMP(!= -1);
    WCHECK_SLACK(dest, LEN);

    /*--------------------------------------------------*/

    return (errs);
}

#endif

int main(void) {
#ifdef HAVE_WCHAR_H
    return (test_mbsrtowcs_s());
#else
    return 0;
#endif
}