Skip to content

Commit 2414b3d

Browse files
committed
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.
1 parent d38f819 commit 2414b3d

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

ext/pcre/php_pcre.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in
595595
zend_string *key;
596596
pcre_cache_entry *ret;
597597

598-
if (locale_aware && BG(ctype_string) &&
599-
(ZSTR_LEN(BG(ctype_string)) != 1 && ZSTR_VAL(BG(ctype_string))[0] != 'C')) {
598+
if (locale_aware && BG(ctype_string)) {
600599
key = zend_string_concat2(
601600
ZSTR_VAL(BG(ctype_string)), ZSTR_LEN(BG(ctype_string)),
602601
ZSTR_VAL(regex), ZSTR_LEN(regex));

ext/standard/string.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,7 @@ PHPAPI zend_string *php_string_tolower(zend_string *s)
14621462
unsigned char *c;
14631463
const unsigned char *e;
14641464

1465-
if (EXPECTED(!BG(locale_changed))) {
1465+
if (EXPECTED(!BG(ctype_string))) {
14661466
return zend_string_tolower(s);
14671467
} else {
14681468
c = (unsigned char *)ZSTR_VAL(s);
@@ -4801,7 +4801,12 @@ PHP_FUNCTION(setlocale)
48014801
if (BG(ctype_string)) {
48024802
zend_string_release_ex(BG(ctype_string), 0);
48034803
}
4804-
if (len == ZSTR_LEN(loc) && !memcmp(ZSTR_VAL(loc), retval, len)) {
4804+
if (len == 1 && *retval == 'C') {
4805+
/* C locale is represented as NULL. */
4806+
BG(ctype_string) = NULL;
4807+
zend_string_release_ex(loc, 0);
4808+
RETURN_INTERNED_STR(ZSTR_CHAR('C'));
4809+
} else if (len == ZSTR_LEN(loc) && !memcmp(ZSTR_VAL(loc), retval, len)) {
48054810
BG(ctype_string) = zend_string_copy(loc);
48064811
RETURN_STR(BG(ctype_string));
48074812
} else {

0 commit comments

Comments
 (0)