From 926ab667f652954de2e5b058d31170c5dc5d196b Mon Sep 17 00:00:00 2001 From: Karl Williamson Date: Sun, 7 Mar 2021 08:04:06 -0700 Subject: [PATCH] locale.c: strxfrm() requires LC_CTYPE eq LC_COLLATE The libc functions strxfrm() on some platforms requires the LC_CTYPE locale to be the same as the LC_COLLATE locale (or rather, probably that they have the same code set, but checking for locale is cheaper). Otherwise mojibake would result, or more likely the function will fail, setting errno. This commit brings the locales into alignment if necessary --- locale.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/locale.c b/locale.c index ede39b2ef0f1..da28903d6251 100644 --- a/locale.c +++ b/locale.c @@ -5448,6 +5448,16 @@ Perl__mem_collxfrm(pTHX_ const char *input_string, /* Store the collation id */ *(U32*)xbuf = PL_collation_ix; +# ifdef USE_LOCALE_CTYPE + + orig_CTYPE_locale = toggle_locale_c(LC_CTYPE, PL_collation_name); + +# define CLEANUP_STRXFRM \ + restore_toggled_locale_c(LC_CTYPE, orig_CTYPE_locale) +# else +# define CLEANUP_STRXFRM NOOP +# endif + /* Then the transformation of the input. We loop until successful, or we * give up */ for (;;) { @@ -5581,6 +5591,8 @@ Perl__mem_collxfrm(pTHX_ const char *input_string, first_time = FALSE; } + CLEANUP_STRXFRM; + DEBUG_Lv((print_collxfrm_input_and_return(s, s + len, xlen, utf8), PerlIO_printf(Perl_debug_log, "Its xfrm is:"), PerlIO_printf(Perl_debug_log, "%s\n", @@ -5598,6 +5610,7 @@ Perl__mem_collxfrm(pTHX_ const char *input_string, bad: + CLEANUP_STRXFRM; DEBUG_Lv(print_collxfrm_input_and_return(s, s + len, NULL, utf8)); Safefree(xbuf);