1 /* Regression test for setlocale invalid environment variable handling.
2 Copyright (C) 2014-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
24 /* The result of setlocale may be overwritten by subsequent calls, so
25 this wrapper makes a copy. */
27 setlocale_copy (int category
, const char *locale
)
29 const char *result
= setlocale (category
, locale
);
32 return strdup (result
);
35 static char *de_locale
;
38 setlocale_fail (const char *envstring
)
40 setenv ("LC_CTYPE", envstring
, 1);
41 if (setlocale (LC_CTYPE
, "") != NULL
)
43 printf ("unexpected setlocale success for \"%s\" locale\n", envstring
);
46 const char *newloc
= setlocale (LC_CTYPE
, NULL
);
47 if (strcmp (newloc
, de_locale
) != 0)
49 printf ("failed setlocale call \"%s\" changed locale to \"%s\"\n",
56 setlocale_success (const char *envstring
)
58 setenv ("LC_CTYPE", envstring
, 1);
59 char *newloc
= setlocale_copy (LC_CTYPE
, "");
62 printf ("setlocale for \"%s\": %m\n", envstring
);
65 if (strcmp (newloc
, de_locale
) == 0)
67 printf ("setlocale with LC_CTYPE=\"%s\" left locale at \"%s\"\n",
68 envstring
, de_locale
);
71 if (setlocale (LC_CTYPE
, de_locale
) == NULL
)
73 printf ("restoring locale \"%s\" with LC_CTYPE=\"%s\": %m\n",
74 de_locale
, envstring
);
77 char *newloc2
= setlocale_copy (LC_CTYPE
, newloc
);
80 printf ("restoring locale \"%s\" following \"%s\": %m\n",
84 if (strcmp (newloc
, newloc2
) != 0)
86 printf ("representation of locale \"%s\" changed from \"%s\" to \"%s\"",
87 envstring
, newloc
, newloc2
);
93 if (setlocale (LC_CTYPE
, de_locale
) == NULL
)
95 printf ("restoring locale \"%s\" with LC_CTYPE=\"%s\": %m\n",
96 de_locale
, envstring
);
101 /* Checks that a known-good locale still works if LC_ALL contains a
102 value which should be ignored. */
104 setlocale_ignore (const char *to_ignore
)
106 const char *fr_locale
= "fr_FR.UTF-8";
107 setenv ("LC_CTYPE", fr_locale
, 1);
108 char *expected_locale
= setlocale_copy (LC_CTYPE
, "");
109 if (expected_locale
== NULL
)
111 printf ("setlocale with LC_CTYPE=\"%s\" failed: %m\n", fr_locale
);
114 if (setlocale (LC_CTYPE
, de_locale
) == NULL
)
116 printf ("failed to restore locale: %m\n");
119 unsetenv ("LC_CTYPE");
121 setenv ("LC_ALL", to_ignore
, 1);
122 setenv ("LC_CTYPE", fr_locale
, 1);
123 const char *actual_locale
= setlocale (LC_CTYPE
, "");
124 if (actual_locale
== NULL
)
126 printf ("setlocale with LC_ALL, LC_CTYPE=\"%s\" failed: %m\n",
130 if (strcmp (actual_locale
, expected_locale
) != 0)
132 printf ("setlocale under LC_ALL failed: got \"%s\", expected \"%s\"\n",
133 actual_locale
, expected_locale
);
136 unsetenv ("LC_CTYPE");
137 setlocale_success (fr_locale
);
139 free (expected_locale
);
145 /* The glibc test harness sets this environment variable
149 de_locale
= setlocale_copy (LC_CTYPE
, "de_DE.UTF-8");
150 if (de_locale
== NULL
)
152 printf ("setlocale (LC_CTYPE, \"de_DE.UTF-8\"): %m\n");
155 setlocale_success ("C");
156 setlocale_success ("en_US.UTF-8");
157 setlocale_success ("/en_US.UTF-8");
158 setlocale_success ("//en_US.UTF-8");
159 setlocale_ignore ("");
161 setlocale_fail ("does-not-exist");
162 setlocale_fail ("/");
163 setlocale_fail ("/../localedata/en_US.UTF-8");
164 setlocale_fail ("en_US.UTF-8/");
165 setlocale_fail ("en_US.UTF-8/..");
166 setlocale_fail ("en_US.UTF-8/../en_US.UTF-8");
167 setlocale_fail ("../localedata/en_US.UTF-8");
169 size_t large_length
= 1024;
170 char *large_name
= malloc (large_length
+ 1);
171 if (large_name
== NULL
)
173 puts ("malloc failure");
176 memset (large_name
, '/', large_length
);
177 const char *suffix
= "en_US.UTF-8";
178 strcpy (large_name
+ large_length
- strlen (suffix
), suffix
);
179 setlocale_fail (large_name
);
183 size_t huge_length
= 64 * 1024 * 1024;
184 char *huge_name
= malloc (huge_length
+ 1);
185 if (huge_name
== NULL
)
187 puts ("malloc failure");
190 memset (huge_name
, 'X', huge_length
);
191 huge_name
[huge_length
] = '\0';
192 /* Construct a composite locale specification. */
193 const char *prefix
= "LC_CTYPE=de_DE.UTF-8;LC_TIME=";
194 memcpy (huge_name
, prefix
, strlen (prefix
));
195 setlocale_fail (huge_name
);
202 #define TEST_FUNCTION do_test ()
203 #include "../test-skeleton.c"