projects/guix·blob javascript-team · refs

231 lines · 7.4 KiB

1The format of locale data can be incompatible between libc versions, and
2loading incompatible data can lead to 'setlocale' returning EINVAL at best
3or triggering an assertion failure at worst. See
4https://lists.gnu.org/archive/html/guix-devel/2015-09/msg00717.html
5for background information.
6
7To address that, this patch changes libc to honor a new 'GUIX_LOCPATH'
8variable, and to look for locale data in version-specific sub-directories of
9that variable. So, if GUIX_LOCPATH=/foo:/bar, locale data is searched for in
10/foo/X.Y and /bar/X.Y, where X.Y is the libc version number.
11
12That way, a single 'GUIX_LOCPATH' setting can work even if different libc
13versions coexist on the system.
14
15--- a/locale/newlocale.c
16+++ b/locale/newlocale.c
17@@ -30,6 +30,7 @@
18 /* Lock for protecting global data. */
19 __libc_rwlock_define (extern , __libc_setlocale_lock attribute_hidden)
20
21+extern error_t compute_locale_search_path (char **, size_t *);
22
23 /* Use this when we come along an error. */
24 #define ERROR_RETURN \
25@@ -48,7 +49,6 @@ __newlocale (int category_mask, const char *locale, __locale_t base)
26 __locale_t result_ptr;
27 char *locale_path;
28 size_t locale_path_len;
29- const char *locpath_var;
30 int cnt;
31 size_t names_len;
32
33@@ -102,17 +102,8 @@ __newlocale (int category_mask, const char *locale, __locale_t base)
34 locale_path = NULL;
35 locale_path_len = 0;
36
37- locpath_var = getenv ("LOCPATH");
38- if (locpath_var != NULL && locpath_var[0] != '\0')
39- {
40- if (__argz_create_sep (locpath_var, ':',
41- &locale_path, &locale_path_len) != 0)
42- return NULL;
43-
44- if (__argz_add_sep (&locale_path, &locale_path_len,
45- _nl_default_locale_path, ':') != 0)
46- return NULL;
47- }
48+ if (compute_locale_search_path (&locale_path, &locale_path_len) != 0)
49+ return NULL;
50
51 /* Get the names for the locales we are interested in. We either
52 allow a composite name or a single name. */
53diff --git a/locale/setlocale.c b/locale/setlocale.c
54index ead030d..0c0e314 100644
55--- a/locale/setlocale.c
56+++ b/locale/setlocale.c
57@@ -215,12 +215,54 @@ setdata (int category, struct __locale_data *data)
58 }
59 }
60
61+/* Return in *LOCALE_PATH and *LOCALE_PATH_LEN the locale data search path as
62+ an argz list. Return ENOMEN on error, zero otherwise. */
63+error_t
64+compute_locale_search_path (char **locale_path, size_t *locale_path_len)
65+{
66+ char* guix_locpath_var = getenv ("GUIX_LOCPATH");
67+ char *locpath_var = getenv ("LOCPATH");
68+
69+ if (guix_locpath_var != NULL && guix_locpath_var[0] != '\0')
70+ {
71+ /* Entries in 'GUIX_LOCPATH' take precedence over 'LOCPATH'. These
72+ entries are systematically prefixed with "/X.Y" where "X.Y" is the
73+ libc version. */
74+ if (__argz_add_sep (locale_path, locale_path_len,
75+ guix_locpath_var, ':') != 0
76+ || __argz_suffix_entries (locale_path, locale_path_len,
77+ "/" VERSION) != 0)
78+ goto bail_out;
79+ }
80+
81+ if (locpath_var != NULL && locpath_var[0] != '\0')
82+ {
83+ if (__argz_add_sep (locale_path, locale_path_len,
84+ locpath_var, ':') != 0)
85+ goto bail_out;
86+
87+ }
88+
89+ /* Append the system default locale directory. */
90+ if (__argz_add_sep (locale_path, locale_path_len,
91+ _nl_default_locale_path, ':') != 0)
92+ goto bail_out;
93+
94+ return 0;
95+
96+ bail_out:
97+ free (*locale_path);
98+ *locale_path = NULL;
99+ *locale_path_len = 0;
100+
101+ return ENOMEM;
102+}
103+
104 char *
105 setlocale (int category, const char *locale)
106 {
107 char *locale_path;
108 size_t locale_path_len;
109- const char *locpath_var;
110 char *composite;
111
112 /* Sanity check for CATEGORY argument. */
113@@ -251,17 +304,10 @@ setlocale (int category, const char *locale)
114 locale_path = NULL;
115 locale_path_len = 0;
116
117- locpath_var = getenv ("LOCPATH");
118- if (locpath_var != NULL && locpath_var[0] != '\0')
119+ if (compute_locale_search_path (&locale_path, &locale_path_len) != 0)
120 {
121- if (__argz_create_sep (locpath_var, ':',
122- &locale_path, &locale_path_len) != 0
123- || __argz_add_sep (&locale_path, &locale_path_len,
124- _nl_default_locale_path, ':') != 0)
125- {
126- __libc_rwlock_unlock (__libc_setlocale_lock);
127- return NULL;
128- }
129+ __libc_rwlock_unlock (__libc_setlocale_lock);
130+ return NULL;
131 }
132
133 if (category == LC_ALL)
134diff --git a/string/Makefile b/string/Makefile
135index 8424a61..f925503 100644
136--- a/string/Makefile
137+++ b/string/Makefile
138@@ -38,7 +38,7 @@ routines := strcat strchr strcmp strcoll strcpy strcspn \
139 swab strfry memfrob memmem rawmemchr strchrnul \
140 $(addprefix argz-,append count create ctsep next \
141 delete extract insert stringify \
142- addsep replace) \
143+ addsep replace suffix) \
144 envz basename \
145 strcoll_l strxfrm_l string-inlines memrchr \
146 xpg-strerror strerror_l
147diff --git a/string/argz-suffix.c b/string/argz-suffix.c
148new file mode 100644
149index 0000000..505b0f2
150--- /dev/null
151+++ b/string/argz-suffix.c
152@@ -0,0 +1,58 @@
153+/* Copyright (C) 2015 Free Software Foundation, Inc.
154+ This file is part of the GNU C Library.
155+ Contributed by Ludovic Courtès <[email protected]>.
156+
157+ The GNU C Library is free software; you can redistribute it and/or
158+ modify it under the terms of the GNU Lesser General Public
159+ License as published by the Free Software Foundation; either
160+ version 2.1 of the License, or (at your option) any later version.
161+
162+ The GNU C Library is distributed in the hope that it will be useful,
163+ but WITHOUT ANY WARRANTY; without even the implied warranty of
164+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
165+ Lesser General Public License for more details.
166+
167+ You should have received a copy of the GNU Lesser General Public
168+ License along with the GNU C Library; if not, see
169+ <http://www.gnu.org/licenses/>. */
170+
171+#include <argz.h>
172+#include <errno.h>
173+#include <stdlib.h>
174+#include <string.h>
175+
176+
177+error_t
178+__argz_suffix_entries (char **argz, size_t *argz_len, const char *suffix)
179+
180+{
181+ size_t suffix_len = strlen (suffix);
182+ size_t count = __argz_count (*argz, *argz_len);
183+ size_t new_argz_len = *argz_len + count * suffix_len;
184+ if (new_argz_len == 0)
185+ return 0;
186+ char *new_argz = malloc (new_argz_len);
187+
188+ if (new_argz)
189+ {
190+ char *p = new_argz, *entry;
191+
192+ for (entry = *argz;
193+ entry != NULL;
194+ entry = argz_next (*argz, *argz_len, entry))
195+ {
196+ p = stpcpy (p, entry);
197+ p = stpcpy (p, suffix);
198+ p++;
199+ }
200+
201+ free (*argz);
202+ *argz = new_argz;
203+ *argz_len = new_argz_len;
204+
205+ return 0;
206+ }
207+ else
208+ return ENOMEM;
209+}
210+weak_alias (__argz_suffix_entries, argz_suffix_entries)
211diff --git a/string/argz.h b/string/argz.h
212index bb62a31..d276a35 100644
213--- a/string/argz.h
214+++ b/string/argz.h
215@@ -134,6 +134,16 @@ extern error_t argz_replace (char **__restrict __argz,
216 const char *__restrict __str,
217 const char *__restrict __with,
218 unsigned int *__restrict __replace_count);
219+
220+/* Suffix each entry of ARGZ & ARGZ_LEN with SUFFIX. Return 0 on success,
221+ and ENOMEN if memory cannot be allocated. */
222+extern error_t __argz_suffix_entries (char **__restrict __argz,
223+ size_t *__restrict __argz_len,
224+ const char *__restrict __suffix);
225+extern error_t argz_suffix_entries (char **__restrict __argz,
226+ size_t *__restrict __argz_len,
227+ const char *__restrict __suffix);
228+
229
230 /* Returns the next entry in ARGZ & ARGZ_LEN after ENTRY, or NULL if there
231 are no more. If entry is NULL, then the first entry is returned. This