]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/findlocale.c
locale: Handle loading a missing locale twice (Bug 14247)
[thirdparty/glibc.git] / locale / findlocale.c
CommitLineData
dff8da6b 1/* Copyright (C) 1996-2024 Free Software Foundation, Inc.
e4cf5070 2 This file is part of the GNU C Library.
7a12c6bb 3
e4cf5070 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.
7a12c6bb 8
e4cf5070
UD
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.
7a12c6bb 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/>. */
7a12c6bb 17
e7f21fa6 18#include <assert.h>
4e8f95a0 19#include <errno.h>
7a12c6bb
RM
20#include <locale.h>
21#include <stdlib.h>
22#include <string.h>
3081378b 23#include <unistd.h>
72c74375
UD
24#ifdef _POSIX_MAPPED_FILES
25# include <sys/mman.h>
26#endif
7a12c6bb
RM
27
28#include "localeinfo.h"
e7f21fa6 29#include "../iconv/gconv_charset.h"
dd9423a6 30#include "../iconv/gconv_int.h"
7a12c6bb
RM
31
32
9a411bf5
RM
33#ifdef NL_CURRENT_INDIRECT
34# define DEFINE_CATEGORY(category, category_name, items, a) \
f095bb72 35extern struct __locale_data _nl_C_##category; \
9a411bf5
RM
36weak_extern (_nl_C_##category)
37# include "categories.def"
38# undef DEFINE_CATEGORY
39
40/* Array indexed by category of pointers to _nl_C_CATEGORY slots.
41 Elements are zero for categories whose data is never used. */
f095bb72 42struct __locale_data *const _nl_C[] attribute_hidden =
9a411bf5
RM
43 {
44# define DEFINE_CATEGORY(category, category_name, items, a) \
45 [category] = &_nl_C_##category,
46# include "categories.def"
47# undef DEFINE_CATEGORY
48 };
30c14c31
RM
49#else
50# define _nl_C (_nl_C_locobj.__locales)
51#endif
7a12c6bb
RM
52
53
54/* For each category we keep a list of records for the locale files
55 which are somehow addressed. */
5746ef6f 56struct loaded_l10nfile *_nl_locale_file_list[__LC_LAST];
7a12c6bb 57
90fe682d 58const char _nl_default_locale_path[] attribute_hidden = COMPLOCALEDIR;
cb09a2cd 59
4e8f95a0
FW
60/* Checks if the name is actually present, that is, not NULL and not
61 empty. */
62static inline int
63name_present (const char *name)
64{
65 return name != NULL && name[0] != '\0';
66}
67
68/* Checks that the locale name neither extremely long, nor contains a
69 ".." path component (to prevent directory traversal). */
70static inline int
71valid_locale_name (const char *name)
72{
73 /* Not set. */
74 size_t namelen = strlen (name);
75 /* Name too long. The limit is arbitrary and prevents stack overflow
76 issues later. */
77 if (__glibc_unlikely (namelen > 255))
78 return 0;
79 /* Directory traversal attempt. */
80 static const char slashdot[4] = {'/', '.', '.', '/'};
9975e3d3
JM
81 if (__glibc_unlikely (__memmem (name, namelen,
82 slashdot, sizeof (slashdot)) != NULL))
4e8f95a0
FW
83 return 0;
84 if (namelen == 2 && __glibc_unlikely (name[0] == '.' && name [1] == '.'))
85 return 0;
86 if (namelen >= 3
87 && __glibc_unlikely (((name[0] == '.'
88 && name[1] == '.'
89 && name[2] == '/')
90 || (name[namelen - 3] == '/'
91 && name[namelen - 2] == '.'
92 && name[namelen - 1] == '.'))))
93 return 0;
94 /* If there is a slash in the name, it must start with one. */
95 if (__glibc_unlikely (memchr (name, '/', namelen) != NULL) && name[0] != '/')
96 return 0;
97 return 1;
98}
7a12c6bb 99
f095bb72 100struct __locale_data *
7a12c6bb 101_nl_find_locale (const char *locale_path, size_t locale_path_len,
c84142e8 102 int category, const char **name)
7a12c6bb
RM
103{
104 int mask;
105 /* Name of the locale for this category. */
e7f07af5 106 const char *cloc_name = *name;
7a12c6bb
RM
107 const char *language;
108 const char *modifier;
109 const char *territory;
110 const char *codeset;
111 const char *normalized_codeset;
7a12c6bb
RM
112 struct loaded_l10nfile *locale_file;
113
e7f07af5 114 if (cloc_name[0] == '\0')
7a12c6bb
RM
115 {
116 /* The user decides which locale to use by setting environment
117 variables. */
e7f07af5
AO
118 cloc_name = getenv ("LC_ALL");
119 if (!name_present (cloc_name))
de18a706 120 cloc_name = getenv (_nl_category_names_get (category));
e7f07af5
AO
121 if (!name_present (cloc_name))
122 cloc_name = getenv ("LANG");
123 if (!name_present (cloc_name))
124 cloc_name = _nl_C_name;
7a12c6bb
RM
125 }
126
4e8f95a0
FW
127 /* We used to fall back to the C locale if the name contains a slash
128 character '/', but we now check for directory traversal in
129 valid_locale_name, so this is no longer necessary. */
63336471 130
e7f07af5
AO
131 if (__builtin_expect (strcmp (cloc_name, _nl_C_name), 1) == 0
132 || __builtin_expect (strcmp (cloc_name, _nl_POSIX_name), 1) == 0)
7a12c6bb
RM
133 {
134 /* We need not load anything. The needed data is contained in
135 the library itself. */
e7f07af5 136 *name = _nl_C_name;
7a12c6bb
RM
137 return _nl_C[category];
138 }
e7f07af5 139 else if (!valid_locale_name (cloc_name))
4e8f95a0
FW
140 {
141 __set_errno (EINVAL);
142 return NULL;
143 }
144
e7f07af5 145 *name = cloc_name;
7a12c6bb 146
cb09a2cd
RM
147 /* We really have to load some data. First we try the archive,
148 but only if there was no LOCPATH environment variable specified. */
a1ffb40e 149 if (__glibc_likely (locale_path == NULL))
cb09a2cd 150 {
f095bb72
UD
151 struct __locale_data *data
152 = _nl_load_locale_from_archive (category, name);
a1ffb40e 153 if (__glibc_likely (data != NULL))
cb09a2cd
RM
154 return data;
155
0d822a01
AO
156 /* Nothing in the archive with the given name. Expanding it as
157 an alias and retry. */
e7f07af5
AO
158 cloc_name = _nl_expand_alias (*name);
159 if (cloc_name != NULL)
0d822a01 160 {
e7f07af5 161 data = _nl_load_locale_from_archive (category, &cloc_name);
0d822a01
AO
162 if (__builtin_expect (data != NULL, 1))
163 return data;
164 }
165
cb09a2cd
RM
166 /* Nothing in the archive. Set the default path to search below. */
167 locale_path = _nl_default_locale_path;
168 locale_path_len = sizeof _nl_default_locale_path;
169 }
0d822a01
AO
170 else
171 /* We really have to load some data. First see whether the name is
172 an alias. Please note that this makes it impossible to have "C"
173 or "POSIX" as aliases. */
e7f07af5 174 cloc_name = _nl_expand_alias (*name);
cb09a2cd 175
e7f07af5 176 if (cloc_name == NULL)
7a12c6bb 177 /* It is no alias. */
e7f07af5 178 cloc_name = *name;
7a12c6bb
RM
179
180 /* Make a writable copy of the locale name. */
e7f07af5 181 char *loc_name = strdupa (cloc_name);
7a12c6bb
RM
182
183 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
184
185 language[_territory[.codeset]][@modifier]
186
7a12c6bb
RM
187 Beside the first all of them are allowed to be missing. If the
188 full specified locale is not found, the less specific one are
cb09a2cd 189 looked for. The various part will be stripped off according to
7a12c6bb 190 the following order:
e155c801
UD
191 (1) codeset
192 (2) normalized codeset
193 (3) territory
194 (4) modifier
7a12c6bb
RM
195 */
196 mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
e155c801 197 &codeset, &normalized_codeset);
4f031072
UD
198 if (mask == -1)
199 /* Memory allocate problem. */
200 return NULL;
7a12c6bb
RM
201
202 /* If exactly this locale was already asked for we have an entry with
203 the complete name. */
5746ef6f 204 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
7a12c6bb
RM
205 locale_path, locale_path_len, mask,
206 language, territory, codeset,
e155c801 207 normalized_codeset, modifier,
de18a706 208 _nl_category_names_get (category), 0);
7a12c6bb
RM
209
210 if (locale_file == NULL)
211 {
212 /* Find status record for addressed locale file. We have to search
213 through all directories in the locale path. */
5746ef6f 214 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
7a12c6bb
RM
215 locale_path, locale_path_len, mask,
216 language, territory, codeset,
e155c801 217 normalized_codeset, modifier,
de18a706 218 _nl_category_names_get (category), 1);
7a12c6bb
RM
219 if (locale_file == NULL)
220 /* This means we are out of core. */
221 return NULL;
222 }
7a12c6bb 223
727211c4
UD
224 /* The space for normalized_codeset is dynamically allocated. Free it. */
225 if (mask & XPG_NORM_CODESET)
226 free ((void *) normalized_codeset);
227
7a12c6bb
RM
228 if (locale_file->decided == 0)
229 _nl_load_locale (locale_file, category);
230
231 if (locale_file->data == NULL)
232 {
233 int cnt;
234 for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
235 {
236 if (locale_file->successor[cnt]->decided == 0)
237 _nl_load_locale (locale_file->successor[cnt], category);
238 if (locale_file->successor[cnt]->data != NULL)
239 break;
240 }
241 /* Move the entry we found (or NULL) to the first place of
242 successors. */
243 locale_file->successor[0] = locale_file->successor[cnt];
244 locale_file = locale_file->successor[cnt];
7a12c6bb 245
3a31f6f4 246 if (locale_file == NULL)
684fbab7
CD
247 {
248 /* If this is the second time we tried to load a failed
249 locale then the locale_file value comes from the cache
250 and we will not carry out any actual filesystem
251 operations so we must set ENOENT here. */
252 __set_errno (ENOENT);
253 return NULL;
254 }
3a31f6f4 255 }
7a12c6bb 256
e7f21fa6
UD
257 /* The LC_CTYPE category allows to check whether a locale is really
258 usable. If the locale name contains a charset name and the
259 charset name used in the locale (present in the LC_CTYPE data) is
260 not the same (after resolving aliases etc) we reject the locale
261 since using it would irritate users expecting the charset named
262 in the locale name. */
263 if (codeset != NULL)
264 {
265 /* Get the codeset information from the locale file. */
266 static const int codeset_idx[] =
267 {
268 [__LC_CTYPE] = _NL_ITEM_INDEX (CODESET),
269 [__LC_NUMERIC] = _NL_ITEM_INDEX (_NL_NUMERIC_CODESET),
270 [__LC_TIME] = _NL_ITEM_INDEX (_NL_TIME_CODESET),
271 [__LC_COLLATE] = _NL_ITEM_INDEX (_NL_COLLATE_CODESET),
272 [__LC_MONETARY] = _NL_ITEM_INDEX (_NL_MONETARY_CODESET),
273 [__LC_MESSAGES] = _NL_ITEM_INDEX (_NL_MESSAGES_CODESET),
274 [__LC_PAPER] = _NL_ITEM_INDEX (_NL_PAPER_CODESET),
275 [__LC_NAME] = _NL_ITEM_INDEX (_NL_NAME_CODESET),
276 [__LC_ADDRESS] = _NL_ITEM_INDEX (_NL_ADDRESS_CODESET),
277 [__LC_TELEPHONE] = _NL_ITEM_INDEX (_NL_TELEPHONE_CODESET),
278 [__LC_MEASUREMENT] = _NL_ITEM_INDEX (_NL_MEASUREMENT_CODESET),
279 [__LC_IDENTIFICATION] = _NL_ITEM_INDEX (_NL_IDENTIFICATION_CODESET)
280 };
f095bb72 281 const struct __locale_data *data;
e7f21fa6
UD
282 const char *locale_codeset;
283 char *clocale_codeset;
284 char *ccodeset;
285
f095bb72 286 data = (const struct __locale_data *) locale_file->data;
e7f21fa6
UD
287 locale_codeset =
288 (const char *) data->values[codeset_idx[category]].string;
289 assert (locale_codeset != NULL);
290 /* Note the length of the allocated memory: +3 for up to two slashes
291 and the NUL byte. */
292 clocale_codeset = (char *) alloca (strlen (locale_codeset) + 3);
293 strip (clocale_codeset, locale_codeset);
294
295 ccodeset = (char *) alloca (strlen (codeset) + 3);
296 strip (ccodeset, codeset);
297
9a018f6c
UD
298 if (__gconv_compare_alias (upstr (ccodeset, ccodeset),
299 upstr (clocale_codeset,
300 clocale_codeset)) != 0)
684fbab7
CD
301 {
302 /* The codesets are not identical, don't use the locale.
303 If this is the second time we tried to load a locale
304 whose codeset doesn't match then the result came from
305 the cache and must set ENOENT here. */
306 __set_errno (ENOENT);
307 return NULL;
308 }
e7f21fa6
UD
309 }
310
7a12c6bb
RM
311 /* Determine the locale name for which loading succeeded. This
312 information comes from the file name. The form is
036cc82f 313 <path>/<locale>/LC_foo. We must extract the <locale> part. */
f095bb72 314 if (((const struct __locale_data *) locale_file->data)->name == NULL)
7a12c6bb 315 {
036cc82f 316 char *cp, *endp;
7a12c6bb
RM
317
318 endp = strrchr (locale_file->filename, '/');
319 cp = endp - 1;
320 while (cp[-1] != '/')
321 --cp;
f095bb72
UD
322 ((struct __locale_data *) locale_file->data)->name
323 = __strndup (cp, endp - cp);
7a12c6bb 324 }
7a12c6bb 325
323fb88d 326 /* Determine whether the user wants transliteration or not. */
5f078c32
UD
327 if (modifier != NULL
328 && __strcasecmp_l (modifier, "TRANSLIT", _nl_C_locobj_ptr) == 0)
f095bb72 329 ((struct __locale_data *) locale_file->data)->use_translit = 1;
323fb88d 330
c84142e8 331 /* Increment the usage count. */
f095bb72 332 if (((const struct __locale_data *) locale_file->data)->usage_count
a5a0310d 333 < MAX_USAGE_COUNT)
f095bb72 334 ++((struct __locale_data *) locale_file->data)->usage_count;
c84142e8 335
f095bb72 336 return (struct __locale_data *) locale_file->data;
7a12c6bb 337}
c84142e8
UD
338
339
340/* Calling this function assumes the lock for handling global locale data
341 is acquired. */
342void
f095bb72 343_nl_remove_locale (int locale, struct __locale_data *data)
c84142e8
UD
344{
345 if (--data->usage_count == 0)
346 {
0f283ffc 347 if (data->alloc != ld_archive)
c84142e8 348 {
0f283ffc
RM
349 /* First search the entry in the list of loaded files. */
350 struct loaded_l10nfile *ptr = _nl_locale_file_list[locale];
351
352 /* Search for the entry. It must be in the list. Otherwise it
353 is a bug and we crash badly. */
f095bb72 354 while ((struct __locale_data *) ptr->data != data)
0f283ffc
RM
355 ptr = ptr->next;
356
357 /* Mark the data as not available anymore. So when the data has
358 to be used again it is reloaded. */
359 ptr->decided = 0;
360 ptr->data = NULL;
c84142e8 361 }
c84142e8 362
0f283ffc 363 /* This does the real work. */
bbebe83a 364 _nl_unload_locale (locale, data);
c84142e8
UD
365 }
366}