]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/tst-sscanf.c
sparc (64bit): Regenerate ulps
[thirdparty/glibc.git] / stdio-common / tst-sscanf.c
CommitLineData
6d7e8eda 1/* Copyright (C) 2000-2023 Free Software Foundation, Inc.
eac4282f 2 This file is part of the GNU C Library.
eac4282f
UD
3
4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
eac4282f
UD
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 12 Lesser General Public License for more details.
eac4282f 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
eac4282f 17
d10b132b 18#include <array_length.h>
eac4282f
UD
19#include <stdlib.h>
20#include <stdio.h>
21#include <locale.h>
22
821d3e70
UD
23#ifndef CHAR
24# define CHAR char
25# define L(str) str
26# define SSCANF sscanf
27#endif
28
29const CHAR *str_double[] =
eac4282f 30{
821d3e70
UD
31 L("-.10000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01"),
32 L("0.10000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01"),
33 L("-1234567E0198765432E0912345678901987654321091234567890198765432109"),
34 L("-0.1000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01")
eac4282f
UD
35};
36
37const double val_double[] =
38{
39 -.10000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01,
40 0.10000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01,
3021e36d
RM
41 -1234567E01, 98765432E09, 12345678901.0, 98765432109.0, 12345678901.0,
42 98765432109.0,
eac4282f
UD
43 -0.1000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01
44};
45
821d3e70 46const CHAR *str_long[] =
eac4282f 47{
821d3e70
UD
48 L("-12345678987654321123456789987654321123456789987654321"),
49 L("-12345678987654321123456789987654321123456789987654321"),
50 L("-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321"),
51 L("-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321")
eac4282f
UD
52};
53
821d3e70 54const CHAR *fmt_long[] =
eac4282f 55{
821d3e70
UD
56 L("%9ld%9ld%9ld%9ld%9ld%9ld"),
57 L("%I9ld%I9ld%I9ld%I9ld%I9ld%I9ld"),
58 L("%'11ld%'11ld%'11ld%'11ld%'11ld%'11ld"),
59 L("%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld")
eac4282f
UD
60};
61
62const long int val_long[] =
63{
64 -12345678, 987654321, 123456789, 987654321, 123456789, 987654321
65};
66
ac2ca022 67struct test
d3b52028 68{
821d3e70
UD
69 const CHAR *str;
70 const CHAR *fmt;
d3b52028 71 int retval;
821d3e70 72} int_tests[] =
d3b52028 73{
821d3e70
UD
74 { L("foo\n"), L("foo\nbar"), -1 },
75 { L("foo\n"), L("foo bar"), -1 },
76 { L("foo\n"), L("foo %d"), -1 },
77 { L("foo\n"), L("foo\n%d"), -1 },
78 { L("foon"), L("foonbar"), -1 },
79 { L("foon"), L("foon%d"), -1 },
80 { L("foo "), L("foo bar"), -1 },
81 { L("foo "), L("foo %d"), -1 },
82 { L("foo\t"), L("foo\tbar"), -1 },
83 { L("foo\t"), L("foo bar"), -1 },
84 { L("foo\t"), L("foo %d"), -1 },
85 { L("foo\t"), L("foo\t%d"), -1 },
86 { L("foo"), L("foo"), 0 },
87 { L("foon"), L("foo bar"), 0 },
88 { L("foon"), L("foo %d"), 0 },
89 { L("foo "), L("fooxbar"), 0 },
90 { L("foo "), L("foox%d"), 0 },
91 { L("foo bar"), L("foon"), 0 },
92 { L("foo bar"), L("foo bar"), 0 },
93 { L("foo bar"), L("foo %d"), 0 },
94 { L("foo bar"), L("foon%d"), 0 },
728dab0e
OB
95 { L("foo (nil)"), L("foo %p"), 1},
96 { L("foo (nil)"), L("foo %4p"), 0},
821d3e70
UD
97 { L("foo "), L("foo %n"), 0 },
98 { L("foo%bar1"), L("foo%%bar%d"), 1 },
f9a06dc1 99 /* Some OSes skip whitespace here while others don't. */
821d3e70 100 { L("foo \t %bar1"), L("foo%%bar%d"), 1 }
d3b52028
UD
101};
102
ac2ca022
UD
103struct test double_tests[] =
104{
105 { L("-1"), L("%1g"), 0 },
106 { L("-.1"), L("%2g"), 0 },
107 { L("-inf"), L("%3g"), 0 },
108 { L("+0"), L("%1g"), },
109 { L("-0x1p0"), L("%2g"), 1 },
110 { L("-..1"), L("%g"), 0 },
111 { L("-inf"), L("%g"), 1 }
112};
113
a4966c61
AS
114struct test2
115{
116 const CHAR *str;
117 const CHAR *fmt;
118 int retval;
119 char residual;
120} double_tests2[] =
121{
122 { L("0e+0"), L("%g%c"), 1, 0 },
123 { L("0xe+0"), L("%g%c"), 2, '+' },
124 { L("0x.e+0"), L("%g%c"), 2, '+' },
125};
126
0035851c
AS
127static int
128do_test (void)
eac4282f
UD
129{
130 double d[6];
131 long l[6];
132 int i, j;
133 int tst_locale;
134 int result = 0;
135
136 tst_locale = 1;
137 if (tst_locale)
138 if (setlocale (LC_ALL, "en_US.ISO-8859-1") == NULL)
139 {
140 puts ("Failed to set en_US locale, skipping locale related tests");
141 tst_locale = 0;
142 }
143
144 for (i = 0; i < 4; ++i)
145 {
821d3e70 146 if (SSCANF (str_double[i], L("%11lf%11lf%11lf%11lf%11lf%11lf"),
eac4282f
UD
147 &d[0], &d[1], &d[2], &d[3], &d[4], &d[5]) != 6)
148 {
149 printf ("Double sscanf test %d wrong number of "
150 "assigned inputs\n", i);
151 result = 1;
152 }
153 else
154 for (j = 0; j < 6; ++j)
155 if (d[j] != val_double[6 * i + j])
156 {
157 printf ("Double sscanf test %d failed (%g instead of %g)\n",
158 i, d[j], val_double[6 * i + j]);
159 result = 1;
160 break;
161 }
162 }
163
164 for (i = 0; i < 4; ++i)
165 {
821d3e70 166 if (SSCANF (str_long[i], fmt_long[i],
eac4282f
UD
167 &l[0], &l[1], &l[2], &l[3], &l[4], &l[5]) != 6)
168 {
169 printf ("Integer sscanf test %d wrong number of "
170 "assigned inputs\n", i);
171 result = 1;
172 }
173 else
174 for (j = 0; j < 6; ++j)
175 if (l[j] != val_long[j])
176 {
177 printf ("Integer sscanf test %d failed (%ld instead %ld)\n",
178 i, l[j], val_long[j]);
179 result = 1;
180 break;
181 }
182
183 if (! tst_locale)
184 break;
185 }
bf4de8f3 186
d10b132b 187 for (i = 0; i < array_length (int_tests); ++i)
d3b52028 188 {
195b8165
DM
189 long dummy;
190 int ret;
d3b52028 191
821d3e70 192 if ((ret = SSCANF (int_tests[i].str, int_tests[i].fmt,
d3b52028
UD
193 &dummy)) != int_tests[i].retval)
194 {
195 printf ("int_tests[%d] returned %d != %d\n",
196 i, ret, int_tests[i].retval);
197 result = 1;
198 }
199 }
200
d10b132b 201 for (i = 0; i < array_length (double_tests); ++i)
ac2ca022
UD
202 {
203 double dummy;
204 int ret;
205
206 if ((ret = SSCANF (double_tests[i].str, double_tests[i].fmt,
207 &dummy)) != double_tests[i].retval)
208 {
209 printf ("double_tests[%d] returned %d != %d\n",
210 i, ret, double_tests[i].retval);
211 result = 1;
212 }
213 }
214
d10b132b 215 for (i = 0; i < array_length (double_tests2); ++i)
a4966c61
AS
216 {
217 double dummy;
218 int ret;
219 char c = 0;
220
221 if ((ret = SSCANF (double_tests2[i].str, double_tests2[i].fmt,
222 &dummy, &c)) != double_tests2[i].retval)
223 {
224 printf ("double_tests2[%d] returned %d != %d\n",
225 i, ret, double_tests2[i].retval);
226 result = 1;
227 }
228 else if (ret == 2 && c != double_tests2[i].residual)
229 {
230 printf ("double_tests2[%d] stopped at '%c' != '%c'\n",
231 i, c, double_tests2[i].residual);
232 result = 1;
233 }
234 }
235
5bd80bfe
PP
236 /* BZ #16618
237 The test will segfault during SSCANF if the buffer overflow
238 is not fixed. The size of `s` is such that it forces the use
239 of malloc internally and this triggers the incorrect computation.
240 Thus the value for SIZE is arbitrariy high enough that malloc
241 is used. */
242 {
243#define SIZE 131072
244 CHAR *s = malloc ((SIZE + 1) * sizeof (*s));
245 if (s == NULL)
246 abort ();
247 for (size_t i = 0; i < SIZE; i++)
248 s[i] = L('0');
249 s[SIZE] = L('\0');
250 int i = 42;
251 /* Scan multi-digit zero into `i`. */
252 if (SSCANF (s, L("%d"), &i) != 1)
253 {
254 printf ("FAIL: bug16618: SSCANF did not read one input item.\n");
255 result = 1;
256 }
257 if (i != 0)
258 {
259 printf ("FAIL: bug16618: Value of `i` was not zero as expected.\n");
260 result = 1;
261 }
262 free (s);
263 if (result != 1)
264 printf ("PASS: bug16618: Did not crash.\n");
265#undef SIZE
266 }
267
268
bf4de8f3 269 return result;
eac4282f 270}
0035851c
AS
271
272#define TEST_FUNCTION do_test ()
273#include "../test-skeleton.c"