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
|
#include <array_length.h>
#include <errno.h>
#include <stdio.h>
#include <support/check.h>
#include <sys/types.h>
#include <wchar.h>
static wchar_t buf[100];
static const struct
{
size_t n;
const char *str;
ssize_t exp;
} tests[] =
{
{ array_length (buf), "hello world", 11 },
{ 0, "hello world", -1 },
{ 1, "hello world", -1 },
{ 2, "hello world", -1 },
{ 11, "hello world", -1 },
{ 12, "hello world", 11 },
{ 0, "", -1 },
{ array_length (buf), "", 0 }
};
static int
do_test (void)
{
size_t n;
TEST_COMPARE (swprintf (buf, array_length (buf), L"Hello %s", "world"), 11);
TEST_COMPARE_STRING_WIDE (buf, L"Hello world");
TEST_COMPARE (swprintf (buf, array_length (buf), L"Is this >%g< 3.1?", 3.1),
18);
TEST_COMPARE_STRING_WIDE (buf, L"Is this >3.1< 3.1?");
for (n = 0; n < array_length (tests); ++n)
{
wmemset (buf, 0xabcd, array_length (buf));
errno = 0;
ssize_t res = swprintf (buf, tests[n].n, L"%s", tests[n].str);
if (tests[n].exp < 0 && res >= 0)
{
support_record_failure ();
printf ("swprintf (buf, %zu, L\"%%s\", \"%s\") expected to fail\n",
tests[n].n, tests[n].str);
}
else if (tests[n].exp >= 0 && tests[n].exp != res)
{
support_record_failure ();
printf ("\
swprintf (buf, %zu, L\"%%s\", \"%s\") expected to return %zd, but got %zd\n",
tests[n].n, tests[n].str, tests[n].exp, res);
}
else if (res < 0
&& tests[n].n > 0
&& wcsnlen (buf, array_length (buf)) == array_length (buf))
{
support_record_failure ();
printf ("\
error: swprintf (buf, %zu, L\"%%s\", \"%s\") missing null terminator\n",
tests[n].n, tests[n].str);
}
else if (res < 0
&& tests[n].n > 0
&& wcsnlen (buf, array_length (buf)) < array_length (buf)
&& buf[wcsnlen (buf, array_length (buf)) + 1] != 0xabcd)
{
support_record_failure ();
printf ("\
error: swprintf (buf, %zu, L\"%%s\", \"%s\") out of bounds write\n",
tests[n].n, tests[n].str);
}
if (res < 0 && tests[n].n < 0)
TEST_COMPARE (errno, E2BIG);
printf ("swprintf (buf, %zu, L\"%%s\", \"%s\") OK\n",
tests[n].n, tests[n].str);
}
TEST_COMPARE (swprintf (buf, array_length (buf), L"%.0s", "foo"), 0);
TEST_COMPARE_STRING_WIDE (buf, L"");
TEST_COMPARE (swprintf (buf, array_length (buf), L"%.0ls", L"foo"), 0);
TEST_COMPARE_STRING_WIDE (buf, L"");
return 0;
}
#include <support/test-driver.c>
|