]> git.ipfire.org Git - thirdparty/glibc.git/blame - libio/tst_swprintf.c
Bug 20116: Fix use after free in pthread_create()
[thirdparty/glibc.git] / libio / tst_swprintf.c
CommitLineData
d64b6ad0
UD
1#include <stdio.h>
2#include <wchar.h>
5569e0a6
UD
3#include <sys/types.h>
4
5
6static wchar_t buf[100];
7#define nbuf (sizeof (buf) / sizeof (buf[0]))
8static const struct
9{
10 size_t n;
11 const char *str;
12 ssize_t exp;
13} tests[] =
14 {
15 { nbuf, "hello world", 11 },
16 { 0, "hello world", -1 },
17 { 0, "", -1 },
18 { nbuf, "", 0 }
19 };
d64b6ad0
UD
20
21int
22main (int argc, char *argv[])
23{
57b36a0a 24 size_t n;
d64b6ad0
UD
25 int result = 0;
26
27 puts ("test 1");
5569e0a6 28 n = swprintf (buf, nbuf, L"Hello %s", "world");
d64b6ad0
UD
29 if (n != 11)
30 {
9a2d7205 31 printf ("incorrect return value: %zd instead of 11\n", n);
d64b6ad0
UD
32 result = 1;
33 }
5569e0a6 34 else if (wcscmp (buf, L"Hello world") != 0)
d64b6ad0
UD
35 {
36 printf ("incorrect string: L\"%ls\" instead of L\"Hello world\"\n", buf);
37 result = 1;
38 }
39
40 puts ("test 2");
5569e0a6 41 n = swprintf (buf, nbuf, L"Is this >%g< 3.1?", 3.1);
d64b6ad0 42 if (n != 18)
5569e0a6 43 {
9a2d7205 44 printf ("incorrect return value: %zd instead of 18\n", n);
d64b6ad0
UD
45 result = 1;
46 }
5569e0a6 47 else if (wcscmp (buf, L"Is this >3.1< 3.1?") != 0)
d64b6ad0
UD
48 {
49 printf ("incorrect string: L\"%ls\" instead of L\"Is this >3.1< 3.1?\"\n",
50 buf);
51 result = 1;
52 }
53
5569e0a6
UD
54 for (n = 0; n < sizeof (tests) / sizeof (tests[0]); ++n)
55 {
56 ssize_t res = swprintf (buf, tests[n].n, L"%s", tests[n].str);
57
58 if (tests[n].exp < 0 && res >= 0)
59 {
60 printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to fail\n",
61 tests[n].n, tests[n].str);
62 result = 1;
63 }
64 else if (tests[n].exp >= 0 && tests[n].exp != res)
65 {
66 printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to return %Zd, but got %Zd\n",
67 tests[n].n, tests[n].str, tests[n].exp, res);
68 result = 1;
69 }
70 else
2e22fed2
UD
71 printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") OK\n",
72 tests[n].n, tests[n].str);
73 }
74
75 if (swprintf (buf, nbuf, L"%.0s", "foo") != 0
76 || wcslen (buf) != 0)
77 {
78 printf ("swprintf (buf, %Zu, L\"%%.0s\", \"foo\") create some output\n",
79 nbuf);
80 result = 1;
81 }
82
83 if (swprintf (buf, nbuf, L"%.0ls", L"foo") != 0
84 || wcslen (buf) != 0)
85 {
86 printf ("swprintf (buf, %Zu, L\"%%.0ls\", L\"foo\") create some output\n",
87 nbuf);
88 result = 1;
5569e0a6
UD
89 }
90
d64b6ad0
UD
91 return result;
92}