File: test_vfwscanf_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 (268 lines) | stat: -rw-r--r-- 6,448 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
/*------------------------------------------------------------------
 * test_vfwscanf_s
 * File 'wchar/vfwscanf_s.c'
 * Lines executed:85.71% of 21
 *
 *------------------------------------------------------------------
 */

#include "test_private.h"
#include "safe_str_lib.h"
#include "safe_lib.h"
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>

#undef USE_PIPE

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

#define LEN (128)

static wchar_t wstr1[LEN];
static wchar_t wstr2[LEN];
static char str3[LEN];
#define TMP "tmpvfwscanf"
static FILE *stream = NULL;
static void win_stuff_stream(const wchar_t *dest);
static int vtwscanf_s(FILE *stream, const wchar_t *restrict fmt, ...);
static int test_vfwscanf_s(void);

static void win_stuff_stream(const wchar_t *dest) {
    if (!stream)
        stream = fopen(TMP, "w+");
    else
        rewind(stream);
    fwprintf(stream, L"%ls\n", dest);
    rewind(stream);
}

#ifndef USE_PIPE
#define stuff_stream(s)                                                        \
    wcscpy(wstr1, s);                                                          \
    win_stuff_stream(s);
#else
#define stuff_stream(s)                                                        \
    wcscpy(wstr1, s);                                                          \
    write(p[1], (s), sizeof(s) - 1);                                           \
    write(p[1], L"\n", sizeof(L"\n") - 1);
#endif

static int vtwscanf_s(FILE *restrict f, const wchar_t *restrict fmt, ...) {
    int rc;
    va_list ap;
    va_start(ap, fmt);
    rc = vfwscanf_s(f, fmt, ap);
    va_end(ap);
    return rc;
}

static int test_vfwscanf_s(void) {
    errno_t rc;
    int32_t ind;
    size_t len1;
    size_t len2;
    size_t len3;
    int num = 0;
    int errs = 0;
#ifdef USE_PIPE
    int p[2];
#endif

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

    print_msvcrt(use_msvcrt);

/* This pipe does not work on windows */
#ifdef USE_PIPE
    pipe(p);
    stream = fdopen(p[0], "rb");
    if (!stream) {
        close(p[0]);
        close(p[1]);
        return 0;
    }
#endif
    stuff_stream(L"a");
    wstr2[0] = '\0';
    rc = vtwscanf_s(NULL, L"%ls", wstr2, LEN);
    init_msvcrt(errno == ESNULLP, &use_msvcrt);
    ERR(EOF);
    ERRNO_MSVC(ESNULLP, EINVAL);
    WEXPSTR(wstr2, L"");

    rc = vtwscanf_s(stream, NULL);
    ERR(EOF);
    ERRNO_MSVC(ESNULLP, EINVAL);

#if 0
    /* Illegal: */
    rc = vfwscanf_s(stream, NULL, NULL);
    ERR(EOF);
    ERRNO_MSVC(ESNULLP, EINVAL);

    /* SEGV: */
    rc = vfwscanf_s(stream, L"", NULL);
    ERR(EOF);
    ERRNO_MSVC(ESNULLP, EINVAL);
    rc = vfwscanf_s(stream, L"%ls", NULL);
    ERREOF(ESNULLP);
#endif

    /*--------------------------------------------------*/
    stuff_stream(L"      24");
    rc = vtwscanf_s(stream, L"%ls %n", wstr2, LEN, &ind);
    ERREOF(EINVAL);

    stuff_stream(L"      24");
    rc = vtwscanf_s(stream, L"%ls %%n", wstr2, LEN);
#ifdef BSD_LIKE
    if (rc != 0) { /* BSD's return -1 on %%n */
        printf("%s %u wrong vfwscanf(\"\",L\"%%n\"): %d\n", __FUNCTION__,
               __LINE__, (int)rc);
    } else
#endif
        ERR(1);
    ERRNO(0);

    stuff_stream(L"      24");
    rc = vtwscanf_s(stream, L"%ls %%n", wstr2, 6);
    ERR(1);
    ERRNO(0);

    stuff_stream(L"      24");
    rc = vtwscanf_s(stream, L"%s %%n", str3, 6);
    ERR(1);
    ERRNO(0);
#if !defined(_WIN32) || defined(HAVE_MINGW64)
    EXPSTR(str3, "24");
#else
    EXPSTR(str3, "2");
#endif

    stuff_stream(L"      24");
    rc = vtwscanf_s(stream, L" %d", &num);
    ERR(1);
    ERRNO(0);
    if (num != 24) {
        debug_printf("%s %u wrong arg: %d\n", __FUNCTION__, __LINE__, num);
        errs++;
    }

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

    stuff_stream(L"aaaaaaaaaa");

    rc = vtwscanf_s(stream, L"%ls", wstr2, LEN);
    ERR(1)
    len2 = wcslen(wstr2);
    len3 = wcslen(wstr1);
    if (len3 != len2) {
#ifdef DEBUG
        len1 = wcslen(wstr1);
#endif
        debug_printf("%s %u lengths wrong: %d  %d  %d \n", __FUNCTION__,
                     __LINE__, (int)len1, (int)len2, (int)len3);
        errs++;
    }

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

    stuff_stream(L"keep it simple");

    rc = vtwscanf_s(stream, L"%ls", wstr2, LEN);
    ERR(1);
    WEXPSTR(wstr1, L"keep it simple")

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

    wcscpy(wstr2, L"keep it simple");
    stuff_stream(L"qqweqq");

    rc = vtwscanf_s(stream, L"%ls", wstr2);
    NOERR()
    WEXPSTR(wstr1, wstr2);

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

    wstr1[0] = '\0';
    wstr2[0] = '\0';
    stuff_stream(L"");

    rc = vtwscanf_s(stream, L"%ls", wstr2, LEN);
    ERR(1)
    WEXPNULL(wstr1)

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

    wstr1[0] = '\0';
    wcscpy(wstr2, L"keep it simple");
    stuff_stream(L"");

    rc = vtwscanf_s(stream, L"%ls", wstr2, LEN);
    ERR(1)
    WEXPSTR(wstr1, L"")

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

    /* overlapping works fine on darwin, different on linux glibc */
    /*
    wcscpy(wstr1, L"12345678901234567890");

    rc = vtwscanf_s(stream, L"%ls", &wstr1[7]);
    ERR(1);
    WEXPSTR(wstr1, L"123456712345678901234567890");

    wcscpy(wstr1, L"123456789");

    rc = vtwscanf_s(stream, L"%ls", &wstr1[8]);
    ERR(1);
    WEXPSTR(wstr1, L"12345678123456789");
    */

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

    /* TODO: we want to test a vfwscanf error propagated through vfwscanf_s.
       The only error is EOF(stream), but this is blocking.
       Reading from a closed stream is platform dependent.
     */
    fclose(stream);
#ifdef USE_PIPE
    close(p[1]);
#endif
#if 0
    rc = vtwscanf_s(stream, L"%ls", wstr2, LEN);

#if defined(__GLIBC__)
    if (rc < 0) {
        ERR(-1);
        ERRNO(EBADF);
    } else {
        ERR(14); /* older glibc upstream bug */
        ERRNO(0);
    }
#elif defined __CYGWIN__
    ERR(1);
#else
    ERR(-1);
#endif
    /*WEXPNULL(wstr2); TODO zero the output args */
#endif

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

    unlink(TMP);

    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_vfwscanf_s()); }
#endif