From: Nikita Popov Date: Thu, 7 May 2020 16:50:42 +0000 (+0200) Subject: Ensure ctype_string is NULL for C locale X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2414b3d7756f7a4937d845fb2cb64947ab6796c8;p=php Ensure ctype_string is NULL for C locale We already document that this is the case, but currently it's only true if setlocale() has not been called. Make sure ctype_string is always NULL, even with an explicit "C" locale call, so we can more efficiently check whether we are in the "C" locale. Closes GH-5542. --- diff --git a/ext/pcre/php_pcre.c b/ext/pcre/php_pcre.c index 611a18bd48..9f67bff14c 100644 --- a/ext/pcre/php_pcre.c +++ b/ext/pcre/php_pcre.c @@ -595,8 +595,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in zend_string *key; pcre_cache_entry *ret; - if (locale_aware && BG(ctype_string) && - (ZSTR_LEN(BG(ctype_string)) != 1 && ZSTR_VAL(BG(ctype_string))[0] != 'C')) { + if (locale_aware && BG(ctype_string)) { key = zend_string_concat2( ZSTR_VAL(BG(ctype_string)), ZSTR_LEN(BG(ctype_string)), ZSTR_VAL(regex), ZSTR_LEN(regex)); diff --git a/ext/standard/string.c b/ext/standard/string.c index 93ad006244..7729acf451 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1462,7 +1462,7 @@ PHPAPI zend_string *php_string_tolower(zend_string *s) unsigned char *c; const unsigned char *e; - if (EXPECTED(!BG(locale_changed))) { + if (EXPECTED(!BG(ctype_string))) { return zend_string_tolower(s); } else { c = (unsigned char *)ZSTR_VAL(s); @@ -4801,7 +4801,12 @@ PHP_FUNCTION(setlocale) if (BG(ctype_string)) { zend_string_release_ex(BG(ctype_string), 0); } - if (len == ZSTR_LEN(loc) && !memcmp(ZSTR_VAL(loc), retval, len)) { + if (len == 1 && *retval == 'C') { + /* C locale is represented as NULL. */ + BG(ctype_string) = NULL; + zend_string_release_ex(loc, 0); + RETURN_INTERNED_STR(ZSTR_CHAR('C')); + } else if (len == ZSTR_LEN(loc) && !memcmp(ZSTR_VAL(loc), retval, len)) { BG(ctype_string) = zend_string_copy(loc); RETURN_STR(BG(ctype_string)); } else {