diff --git a/lib/libc/locale/collate.c b/lib/libc/locale/collate.c --- a/lib/libc/locale/collate.c +++ b/lib/libc/locale/collate.c @@ -68,6 +68,10 @@ {{0}, "C"}, 1, 0, 0, 0 }; +struct xlocale_collate __xlocale_CU8_collate = { + {{0}, "C"}, 1, 0, 0, 0 +}; + static int __collate_load_tables_l(const char *encoding, struct xlocale_collate *table); @@ -84,10 +88,11 @@ void * __collate_load(const char *encoding, __unused locale_t unused) { - if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0 || - strncmp(encoding, "C.", 2) == 0) { + if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) return (&__xlocale_C_collate); - } + else if (strncmp(encoding, "C.", 2) == 0) + return (&__xlocale_CU8_collate); + struct xlocale_collate *table = calloc(sizeof(struct xlocale_collate), 1); if (table == NULL) diff --git a/lib/libc/tests/locale/Makefile b/lib/libc/tests/locale/Makefile --- a/lib/libc/tests/locale/Makefile +++ b/lib/libc/tests/locale/Makefile @@ -13,6 +13,7 @@ ATF_TESTS_C+= mbsrtowcs_test ATF_TESTS_C+= mbstowcs_2_test ATF_TESTS_C+= mbtowc_2_test +ATF_TESTS_C+= newlocale_cu8_test ATF_TESTS_C+= towctrans_test ATF_TESTS_C+= wcrtomb_test ATF_TESTS_C+= wcsnrtombs_test diff --git a/lib/libc/tests/locale/newlocale_cu8_test.c b/lib/libc/tests/locale/newlocale_cu8_test.c new file mode 100644 --- /dev/null +++ b/lib/libc/tests/locale/newlocale_cu8_test.c @@ -0,0 +1,73 @@ +#include + +#include + +#include + +/* + * PR269375: Check that creating/using the C.UTF-8 locale does not clobber the + * C locale contents. The issue is cosmetic only as both locales have empty + * collate parts, but we need to correctly report the one in use in any case. + */ + +struct { + int lpmask; + const char *lpname; +} lparts[] = { + { LC_COLLATE_MASK, "LC_COLLATE" }, + { LC_CTYPE_MASK, "LC_CTYPE" }, + { LC_MONETARY_MASK, "LC_MONETARY" }, + { LC_NUMERIC_MASK, "LC_NUMERIC" }, + { LC_TIME_MASK, "LC_TIME" }, + { LC_MESSAGES_MASK, "LC_MESSAGES" }, +}; + +void +check_lparts(const char *step, const char *expected) +{ + int i; + + for (i = 0; i < nitems(lparts); i++) { + const char *actual; + + actual = querylocale(lparts[i].lpmask, uselocale(NULL)); + ATF_CHECK_STREQ_MSG(expected, actual, "%s: wrong value for %s", + step, lparts[i].lpname); + } +} + +ATF_TC_WITHOUT_HEAD(newlocale_cu8_test); +ATF_TC_BODY(newlocale_cu8_test, tc) +{ + locale_t c, cu8; + + c = newlocale(LC_ALL_MASK, "C", NULL); + ATF_REQUIRE(c != NULL); + + ATF_REQUIRE(uselocale(c) != NULL); + check_lparts("initial C", "C"); + + /* + * Creating the C.UTF-8 locale only after the initial C check is + * deliberate - newlocale() call would clobber the C locale contents, + * hence we want at least the initial check above to be correct. + */ + cu8 = newlocale(LC_ALL_MASK, "C.UTF-8", NULL); + ATF_REQUIRE(cu8 != NULL); + ATF_REQUIRE(uselocale(cu8) != NULL); + check_lparts("switched to C.UTF-8", "C.UTF-8"); + + ATF_REQUIRE(uselocale(c) != NULL); + check_lparts("back to C", "C"); + + freelocale(c); + freelocale(cu8); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, newlocale_cu8_test); + + return (atf_no_error()); +}