2014-05-12 13:24:12 +00:00
|
|
|
/* Regression test for setlocale invalid environment variable handling.
|
2020-01-01 00:14:33 +00:00
|
|
|
Copyright (C) 2014-2020 Free Software Foundation, Inc.
|
2014-05-12 13:24:12 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
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.
|
|
|
|
|
|
|
|
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
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
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/>. */
|
2014-05-12 13:24:12 +00:00
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/* The result of setlocale may be overwritten by subsequent calls, so
|
|
|
|
this wrapper makes a copy. */
|
|
|
|
static char *
|
|
|
|
setlocale_copy (int category, const char *locale)
|
|
|
|
{
|
|
|
|
const char *result = setlocale (category, locale);
|
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
|
|
|
return strdup (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *de_locale;
|
|
|
|
|
|
|
|
static void
|
|
|
|
setlocale_fail (const char *envstring)
|
|
|
|
{
|
|
|
|
setenv ("LC_CTYPE", envstring, 1);
|
|
|
|
if (setlocale (LC_CTYPE, "") != NULL)
|
|
|
|
{
|
|
|
|
printf ("unexpected setlocale success for \"%s\" locale\n", envstring);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
const char *newloc = setlocale (LC_CTYPE, NULL);
|
|
|
|
if (strcmp (newloc, de_locale) != 0)
|
|
|
|
{
|
|
|
|
printf ("failed setlocale call \"%s\" changed locale to \"%s\"\n",
|
|
|
|
envstring, newloc);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setlocale_success (const char *envstring)
|
|
|
|
{
|
|
|
|
setenv ("LC_CTYPE", envstring, 1);
|
|
|
|
char *newloc = setlocale_copy (LC_CTYPE, "");
|
|
|
|
if (newloc == NULL)
|
|
|
|
{
|
|
|
|
printf ("setlocale for \"%s\": %m\n", envstring);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
if (strcmp (newloc, de_locale) == 0)
|
|
|
|
{
|
|
|
|
printf ("setlocale with LC_CTYPE=\"%s\" left locale at \"%s\"\n",
|
|
|
|
envstring, de_locale);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
if (setlocale (LC_CTYPE, de_locale) == NULL)
|
|
|
|
{
|
|
|
|
printf ("restoring locale \"%s\" with LC_CTYPE=\"%s\": %m\n",
|
|
|
|
de_locale, envstring);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
char *newloc2 = setlocale_copy (LC_CTYPE, newloc);
|
|
|
|
if (newloc2 == NULL)
|
|
|
|
{
|
|
|
|
printf ("restoring locale \"%s\" following \"%s\": %m\n",
|
|
|
|
newloc, envstring);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
if (strcmp (newloc, newloc2) != 0)
|
|
|
|
{
|
|
|
|
printf ("representation of locale \"%s\" changed from \"%s\" to \"%s\"",
|
|
|
|
envstring, newloc, newloc2);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
free (newloc);
|
|
|
|
free (newloc2);
|
|
|
|
|
|
|
|
if (setlocale (LC_CTYPE, de_locale) == NULL)
|
|
|
|
{
|
|
|
|
printf ("restoring locale \"%s\" with LC_CTYPE=\"%s\": %m\n",
|
|
|
|
de_locale, envstring);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Checks that a known-good locale still works if LC_ALL contains a
|
|
|
|
value which should be ignored. */
|
|
|
|
static void
|
|
|
|
setlocale_ignore (const char *to_ignore)
|
|
|
|
{
|
|
|
|
const char *fr_locale = "fr_FR.UTF-8";
|
|
|
|
setenv ("LC_CTYPE", fr_locale, 1);
|
|
|
|
char *expected_locale = setlocale_copy (LC_CTYPE, "");
|
|
|
|
if (expected_locale == NULL)
|
|
|
|
{
|
|
|
|
printf ("setlocale with LC_CTYPE=\"%s\" failed: %m\n", fr_locale);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
if (setlocale (LC_CTYPE, de_locale) == NULL)
|
|
|
|
{
|
|
|
|
printf ("failed to restore locale: %m\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
unsetenv ("LC_CTYPE");
|
|
|
|
|
|
|
|
setenv ("LC_ALL", to_ignore, 1);
|
|
|
|
setenv ("LC_CTYPE", fr_locale, 1);
|
|
|
|
const char *actual_locale = setlocale (LC_CTYPE, "");
|
|
|
|
if (actual_locale == NULL)
|
|
|
|
{
|
|
|
|
printf ("setlocale with LC_ALL, LC_CTYPE=\"%s\" failed: %m\n",
|
|
|
|
fr_locale);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
if (strcmp (actual_locale, expected_locale) != 0)
|
|
|
|
{
|
|
|
|
printf ("setlocale under LC_ALL failed: got \"%s\", expected \"%s\"\n",
|
|
|
|
actual_locale, expected_locale);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
unsetenv ("LC_CTYPE");
|
|
|
|
setlocale_success (fr_locale);
|
|
|
|
unsetenv ("LC_ALL");
|
|
|
|
free (expected_locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_test (void)
|
|
|
|
{
|
|
|
|
/* The glibc test harness sets this environment variable
|
|
|
|
uncondionally. */
|
|
|
|
unsetenv ("LC_ALL");
|
|
|
|
|
|
|
|
de_locale = setlocale_copy (LC_CTYPE, "de_DE.UTF-8");
|
|
|
|
if (de_locale == NULL)
|
|
|
|
{
|
|
|
|
printf ("setlocale (LC_CTYPE, \"de_DE.UTF-8\"): %m\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
setlocale_success ("C");
|
|
|
|
setlocale_success ("en_US.UTF-8");
|
|
|
|
setlocale_success ("/en_US.UTF-8");
|
|
|
|
setlocale_success ("//en_US.UTF-8");
|
|
|
|
setlocale_ignore ("");
|
|
|
|
|
|
|
|
setlocale_fail ("does-not-exist");
|
|
|
|
setlocale_fail ("/");
|
|
|
|
setlocale_fail ("/../localedata/en_US.UTF-8");
|
|
|
|
setlocale_fail ("en_US.UTF-8/");
|
|
|
|
setlocale_fail ("en_US.UTF-8/..");
|
|
|
|
setlocale_fail ("en_US.UTF-8/../en_US.UTF-8");
|
|
|
|
setlocale_fail ("../localedata/en_US.UTF-8");
|
|
|
|
{
|
|
|
|
size_t large_length = 1024;
|
|
|
|
char *large_name = malloc (large_length + 1);
|
|
|
|
if (large_name == NULL)
|
|
|
|
{
|
|
|
|
puts ("malloc failure");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
memset (large_name, '/', large_length);
|
|
|
|
const char *suffix = "en_US.UTF-8";
|
|
|
|
strcpy (large_name + large_length - strlen (suffix), suffix);
|
|
|
|
setlocale_fail (large_name);
|
|
|
|
free (large_name);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t huge_length = 64 * 1024 * 1024;
|
|
|
|
char *huge_name = malloc (huge_length + 1);
|
|
|
|
if (huge_name == NULL)
|
|
|
|
{
|
|
|
|
puts ("malloc failure");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
memset (huge_name, 'X', huge_length);
|
|
|
|
huge_name[huge_length] = '\0';
|
|
|
|
/* Construct a composite locale specification. */
|
|
|
|
const char *prefix = "LC_CTYPE=de_DE.UTF-8;LC_TIME=";
|
|
|
|
memcpy (huge_name, prefix, strlen (prefix));
|
|
|
|
setlocale_fail (huge_name);
|
|
|
|
free (huge_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TEST_FUNCTION do_test ()
|
|
|
|
#include "../test-skeleton.c"
|