Skip to content

Commit

Permalink
locale.c: Don't assume LC_CTYPE, LC_COLLATE are same
Browse files Browse the repository at this point in the history
This code is using isCNTRL_LC which depends on LC_CTYPE to verify that
something in the LC_COLLATE locale is a control.  That only works
properly if the two locales are the same.  This commit adds code to
ensure they are.
  • Loading branch information
khwilliamson committed May 5, 2021
1 parent 1c1c507 commit 72ae42b
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions locale.c
Expand Up @@ -5108,6 +5108,7 @@ Perl__mem_collxfrm(pTHX_ const char *input_string,
STRLEN xAlloc; /* xalloc is a reserved word in VC */
STRLEN length_in_chars;
bool first_time = TRUE; /* Cleared after first loop iteration */
const char * orig_CTYPE_locale = NULL;

PERL_ARGS_ASSERT__MEM_COLLXFRM;

Expand Down Expand Up @@ -5159,6 +5160,16 @@ Perl__mem_collxfrm(pTHX_ const char *input_string,
try_non_controls < 2;
try_non_controls++)
{

# ifdef USE_LOCALE_CTYPE

/* In this case we use isCNTRL_LC() below, which relies on
* LC_CTYPE, so that must be switched to correspond with the
* LC_COLLATE locale */
if (! try_non_controls && ! PL_in_utf8_COLLATE_locale) {
orig_CTYPE_locale = toggle_locale_c(LC_CTYPE, PL_collation_name);
}
# endif
/* Look through all legal code points (NUL isn't) */
for (j = 1; j < 256; j++) {
char * x; /* j's xfrm plus collation index */
Expand Down Expand Up @@ -5203,6 +5214,8 @@ Perl__mem_collxfrm(pTHX_ const char *input_string,
}
} /* end of loop through all 255 characters */

restore_toggled_locale_c(LC_CTYPE, orig_CTYPE_locale);

/* Stop looking if found */
if (cur_min_x) {
break;
Expand Down

0 comments on commit 72ae42b

Please sign in to comment.