Further harden glibc malloc metadata against 1-byte overflows.
[glibc.git] / locale / programs / ld-identification.c
blob3e3ea649d74efecb161e5c0c428bf7124298da07
1 /* Copyright (C) 1998-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <error.h>
23 #include <langinfo.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdint.h>
27 #include <sys/uio.h>
29 #include <assert.h>
31 #include "localedef.h"
32 #include "localeinfo.h"
33 #include "locfile.h"
36 /* The real definition of the struct for the LC_IDENTIFICATION locale. */
37 struct locale_identification_t
39 const char *title;
40 const char *source;
41 const char *address;
42 const char *contact;
43 const char *email;
44 const char *tel;
45 const char *fax;
46 const char *language;
47 const char *territory;
48 const char *audience;
49 const char *application;
50 const char *abbreviation;
51 const char *revision;
52 const char *date;
53 const char *category[__LC_LAST];
57 static const char *category_name[__LC_LAST] =
59 [LC_CTYPE] = "LC_CTYPE",
60 [LC_NUMERIC] = "LC_NUMERIC",
61 [LC_TIME] = "LC_TIME",
62 [LC_COLLATE] = "LC_COLLATE",
63 [LC_MONETARY] = "LC_MONETARY",
64 [LC_MESSAGES] = "LC_MESSAGES",
65 [LC_ALL] = "LC_ALL",
66 [LC_PAPER] = "LC_PAPER",
67 [LC_NAME] = "LC_NAME",
68 [LC_ADDRESS] = "LC_ADDRESS",
69 [LC_TELEPHONE] = "LC_TELEPHONE",
70 [LC_MEASUREMENT] = "LC_MEASUREMENT",
71 [LC_IDENTIFICATION] = "LC_IDENTIFICATION"
75 static void
76 identification_startup (struct linereader *lr, struct localedef_t *locale,
77 int ignore_content)
79 if (!ignore_content)
81 locale->categories[LC_IDENTIFICATION].identification =
82 (struct locale_identification_t *)
83 xcalloc (1, sizeof (struct locale_identification_t));
85 locale->categories[LC_IDENTIFICATION].identification->category[LC_ALL] =
86 "";
89 if (lr != NULL)
91 lr->translate_strings = 1;
92 lr->return_widestr = 0;
97 void
98 identification_finish (struct localedef_t *locale,
99 const struct charmap_t *charmap)
101 struct locale_identification_t *identification
102 = locale->categories[LC_IDENTIFICATION].identification;
103 int nothing = 0;
104 size_t num;
106 /* Now resolve copying and also handle completely missing definitions. */
107 if (identification == NULL)
109 /* First see whether we were supposed to copy. If yes, find the
110 actual definition. */
111 if (locale->copy_name[LC_IDENTIFICATION] != NULL)
113 /* Find the copying locale. This has to happen transitively since
114 the locale we are copying from might also copying another one. */
115 struct localedef_t *from = locale;
118 from = find_locale (LC_IDENTIFICATION,
119 from->copy_name[LC_IDENTIFICATION],
120 from->repertoire_name, charmap);
121 while (from->categories[LC_IDENTIFICATION].identification == NULL
122 && from->copy_name[LC_IDENTIFICATION] != NULL);
124 identification = locale->categories[LC_IDENTIFICATION].identification
125 = from->categories[LC_IDENTIFICATION].identification;
128 /* If there is still no definition issue an warning and create an
129 empty one. */
130 if (identification == NULL)
132 if (! be_quiet)
133 WITH_CUR_LOCALE (error (0, 0, _("\
134 No definition for %s category found"), "LC_IDENTIFICATION"));
135 identification_startup (NULL, locale, 0);
136 identification
137 = locale->categories[LC_IDENTIFICATION].identification;
138 nothing = 1;
142 #define TEST_ELEM(cat) \
143 if (identification->cat == NULL) \
145 if (verbose && ! nothing) \
146 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
147 "LC_IDENTIFICATION", #cat)); \
148 identification->cat = ""; \
151 TEST_ELEM (title);
152 TEST_ELEM (source);
153 TEST_ELEM (address);
154 TEST_ELEM (contact);
155 TEST_ELEM (email);
156 TEST_ELEM (tel);
157 TEST_ELEM (fax);
158 TEST_ELEM (language);
159 TEST_ELEM (territory);
160 TEST_ELEM (audience);
161 TEST_ELEM (application);
162 TEST_ELEM (abbreviation);
163 TEST_ELEM (revision);
164 TEST_ELEM (date);
166 for (num = 0; num < __LC_LAST; ++num)
168 /* We don't accept/parse this category, so skip it early. */
169 if (num == LC_ALL)
170 continue;
172 if (identification->category[num] == NULL)
174 if (verbose && ! nothing)
175 WITH_CUR_LOCALE (error (0, 0, _("\
176 %s: no identification for category `%s'"),
177 "LC_IDENTIFICATION", category_name[num]));
178 identification->category[num] = "";
180 else
182 /* Only list the standards we care about. This is based on the
183 ISO 30112 WD10 [2014] standard which supersedes all previous
184 revisions of the ISO 14652 standard. */
185 static const char * const standards[] =
187 "posix:1993",
188 "i18n:2004",
189 "i18n:2012",
191 size_t i;
192 bool matched = false;
194 for (i = 0; i < sizeof (standards) / sizeof (standards[0]); ++i)
195 if (strcmp (identification->category[num], standards[i]) == 0)
196 matched = true;
198 if (matched != true)
199 WITH_CUR_LOCALE (error (0, 0, _("\
200 %s: unknown standard `%s' for category `%s'"),
201 "LC_IDENTIFICATION",
202 identification->category[num],
203 category_name[num]));
209 void
210 identification_output (struct localedef_t *locale,
211 const struct charmap_t *charmap,
212 const char *output_path)
214 struct locale_identification_t *identification
215 = locale->categories[LC_IDENTIFICATION].identification;
216 struct locale_file file;
217 size_t num;
219 init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION));
220 add_locale_string (&file, identification->title);
221 add_locale_string (&file, identification->source);
222 add_locale_string (&file, identification->address);
223 add_locale_string (&file, identification->contact);
224 add_locale_string (&file, identification->email);
225 add_locale_string (&file, identification->tel);
226 add_locale_string (&file, identification->fax);
227 add_locale_string (&file, identification->language);
228 add_locale_string (&file, identification->territory);
229 add_locale_string (&file, identification->audience);
230 add_locale_string (&file, identification->application);
231 add_locale_string (&file, identification->abbreviation);
232 add_locale_string (&file, identification->revision);
233 add_locale_string (&file, identification->date);
234 start_locale_structure (&file);
235 for (num = 0; num < __LC_LAST; ++num)
236 if (num != LC_ALL)
237 add_locale_string (&file, identification->category[num]);
238 end_locale_structure (&file);
239 add_locale_string (&file, charmap->code_set_name);
240 write_locale_data (output_path, LC_IDENTIFICATION, "LC_IDENTIFICATION",
241 &file);
245 /* The parser for the LC_IDENTIFICATION section of the locale definition. */
246 void
247 identification_read (struct linereader *ldfile, struct localedef_t *result,
248 const struct charmap_t *charmap, const char *repertoire_name,
249 int ignore_content)
251 struct locale_identification_t *identification;
252 struct token *now;
253 struct token *arg;
254 struct token *cattok;
255 int category;
256 enum token_t nowtok;
258 /* The rest of the line containing `LC_IDENTIFICATION' must be free. */
259 lr_ignore_rest (ldfile, 1);
263 now = lr_token (ldfile, charmap, result, NULL, verbose);
264 nowtok = now->tok;
266 while (nowtok == tok_eol);
268 /* If we see `copy' now we are almost done. */
269 if (nowtok == tok_copy)
271 handle_copy (ldfile, charmap, repertoire_name, result,
272 tok_lc_identification, LC_IDENTIFICATION,
273 "LC_IDENTIFICATION", ignore_content);
274 return;
277 /* Prepare the data structures. */
278 identification_startup (ldfile, result, ignore_content);
279 identification = result->categories[LC_IDENTIFICATION].identification;
281 while (1)
283 /* Of course we don't proceed beyond the end of file. */
284 if (nowtok == tok_eof)
285 break;
287 /* Ignore empty lines. */
288 if (nowtok == tok_eol)
290 now = lr_token (ldfile, charmap, result, NULL, verbose);
291 nowtok = now->tok;
292 continue;
295 switch (nowtok)
297 #define STR_ELEM(cat) \
298 case tok_##cat: \
299 /* Ignore the rest of the line if we don't need the input of \
300 this line. */ \
301 if (ignore_content) \
303 lr_ignore_rest (ldfile, 0); \
304 break; \
307 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
308 if (arg->tok != tok_string) \
309 goto err_label; \
310 if (identification->cat != NULL) \
311 lr_error (ldfile, _("\
312 %s: field `%s' declared more than once"), "LC_IDENTIFICATION", #cat); \
313 else if (!ignore_content && arg->val.str.startmb == NULL) \
315 lr_error (ldfile, _("\
316 %s: unknown character in field `%s'"), "LC_IDENTIFICATION", #cat); \
317 identification->cat = ""; \
319 else if (!ignore_content) \
320 identification->cat = arg->val.str.startmb; \
321 break
323 STR_ELEM (title);
324 STR_ELEM (source);
325 STR_ELEM (address);
326 STR_ELEM (contact);
327 STR_ELEM (email);
328 STR_ELEM (tel);
329 STR_ELEM (fax);
330 STR_ELEM (language);
331 STR_ELEM (territory);
332 STR_ELEM (audience);
333 STR_ELEM (application);
334 STR_ELEM (abbreviation);
335 STR_ELEM (revision);
336 STR_ELEM (date);
338 case tok_category:
339 /* Ignore the rest of the line if we don't need the input of
340 this line. */
341 if (ignore_content)
343 lr_ignore_rest (ldfile, 0);
344 break;
347 /* We expect two operands. */
348 arg = lr_token (ldfile, charmap, result, NULL, verbose);
349 if (arg->tok != tok_string && arg->tok != tok_ident)
350 goto err_label;
351 /* Next is a semicolon. */
352 cattok = lr_token (ldfile, charmap, result, NULL, verbose);
353 if (cattok->tok != tok_semicolon)
354 goto err_label;
355 /* Now a LC_xxx identifier. */
356 cattok = lr_token (ldfile, charmap, result, NULL, verbose);
357 switch (cattok->tok)
359 #define CATEGORY(lname, uname) \
360 case tok_lc_##lname: \
361 category = LC_##uname; \
362 break
364 CATEGORY (identification, IDENTIFICATION);
365 CATEGORY (ctype, CTYPE);
366 CATEGORY (collate, COLLATE);
367 CATEGORY (time, TIME);
368 CATEGORY (numeric, NUMERIC);
369 CATEGORY (monetary, MONETARY);
370 CATEGORY (messages, MESSAGES);
371 CATEGORY (paper, PAPER);
372 CATEGORY (name, NAME);
373 CATEGORY (address, ADDRESS);
374 CATEGORY (telephone, TELEPHONE);
375 CATEGORY (measurement, MEASUREMENT);
377 default:
378 goto err_label;
380 if (identification->category[category] != NULL)
382 lr_error (ldfile, _("\
383 %s: duplicate category version definition"), "LC_IDENTIFICATION");
384 free (arg->val.str.startmb);
386 else
387 identification->category[category] = arg->val.str.startmb;
388 break;
390 case tok_end:
391 /* Next we assume `LC_IDENTIFICATION'. */
392 arg = lr_token (ldfile, charmap, result, NULL, verbose);
393 if (arg->tok == tok_eof)
394 break;
395 if (arg->tok == tok_eol)
396 lr_error (ldfile, _("%s: incomplete `END' line"),
397 "LC_IDENTIFICATION");
398 else if (arg->tok != tok_lc_identification)
399 lr_error (ldfile, _("\
400 %1$s: definition does not end with `END %1$s'"), "LC_IDENTIFICATION");
401 lr_ignore_rest (ldfile, arg->tok == tok_lc_identification);
402 return;
404 default:
405 err_label:
406 SYNTAX_ERROR (_("%s: syntax error"), "LC_IDENTIFICATION");
409 /* Prepare for the next round. */
410 now = lr_token (ldfile, charmap, result, NULL, verbose);
411 nowtok = now->tok;
414 /* When we come here we reached the end of the file. */
415 lr_error (ldfile, _("%s: premature end of file"), "LC_IDENTIFICATION");