diff options
author | Rich Felker <dalias@aerifal.cx> | 2015-05-27 15:54:47 -0400 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2015-05-27 15:54:47 -0400 |
commit | aeeac9ca5490d7d90fe061ab72da446c01ddf746 (patch) | |
tree | 4f74e2fb42ef8ac46151fe9037c41bb8b5eff52b /src | |
parent | 11858d31aa020df3e7e7dedf49f9870ce12f31cc (diff) | |
download | musl-aeeac9ca5490d7d90fe061ab72da446c01ddf746.tar.gz musl-aeeac9ca5490d7d90fe061ab72da446c01ddf746.tar.bz2 musl-aeeac9ca5490d7d90fe061ab72da446c01ddf746.tar.xz musl-aeeac9ca5490d7d90fe061ab72da446c01ddf746.zip |
implement fail-safe static locales for newlocale
this frees applications which need to make temporary use of the C
locale (via uselocale) from the possibility that newlocale might fail.
the C.UTF-8 locale is also provided as a static locale. presently they
behave the same, but this may change in the future.
Diffstat (limited to 'src')
-rw-r--r-- | src/locale/freelocale.c | 4 | ||||
-rw-r--r-- | src/locale/locale_map.c | 6 | ||||
-rw-r--r-- | src/locale/newlocale.c | 49 |
3 files changed, 46 insertions, 13 deletions
diff --git a/src/locale/freelocale.c b/src/locale/freelocale.c index ee3f029a..c2ae1a31 100644 --- a/src/locale/freelocale.c +++ b/src/locale/freelocale.c @@ -2,9 +2,11 @@ #include "locale_impl.h" #include "libc.h" +int __loc_is_allocated(locale_t); + void freelocale(locale_t l) { - free(l); + if (__loc_is_allocated(l)) free(l); } weak_alias(freelocale, __freelocale); diff --git a/src/locale/locale_map.c b/src/locale/locale_map.c index 30aa7fcc..4346bb02 100644 --- a/src/locale/locale_map.c +++ b/src/locale/locale_map.c @@ -26,7 +26,7 @@ static const char envvars[][12] = { static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 }; -static const struct __locale_map c_dot_utf8 = { +const struct __locale_map __c_dot_utf8 = { .map = empty_mo, .map_size = sizeof empty_mo, .name = "C.UTF-8" @@ -58,7 +58,7 @@ const struct __locale_map *__get_locale(int cat, const char *val) if (builtin) { if (cat == LC_CTYPE && val[1]=='.') - return (void *)&c_dot_utf8; + return (void *)&__c_dot_utf8; return 0; } @@ -117,7 +117,7 @@ const struct __locale_map *__get_locale(int cat, const char *val) /* For LC_CTYPE, never return a null pointer unless the * requested name was "C" or "POSIX". */ - if (!new && cat == LC_CTYPE) new = (void *)&c_dot_utf8; + if (!new && cat == LC_CTYPE) new = (void *)&__c_dot_utf8; UNLOCK(lock); return new; diff --git a/src/locale/newlocale.c b/src/locale/newlocale.c index 4e0cbd34..89d36b1d 100644 --- a/src/locale/newlocale.c +++ b/src/locale/newlocale.c @@ -3,21 +3,52 @@ #include "locale_impl.h" #include "libc.h" +extern const struct __locale_map __c_dot_utf8; + +static const struct __locale_struct c_locale = { 0 }; +static const struct __locale_struct c_dot_utf8_locale = { + .cat[LC_CTYPE] = &__c_dot_utf8 +}; + +int __loc_is_allocated(locale_t loc) +{ + return loc && loc != &c_locale && loc != &c_dot_utf8_locale; +} + locale_t __newlocale(int mask, const char *name, locale_t loc) { - int i; + int i, j; + struct __locale_struct tmp; + const struct __locale_map *lm; - if (!loc) { - loc = malloc(sizeof *loc); - if (!loc) return 0; + /* For locales with allocated storage, modify in-place. */ + if (__loc_is_allocated(loc)) { for (i=0; i<LC_ALL; i++) - if (!(mask & (1<<i))) - loc->cat[i] = __get_locale(i, ""); + if (mask & (1<<i)) + loc->cat[i] = __get_locale(i, name); + 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; i<LC_ALL; i++) { + if (loc && !(mask & (1<<i))) + lm = loc->cat[i]; + else + lm = __get_locale(i, mask & (1<<i) ? name : ""); + if (lm) j++; + tmp.cat[i] = lm; } - for (i=0; i<LC_ALL; i++) - if (mask & (1<<i)) - loc->cat[i] = __get_locale(i, name); + if (!j) + return (locale_t)&c_locale; + if (j==1 && tmp.cat[LC_CTYPE]==c_dot_utf8_locale.cat[LC_CTYPE]) + return (locale_t)&c_dot_utf8_locale; + + if ((loc = malloc(sizeof *loc))) *loc = tmp; return loc; } |