File: test_wcstombs_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 (190 lines) | stat: -rw-r--r-- 4,389 bytes parent folder | download
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
/*------------------------------------------------------------------
 * test_wcstombs_s
 * File 'wchar/wcstombs_s.c'
 * Lines executed:93.75% of 32
 *
 *------------------------------------------------------------------
 */

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

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

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

static char dest[LEN];
static wchar_t src[LEN];
int test_wcstombs_s(void);

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

int test_wcstombs_s(void) {
    errno_t rc;
    size_t ind = 0;
    const wchar_t *cs;
    const char *lang;
    const char *lc_cat;
    int errs = 0;
#ifndef HAVE_CT_BOS_OVR
    int have_wine = 0;
#endif

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

    cs = L"a";
    print_msvcrt(use_msvcrt);
#ifndef HAVE_CT_BOS_OVR
    EXPECT_BOS("empty retvalp") EXPECT_BOS("empty dest")
    rc = wcstombs_s(NULL, NULL, LEN, cs, 1);
    init_msvcrt(rc == ESNULLP, &use_msvcrt);
    ERR_MSVC(ESNULLP, EINVAL);

    EXPECT_BOS("empty dest or dmax")
    rc = wcstombs_s(&ind, dest, 0, cs, 1);
    if (use_msvcrt && rc == 0 && ind == 0) {
        printf("Using wine\n");
        have_wine = 1;
    }
    ERR_MSVC(ESZEROL, have_wine ? 0 : EINVAL);

    EXPECT_BOS("empty dest") EXPECT_BOS("empty dest or dmax")
    rc = wcstombs_s(&ind, NULL, 0, cs, 1);
    ERR_MSVC(ESNOSPC, 0);

    EXPECT_BOS("dest overflow")
    rc = wcstombs_s(&ind, dest, RSIZE_MAX_STR + 1, cs, 1);
    ERR_MSVC(ESLEMAX, 0);

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

    {
        const wchar_t *srcp = (const wchar_t *)(void *)dest;
        strcpy(dest, "abcdef");
        rc = wcstombs_s(&ind, dest, LEN, srcp, 3);
        ERR_MSVC(ESOVRLP, EILSEQ);

        dest[0] = 'a';
        dest[1] = '\0';
        rc = wcstombs_s(&ind, dest, LEN, srcp, 1);
        ERR_MSVC(ESOVRLP, 0);
    }

#ifndef HAVE_CT_BOS_OVR
    EXPECT_BOS("empty src") EXPECT_BOS("empty src or len")
    rc = wcstombs_s(&ind, dest, LEN, NULL, 0);
    ERR_MSVC(ESNULLP, have_wine ? EINVAL : 0);
    CHECK_SLACK(dest, LEN);
#endif

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

    memset(dest, 'x', LEN);
    rc = wcstombs_s(&ind, dest, LEN, (cs = L"abcdef", cs), 3);
    ERR(EOK);
    if (!use_msvcrt) {
        INDCMP(!= 3);
    } else {
        INDCMP(!= 4); /* !!! */
    }
    CHECK_SLACK(&dest[3], LEN - 3);

    rc = wcstombs_s(&ind, dest, LEN, (cs = L"abcdef", cs), 6);
    ERR(EOK);
    if (!use_msvcrt) {
        INDCMP(!= 6);
    } else {
        INDCMP(!= 7);
    }
    CHECK_SLACK(&dest[6], LEN - 6);

#ifndef HAVE_CT_BOS_OVR
    memset(dest, 'x', LEN);
    EXPECT_BOS("empty dest")
    rc = wcstombs_s(&ind, NULL, LEN, (cs = L"abcdef", cs), 2);
    ERR_MSVC(EOK, EINVAL);
    if (!use_msvcrt) {
        INDCMP(!= 6);
    } else if (use_msvcrt) {
        INDCMP(!= 0);
    }
#endif

    SETLOCALE_C;
    SETLANG("C");
    CHKLOCALE_C;

    memset(dest, 'x', LEN);
    rc = wcstombs_s(&ind, dest, LEN, (cs = L"\x78", cs), 1);
    ERR(EOK);
    if (!use_msvcrt) {
        INDCMP(!= 1);
    } else {
        INDCMP(!= 2);
    }
    CHECK_SLACK(&dest[1], LEN - 1);

    src[0] = 0xdf81;
    src[1] = 0;
    cs = src;
    memset(dest, 'x', LEN);
    rc = wcstombs_s(&ind, dest, LEN, cs, LEN);
    if (rc == 0) { /* well, musl on ASCII allows this */
        INDCMP(!= 1);
        CHECK_SLACK(&dest[1], LEN - 1);
    } else {
        ERR(EILSEQ);
        if (!use_msvcrt) {
            INDCMP(!= -1);
        } else {
            INDCMP(!= 0);
        }
        CHECK_SLACK(&dest[0], LEN);
    }

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

    /* illegal sequences (locale independent) */

    memset(dest, 'x', LEN);
    rc = wcstombs_s(&ind, dest, LEN, (cs = L"0xdf79", cs), 1);
    ERR(EOK);
    INDCMP(!= 1);
    CHECK_SLACK(&dest[1], LEN - 1);

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

    return (errs);
}

#endif

#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) {
#ifdef HAVE_WCHAR_H
    return (test_wcstombs_s());
#else
    return 0;
#endif
}
#endif