Skip to content

Commit ca03bb4

Browse files
committed
ext/intl: Fix Locale::lookup() fallback on invalid language tags
1 parent 9e9b309 commit ca03bb4

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ PHP NEWS
3232
(David Carlier)
3333

3434
- Intl:
35+
. Fixed Locale::lookup() and locale_lookup() to return NULL instead of the
36+
fallback locale when a language tag cannot be canonicalized. (Weilin Du)
3537
. Fix incorrect argument positions for uninitialized calendar arguments in
3638
IntlCalendar::equals(), ::before(), ::after(), and ::isEquivalentTo().
3739
(Weilin Du)

ext/intl/locale/locale_methods.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,15 @@ PHP_FUNCTION(locale_lookup)
15621562
}
15631563

15641564
result_str = lookup_loc_range(loc_range, hash_arr, boolCanonical);
1565+
if (EG(exception)) {
1566+
RETURN_THROWS();
1567+
}
1568+
if (U_FAILURE(intl_error_get_code(NULL))) {
1569+
if (result_str) {
1570+
zend_string_release_ex(result_str, 0);
1571+
}
1572+
RETURN_NULL();
1573+
}
15651574
if(result_str == NULL || ZSTR_VAL(result_str)[0] == '\0') {
15661575
if( fallback_loc_str ) {
15671576
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(75) "lookup_loc_range: unable to canonicalize lang_tag: U_ILLEGAL_ARGUMENT_ERROR"
32+
NULL
33+
string(75) "lookup_loc_range: unable to canonicalize lang_tag: U_ILLEGAL_ARGUMENT_ERROR"
34+
lookup_loc_range: unable to canonicalize lang_tag
35+
lookup_loc_range: unable to canonicalize lang_tag

0 commit comments

Comments
 (0)