File: test_wctomb_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 (194 lines) | stat: -rw-r--r-- 4,854 bytes parent folder | download | duplicates (3)
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
/*------------------------------------------------------------------
 * test_wctomb_s
 * File 'wchar/wctomb_s.c'
 * Lines executed:94.74% of 19
 *
 *------------------------------------------------------------------
 */

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

#ifdef HAVE_WCTOMB_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;
int test_wctomb_s(void);

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

int test_wctomb_s(void) {
    errno_t rc;
    int ind;
    const char *lang;
    const char *lc_cat;
    int errs = 0;

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

    src = L'a';
    print_msvcrt(use_msvcrt);
#ifndef HAVE_CT_BOS_OVR
    EXPECT_BOS("empty retvalp")
    rc = wctomb_s(NULL, dest, LEN, src);
    init_msvcrt(rc == ESNULLP, &use_msvcrt);
    ERR_MSVC(ESNULLP, 0);

    EXPECT_BOS("dest overflow or empty")
    rc = wctomb_s(&ind, dest, 0, src);
    ERR_MSVC(ESZEROL, ERANGE);

    EXPECT_BOS("dest overflow or empty")
    rc = wctomb_s(&ind, dest, RSIZE_MAX_STR + 1, src);
    ERR_MSVC(ESLEMAX, 0);

#ifdef HAVE___BUILTIN_OBJECT_SIZE
    EXPECT_BOS("dest overflow or empty")
    rc = wctomb_s(&ind, dest, LEN + 1, src);
    ERR(EOVERFLOW);
#endif
#endif

    /* check dmax zero, with !dest */
    rc = wctomb_s(&ind, NULL, 1, src);
    ERR_MSVC(ESNULLP, ERANGE);

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

    rc = wctomb_s(&ind, dest, LEN, L'\0');
    ERR(EOK);
    INDCMP(!= 1);
    CHECK_SLACK(&dest[1], LEN - 1);

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

    rc = wctomb_s(&ind, dest, LEN, L'\a');
    ERR(EOK);
    INDCMP(!= 1);
    CHECK_SLACK(&dest[1], LEN - 1);

    SETLOCALE_C;
    SETLANG("C");
    CHKLOCALE_C;

    /* no-breaking space illegal in ASCII, but legal in C */
    rc = wctomb_s(&ind, dest, LEN, L'\xa0');
    if (rc == 0) { /* legal */
        ERR(EOK);
        INDCMP(!= 1);
        if ((unsigned char)dest[0] != 0xa0) {
            printf("%s %u  Error  ind=%d rc=%d %d\n", __FUNCTION__, __LINE__,
                   (int)ind, rc, dest[0]);
            errs++;
        }
        if (dest[1] != L'\0') {
            printf("%s %u  Error  ind=%d rc=%d %d\n", __FUNCTION__, __LINE__,
                   (int)ind, rc, dest[1]);
            errs++;
        }
        CHECK_SLACK(&dest[1], LEN - 1);
    } else {
        ERR(EILSEQ); /* or illegal */
        INDCMP(!= -1);
        if (dest[0] != '\0') {
            printf("%s %u  Error  ind=%d rc=%d %d\n", __FUNCTION__, __LINE__,
                   (int)ind, rc, dest[0]);
            errs++;
        }
        CHECK_SLACK(&dest[0], LEN);
    }

    rc = wctomb_s(&ind, dest, LEN, L'\x78');
    ERR(EOK);
    INDCMP(!= 1);
    CHECK_SLACK(&dest[1], LEN - 1);

    /* surrogates */
    rc = wctomb_s(&ind, dest, LEN, L'\xdf81');
    if (rc == 0) { /* well, musl on ASCII allows this */
        INDCMP(!= 1);
    } else {
        ERR(EILSEQ); /* FIXME NOSPC */
        INDCMP(!= -1);
    }

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

    /* overlarge utf-8 sequence */
    rc = wctomb_s(&ind, dest, 2, L'\x2219');
    ERR_MSVC(ESNOSPC, EILSEQ);
    CHECK_SLACK(&dest[0], 2);

    rc = wctomb_s(&ind, dest, 3, L'\x2219');
    ERR_MSVC(ESNOSPC, EILSEQ);
    CHECK_SLACK(&dest[0], 3);

    rc = wctomb_s(&ind, dest, 4, L'\x2219');
    ERR(EOK);
    INDCMP(!= 3);
    if (dest[0] != '\xe2') {
        printf("%s %u  Error  ind=%d rc=%d %x\n", __FUNCTION__, __LINE__,
               (int)ind, rc, dest[0]);
        errs++;
    }
    if (dest[1] != '\x88') {
        printf("%s %u  Error  ind=%d rc=%d %x\n", __FUNCTION__, __LINE__,
               (int)ind, rc, dest[1]);
        errs++;
    }
    if (dest[2] != '\x99') {
        printf("%s %u  Error  ind=%d rc=%d %x\n", __FUNCTION__, __LINE__,
               (int)ind, rc, dest[2]);
        errs++;
    }
    if (dest[3] != '\0') {
        printf("%s %u  Error  ind=%d rc=%d %x\n", __FUNCTION__, __LINE__,
               (int)ind, rc, dest[3]);
        errs++;
    }

    /* illegal unicode. but some may allow it: 0xf0 0x9d 0x8c 0x81 */
    /* 32bit: hex escape sequence out of range */
    rc = wctomb_s(&ind, dest, LEN, L'\xd834df01');
#if SIZEOF_WCHAR_T == 2
    ERR(0);
    CHECK_SLACK(&dest[ind], LEN - ind);
#else
    ERR(EILSEQ); /* FIXME NOSPC */
    INDCMP(!= -1);
    CHECK_SLACK(&dest[0], LEN);
#endif

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

    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_wctomb_s());
#else
    return 0;
#endif
}
#endif