2023-01-06 21:08:04 +00:00
|
|
|
/* Copyright (C) 2000-2023 Free Software Foundation, Inc.
|
2000-07-28 17:45:15 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 04:58:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2000-07-28 17:45:15 +00:00
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 04:58:11 +00:00
|
|
|
Lesser General Public License for more details.
|
2000-07-28 17:45:15 +00:00
|
|
|
|
2001-07-06 04:58:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2000-07-28 17:45:15 +00:00
|
|
|
|
2017-11-02 11:14:51 +00:00
|
|
|
#include <array_length.h>
|
2000-07-28 17:45:15 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <locale.h>
|
|
|
|
|
2007-02-18 09:21:24 +00:00
|
|
|
#ifndef CHAR
|
|
|
|
# define CHAR char
|
|
|
|
# define L(str) str
|
|
|
|
# define SSCANF sscanf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const CHAR *str_double[] =
|
2000-07-28 17:45:15 +00:00
|
|
|
{
|
2007-02-18 09:21:24 +00:00
|
|
|
L("-.10000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01"),
|
|
|
|
L("0.10000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01"),
|
|
|
|
L("-1234567E0198765432E0912345678901987654321091234567890198765432109"),
|
|
|
|
L("-0.1000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01")
|
2000-07-28 17:45:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const double val_double[] =
|
|
|
|
{
|
|
|
|
-.10000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01,
|
|
|
|
0.10000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01,
|
2002-10-11 10:55:33 +00:00
|
|
|
-1234567E01, 98765432E09, 12345678901.0, 98765432109.0, 12345678901.0,
|
|
|
|
98765432109.0,
|
2000-07-28 17:45:15 +00:00
|
|
|
-0.1000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01
|
|
|
|
};
|
|
|
|
|
2007-02-18 09:21:24 +00:00
|
|
|
const CHAR *str_long[] =
|
2000-07-28 17:45:15 +00:00
|
|
|
{
|
2007-02-18 09:21:24 +00:00
|
|
|
L("-12345678987654321123456789987654321123456789987654321"),
|
|
|
|
L("-12345678987654321123456789987654321123456789987654321"),
|
|
|
|
L("-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321"),
|
|
|
|
L("-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321")
|
2000-07-28 17:45:15 +00:00
|
|
|
};
|
|
|
|
|
2007-02-18 09:21:24 +00:00
|
|
|
const CHAR *fmt_long[] =
|
2000-07-28 17:45:15 +00:00
|
|
|
{
|
2007-02-18 09:21:24 +00:00
|
|
|
L("%9ld%9ld%9ld%9ld%9ld%9ld"),
|
|
|
|
L("%I9ld%I9ld%I9ld%I9ld%I9ld%I9ld"),
|
|
|
|
L("%'11ld%'11ld%'11ld%'11ld%'11ld%'11ld"),
|
|
|
|
L("%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld")
|
2000-07-28 17:45:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const long int val_long[] =
|
|
|
|
{
|
|
|
|
-12345678, 987654321, 123456789, 987654321, 123456789, 987654321
|
|
|
|
};
|
|
|
|
|
2007-02-18 19:03:30 +00:00
|
|
|
struct test
|
2004-04-20 04:28:00 +00:00
|
|
|
{
|
2007-02-18 09:21:24 +00:00
|
|
|
const CHAR *str;
|
|
|
|
const CHAR *fmt;
|
2004-04-20 04:28:00 +00:00
|
|
|
int retval;
|
2007-02-18 09:21:24 +00:00
|
|
|
} int_tests[] =
|
2004-04-20 04:28:00 +00:00
|
|
|
{
|
2007-02-18 09:21:24 +00:00
|
|
|
{ L("foo\n"), L("foo\nbar"), -1 },
|
|
|
|
{ L("foo\n"), L("foo bar"), -1 },
|
|
|
|
{ L("foo\n"), L("foo %d"), -1 },
|
|
|
|
{ L("foo\n"), L("foo\n%d"), -1 },
|
|
|
|
{ L("foon"), L("foonbar"), -1 },
|
|
|
|
{ L("foon"), L("foon%d"), -1 },
|
|
|
|
{ L("foo "), L("foo bar"), -1 },
|
|
|
|
{ L("foo "), L("foo %d"), -1 },
|
|
|
|
{ L("foo\t"), L("foo\tbar"), -1 },
|
|
|
|
{ L("foo\t"), L("foo bar"), -1 },
|
|
|
|
{ L("foo\t"), L("foo %d"), -1 },
|
|
|
|
{ L("foo\t"), L("foo\t%d"), -1 },
|
|
|
|
{ L("foo"), L("foo"), 0 },
|
|
|
|
{ L("foon"), L("foo bar"), 0 },
|
|
|
|
{ L("foon"), L("foo %d"), 0 },
|
|
|
|
{ L("foo "), L("fooxbar"), 0 },
|
|
|
|
{ L("foo "), L("foox%d"), 0 },
|
|
|
|
{ L("foo bar"), L("foon"), 0 },
|
|
|
|
{ L("foo bar"), L("foo bar"), 0 },
|
|
|
|
{ L("foo bar"), L("foo %d"), 0 },
|
|
|
|
{ L("foo bar"), L("foon%d"), 0 },
|
2013-11-18 11:41:00 +00:00
|
|
|
{ L("foo (nil)"), L("foo %p"), 1},
|
|
|
|
{ L("foo (nil)"), L("foo %4p"), 0},
|
2007-02-18 09:21:24 +00:00
|
|
|
{ L("foo "), L("foo %n"), 0 },
|
|
|
|
{ L("foo%bar1"), L("foo%%bar%d"), 1 },
|
2004-04-20 18:58:56 +00:00
|
|
|
/* Some OSes skip whitespace here while others don't. */
|
2007-02-18 09:21:24 +00:00
|
|
|
{ L("foo \t %bar1"), L("foo%%bar%d"), 1 }
|
2004-04-20 04:28:00 +00:00
|
|
|
};
|
|
|
|
|
2007-02-18 19:03:30 +00:00
|
|
|
struct test double_tests[] =
|
|
|
|
{
|
|
|
|
{ L("-1"), L("%1g"), 0 },
|
|
|
|
{ L("-.1"), L("%2g"), 0 },
|
|
|
|
{ L("-inf"), L("%3g"), 0 },
|
|
|
|
{ L("+0"), L("%1g"), },
|
|
|
|
{ L("-0x1p0"), L("%2g"), 1 },
|
|
|
|
{ L("-..1"), L("%g"), 0 },
|
|
|
|
{ L("-inf"), L("%g"), 1 }
|
|
|
|
};
|
|
|
|
|
2013-10-31 11:51:03 +00:00
|
|
|
struct test2
|
|
|
|
{
|
|
|
|
const CHAR *str;
|
|
|
|
const CHAR *fmt;
|
|
|
|
int retval;
|
|
|
|
char residual;
|
|
|
|
} double_tests2[] =
|
|
|
|
{
|
|
|
|
{ L("0e+0"), L("%g%c"), 1, 0 },
|
|
|
|
{ L("0xe+0"), L("%g%c"), 2, '+' },
|
|
|
|
{ L("0x.e+0"), L("%g%c"), 2, '+' },
|
|
|
|
};
|
|
|
|
|
2015-07-09 15:29:17 +00:00
|
|
|
static int
|
|
|
|
do_test (void)
|
2000-07-28 17:45:15 +00:00
|
|
|
{
|
|
|
|
double d[6];
|
|
|
|
long l[6];
|
|
|
|
int i, j;
|
|
|
|
int tst_locale;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
tst_locale = 1;
|
|
|
|
if (tst_locale)
|
|
|
|
if (setlocale (LC_ALL, "en_US.ISO-8859-1") == NULL)
|
|
|
|
{
|
|
|
|
puts ("Failed to set en_US locale, skipping locale related tests");
|
|
|
|
tst_locale = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 4; ++i)
|
|
|
|
{
|
2007-02-18 09:21:24 +00:00
|
|
|
if (SSCANF (str_double[i], L("%11lf%11lf%11lf%11lf%11lf%11lf"),
|
2000-07-28 17:45:15 +00:00
|
|
|
&d[0], &d[1], &d[2], &d[3], &d[4], &d[5]) != 6)
|
|
|
|
{
|
|
|
|
printf ("Double sscanf test %d wrong number of "
|
|
|
|
"assigned inputs\n", i);
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for (j = 0; j < 6; ++j)
|
|
|
|
if (d[j] != val_double[6 * i + j])
|
|
|
|
{
|
|
|
|
printf ("Double sscanf test %d failed (%g instead of %g)\n",
|
|
|
|
i, d[j], val_double[6 * i + j]);
|
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 4; ++i)
|
|
|
|
{
|
2007-02-18 09:21:24 +00:00
|
|
|
if (SSCANF (str_long[i], fmt_long[i],
|
2000-07-28 17:45:15 +00:00
|
|
|
&l[0], &l[1], &l[2], &l[3], &l[4], &l[5]) != 6)
|
|
|
|
{
|
|
|
|
printf ("Integer sscanf test %d wrong number of "
|
|
|
|
"assigned inputs\n", i);
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for (j = 0; j < 6; ++j)
|
|
|
|
if (l[j] != val_long[j])
|
|
|
|
{
|
|
|
|
printf ("Integer sscanf test %d failed (%ld instead %ld)\n",
|
|
|
|
i, l[j], val_long[j]);
|
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! tst_locale)
|
|
|
|
break;
|
|
|
|
}
|
2000-12-31 10:52:32 +00:00
|
|
|
|
2017-11-02 11:14:51 +00:00
|
|
|
for (i = 0; i < array_length (int_tests); ++i)
|
2004-04-20 04:28:00 +00:00
|
|
|
{
|
2014-02-06 06:13:13 +00:00
|
|
|
long dummy;
|
|
|
|
int ret;
|
2004-04-20 04:28:00 +00:00
|
|
|
|
2007-02-18 09:21:24 +00:00
|
|
|
if ((ret = SSCANF (int_tests[i].str, int_tests[i].fmt,
|
2004-04-20 04:28:00 +00:00
|
|
|
&dummy)) != int_tests[i].retval)
|
|
|
|
{
|
|
|
|
printf ("int_tests[%d] returned %d != %d\n",
|
|
|
|
i, ret, int_tests[i].retval);
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 11:14:51 +00:00
|
|
|
for (i = 0; i < array_length (double_tests); ++i)
|
2007-02-18 19:03:30 +00:00
|
|
|
{
|
|
|
|
double dummy;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = SSCANF (double_tests[i].str, double_tests[i].fmt,
|
|
|
|
&dummy)) != double_tests[i].retval)
|
|
|
|
{
|
|
|
|
printf ("double_tests[%d] returned %d != %d\n",
|
|
|
|
i, ret, double_tests[i].retval);
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 11:14:51 +00:00
|
|
|
for (i = 0; i < array_length (double_tests2); ++i)
|
2013-10-31 11:51:03 +00:00
|
|
|
{
|
|
|
|
double dummy;
|
|
|
|
int ret;
|
|
|
|
char c = 0;
|
|
|
|
|
|
|
|
if ((ret = SSCANF (double_tests2[i].str, double_tests2[i].fmt,
|
|
|
|
&dummy, &c)) != double_tests2[i].retval)
|
|
|
|
{
|
|
|
|
printf ("double_tests2[%d] returned %d != %d\n",
|
|
|
|
i, ret, double_tests2[i].retval);
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
else if (ret == 2 && c != double_tests2[i].residual)
|
|
|
|
{
|
|
|
|
printf ("double_tests2[%d] stopped at '%c' != '%c'\n",
|
|
|
|
i, c, double_tests2[i].residual);
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 05:30:42 +00:00
|
|
|
/* BZ #16618
|
|
|
|
The test will segfault during SSCANF if the buffer overflow
|
|
|
|
is not fixed. The size of `s` is such that it forces the use
|
|
|
|
of malloc internally and this triggers the incorrect computation.
|
|
|
|
Thus the value for SIZE is arbitrariy high enough that malloc
|
|
|
|
is used. */
|
|
|
|
{
|
|
|
|
#define SIZE 131072
|
|
|
|
CHAR *s = malloc ((SIZE + 1) * sizeof (*s));
|
|
|
|
if (s == NULL)
|
|
|
|
abort ();
|
|
|
|
for (size_t i = 0; i < SIZE; i++)
|
|
|
|
s[i] = L('0');
|
|
|
|
s[SIZE] = L('\0');
|
|
|
|
int i = 42;
|
|
|
|
/* Scan multi-digit zero into `i`. */
|
|
|
|
if (SSCANF (s, L("%d"), &i) != 1)
|
|
|
|
{
|
|
|
|
printf ("FAIL: bug16618: SSCANF did not read one input item.\n");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
printf ("FAIL: bug16618: Value of `i` was not zero as expected.\n");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
free (s);
|
|
|
|
if (result != 1)
|
|
|
|
printf ("PASS: bug16618: Did not crash.\n");
|
|
|
|
#undef SIZE
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-31 10:52:32 +00:00
|
|
|
return result;
|
2000-07-28 17:45:15 +00:00
|
|
|
}
|
2015-07-09 15:29:17 +00:00
|
|
|
|
|
|
|
#define TEST_FUNCTION do_test ()
|
|
|
|
#include "../test-skeleton.c"
|