Commit f868473e5be for php
commit f868473e5be49b0824e1332bd714f27a3023b4d2
Author: hohphu8 <kodoku169@gmail.com>
Date: Sun Nov 29 18:44:14 2026 +0700
ext/intl: Fix NULL deref in locale_parse() and locale_get_all_variants()
In ext/intl/locale/locale_methods.c, both add_array_entry() (used by
Locale::parseLocale() / locale_parse()) and locale_get_all_variants() call
php_strtok_r() on private/variant subtags without checking for a NULL
return before passing token to add_assoc_string() or strlen(). Passing a
locale string whose private or variant portion consists only of delimiters
(e.g., Locale::parseLocale('en-x--'), Locale::parseLocale('en_US___'), or
Locale::getAllVariants('en_US___')) causes a NULL pointer dereference
(SIGSEGV).
Guard the first php_strtok_r() call in both functions with if (token)
before allocating/adding entries or calling strlen().
Added a regression test covering delimiter-only private and variant
subtags for both Locale::parseLocale() and Locale::getAllVariants().
Close GH-25897
diff --git a/NEWS b/NEWS
index 6c767d74d0c..f0f2564d446 100644
--- a/NEWS
+++ b/NEWS
@@ -122,6 +122,9 @@ PHP NEWS
the new pattern is invalid. (Weilin Du, David Carlier)
. Fixed a crash when Intl formatters receive an uninitialized DateTime
subclass (GH-25878). (Ho Hoang Phu)
+ . Fixed NULL pointer dereference in Locale::parseLocale() and
+ Locale::getAllVariants() with delimiter-only private or variant subtags.
+ (Ho Hoang Phu)
- JSON:
. Fixed json_decode() and json_validate() error messages. (lacatoire)
diff --git a/ext/intl/locale/locale_methods.c b/ext/intl/locale/locale_methods.c
index 1b61794d67f..5b223bb1c51 100644
--- a/ext/intl/locale/locale_methods.c
+++ b/ext/intl/locale/locale_methods.c
@@ -1076,21 +1076,23 @@ static int add_array_entry(const char* loc_name, zval* hash_arr, char* key_name)
if( (strcmp(key_name , LOC_PRIVATE_TAG)==0) ||
( strcmp(key_name , LOC_VARIANT_TAG)==0) ){
if( result > 0 && key_value){
- int cnt = 0;
/* Tokenize on the "_" or "-" */
token = php_strtok_r( key_value->val , DELIMITER ,&last_ptr);
- if( cur_key_name ){
- efree( cur_key_name);
- }
- /* Over-allocates a few bytes for the integer so we don't have to reallocate. */
- size_t cur_key_name_size = (sizeof("-2147483648") - 1) + strlen(key_name) + 1;
- cur_key_name = emalloc(cur_key_name_size);
- snprintf( cur_key_name, cur_key_name_size , "%s%d", key_name , cnt++);
- add_assoc_string( hash_arr, cur_key_name , token);
- /* tokenize on the "_" or "-" and stop at singleton if any */
- while( (token = php_strtok_r(NULL , DELIMITER , &last_ptr)) && (strlen(token)>1) ){
- snprintf( cur_key_name , cur_key_name_size, "%s%d", key_name , cnt++);
+ if (token) {
+ int cnt = 0;
+ if( cur_key_name ){
+ efree( cur_key_name);
+ }
+ /* Over-allocates a few bytes for the integer so we don't have to reallocate. */
+ size_t cur_key_name_size = (sizeof("-2147483648") - 1) + strlen(key_name) + 1;
+ cur_key_name = emalloc(cur_key_name_size);
+ snprintf( cur_key_name, cur_key_name_size , "%s%d", key_name , cnt++);
add_assoc_string( hash_arr, cur_key_name , token);
+ /* tokenize on the "_" or "-" and stop at singleton if any */
+ while( (token = php_strtok_r(NULL , DELIMITER , &last_ptr)) && (strlen(token)>1) ){
+ snprintf( cur_key_name , cur_key_name_size, "%s%d", key_name , cnt++);
+ add_assoc_string( hash_arr, cur_key_name , token);
+ }
}
/*
if( strcmp(key_name, LOC_PRIVATE_TAG) == 0 ){
@@ -1189,10 +1191,12 @@ PHP_FUNCTION(locale_get_all_variants)
if( result > 0 && variant){
/* Tokenize on the "_" or "-" */
token = php_strtok_r( variant->val , DELIMITER , &saved_ptr);
- add_next_index_stringl( return_value, token , strlen(token));
- /* tokenize on the "_" or "-" and stop at singleton if any */
- while( (token = php_strtok_r(NULL , DELIMITER, &saved_ptr)) && (strlen(token)>1) ){
+ if (token) {
add_next_index_stringl( return_value, token , strlen(token));
+ /* tokenize on the "_" or "-" and stop at singleton if any */
+ while( (token = php_strtok_r(NULL , DELIMITER, &saved_ptr)) && (strlen(token)>1) ){
+ add_next_index_stringl( return_value, token , strlen(token));
+ }
}
}
if( variant ){
diff --git a/ext/intl/tests/locale_parse_empty_subtags.phpt b/ext/intl/tests/locale_parse_empty_subtags.phpt
new file mode 100644
index 00000000000..80455e1181e
--- /dev/null
+++ b/ext/intl/tests/locale_parse_empty_subtags.phpt
@@ -0,0 +1,27 @@
+--TEST--
+Locale::parseLocale() and Locale::getAllVariants() handle delimiter-only subtags without crashing
+--EXTENSIONS--
+intl
+--FILE--
+<?php
+
+var_dump(Locale::parseLocale('en-x--'));
+var_dump(Locale::parseLocale('x---'));
+var_dump(Locale::parseLocale('en_US___'));
+var_dump(Locale::getAllVariants('en_US___'));
+?>
+--EXPECT--
+array(1) {
+ ["language"]=>
+ string(2) "en"
+}
+array(0) {
+}
+array(2) {
+ ["language"]=>
+ string(2) "en"
+ ["region"]=>
+ string(2) "US"
+}
+array(0) {
+}