From 74e704006a0004058fc38806a19c1552b1e2463d Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sun, 21 Oct 2018 01:09:20 -0400 Subject: simplify newlocale and allow failure for explicit locale names unify the code paths for allocated and non-allocated locale objects, always using a tmp object. this is necessary to avoid clobbering the base locale object too soon if we allow for the possibility that looking up an explicitly requested locale name may fail, and makes the code simpler and cleaner anyway. eliminate the complex and fragile logic for checking whether one of the non-allocated locale objects can be used for the result, and instead just memcmp against each of them. --- src/locale/newlocale.c | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) (limited to 'src/locale/newlocale.c') diff --git a/src/locale/newlocale.c b/src/locale/newlocale.c index 8fb006a7..68574605 100644 --- a/src/locale/newlocale.c +++ b/src/locale/newlocale.c @@ -9,37 +9,28 @@ int __loc_is_allocated(locale_t loc) locale_t __newlocale(int mask, const char *name, locale_t loc) { - int i, j; struct __locale_struct tmp; - const struct __locale_map *lm; + + for (int i=0; icat[i] : + __get_locale(i, (mask & (1<cat[i] = __get_locale(i, name); + *loc = tmp; return loc; } - /* Otherwise, build a temporary locale object, which will only - * be instantiated in allocated storage if it does not match - * one of the built-in static locales. This makes the common - * usage case for newlocale, getting a C locale with predictable - * behavior, very fast, and more importantly, fail-safe. */ - for (j=i=0; icat[i]; - else - lm = __get_locale(i, mask & (1<