Skip to content

Commit 3b40dbc

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: ext/intl: Fix Locale::lookup() fallback on invalid language tags (#22306)
2 parents cc545f3 + 7a3dd1b commit 3b40dbc

3 files changed

Lines changed: 51 additions & 4 deletions

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ PHP NEWS
8181
transliterator_transliterate(). (Weilin Du)
8282
. Fixed IntlTimeZone::getDisplayName() to synchronize object error state
8383
for invalid display types. (Weilin Du)
84+
. Fixed Locale::lookup() and locale_lookup() to return NULL instead of the
85+
fallback locale when a language tag cannot be canonicalized. (Weilin Du)
8486
. Added IntlNumberRangeFormatter class to format an interval of two numbers
8587
with a given skeleton, locale, collapse type and identity fallback.
8688
(BogdanUngureanu)

ext/intl/locale/locale_methods.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,14 +1435,15 @@ static zend_string* lookup_loc_range(const char* loc_range, HashTable* hash_arr,
14351435
zend_argument_value_error(2, "must not contain any null bytes");
14361436
LOOKUP_CLEAN_RETURN(NULL);
14371437
}
1438-
cur_arr[cur_arr_len*2] = estrndup(Z_STRVAL_P(ele_value), Z_STRLEN_P(ele_value));
1439-
result = strToMatch(Z_STRVAL_P(ele_value), cur_arr[cur_arr_len*2]);
1438+
i = cur_arr_len*2;
1439+
cur_arr[i] = estrndup(Z_STRVAL_P(ele_value), Z_STRLEN_P(ele_value));
1440+
cur_arr_len++;
1441+
result = strToMatch(Z_STRVAL_P(ele_value), cur_arr[i]);
14401442
if(result == 0) {
14411443
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "unable to canonicalize lang_tag");
14421444
LOOKUP_CLEAN_RETURN(NULL);
14431445
}
1444-
cur_arr[cur_arr_len*2+1] = Z_STRVAL_P(ele_value);
1445-
cur_arr_len++ ;
1446+
cur_arr[i+1] = Z_STRVAL_P(ele_value);
14461447
} ZEND_HASH_FOREACH_END(); /* end of for */
14471448

14481449
/* Canonicalize array elements */
@@ -1562,6 +1563,15 @@ U_CFUNC PHP_FUNCTION(locale_lookup)
15621563
}
15631564

15641565
result_str = lookup_loc_range(loc_range, hash_arr, boolCanonical);
1566+
if (EG(exception)) {
1567+
RETURN_THROWS();
1568+
}
1569+
if (U_FAILURE(intl_error_get_code(NULL))) {
1570+
if (result_str) {
1571+
zend_string_release_ex(result_str, 0);
1572+
}
1573+
RETURN_NULL();
1574+
}
15651575
if(result_str == NULL || ZSTR_VAL(result_str)[0] == '\0') {
15661576
if( fallback_loc_str ) {
15671577
result_str = zend_string_copy(fallback_loc_str);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Locale::lookup() returns null for invalid language tags
3+
--EXTENSIONS--
4+
intl
5+
--FILE--
6+
<?php
7+
8+
var_dump(Locale::lookup([''], 'de-DE', false, 'en-US'));
9+
var_dump(intl_get_error_message());
10+
11+
var_dump(locale_lookup([''], 'de-DE', false, 'en-US'));
12+
var_dump(intl_get_error_message());
13+
14+
ini_set('intl.use_exceptions', '1');
15+
16+
try {
17+
Locale::lookup([''], 'de-DE', false, 'en-US');
18+
} catch (IntlException $e) {
19+
echo $e->getMessage(), PHP_EOL;
20+
}
21+
22+
try {
23+
locale_lookup([''], 'de-DE', false, 'en-US');
24+
} catch (IntlException $e) {
25+
echo $e->getMessage(), PHP_EOL;
26+
}
27+
28+
?>
29+
--EXPECT--
30+
NULL
31+
string(57) "unable to canonicalize lang_tag: U_ILLEGAL_ARGUMENT_ERROR"
32+
NULL
33+
string(57) "unable to canonicalize lang_tag: U_ILLEGAL_ARGUMENT_ERROR"
34+
unable to canonicalize lang_tag
35+
unable to canonicalize lang_tag

0 commit comments

Comments
 (0)