Skip to content

Commit a5840e8

Browse files
committed
Fix GH-12727: NumberFormatter constructor throws an exception on
invalid locale. Also re-establishing exception throwing on IntlDateFormatter constructor overwritten by accident most likely so postponing it for next major release.
1 parent 866aa12 commit a5840e8

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

ext/intl/dateformat/dateformat_create.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ static zend_result datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_error_handlin
113113
locale = Locale::createFromName(locale_str);
114114
/* get*Name accessors being set does not preclude being bogus */
115115
if (locale.isBogus() || strlen(locale.getISO3Language()) == 0) {
116-
goto error;
116+
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_create: invalid locale", 0);
117+
return FAILURE;
117118
}
118119

119120
/* process calendar */

ext/intl/formatter/formatter_main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#endif
1818

1919
#include <unicode/ustring.h>
20+
#include <unicode/uloc.h>
2021

2122
#include "php_intl.h"
2223
#include "formatter_class.h"
@@ -63,6 +64,11 @@ static int numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_error_handling *error_
6364
locale = intl_locale_get_default();
6465
}
6566

67+
if (strlen(uloc_getISO3Language(locale)) == 0) {
68+
zend_argument_value_error(1, "is invalid");
69+
return FAILURE;
70+
}
71+
6672
/* Create an ICU number formatter. */
6773
FORMATTER_OBJECT(nfo) = unum_open(style, spattern, spattern_len, locale, NULL, &INTL_DATA_ERROR_CODE(nfo));
6874

ext/intl/tests/gh12282.phpt

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,25 @@ intl
55
--FILE--
66
<?php
77

8-
var_dump(new IntlDateFormatter(
9-
'xx',
10-
IntlDateFormatter::FULL,
11-
IntlDateFormatter::FULL,
12-
null,
13-
null,
14-
'w'
15-
));
16-
Locale::setDefault('xx');
17-
var_dump(new IntlDateFormatter(Locale::getDefault()));
18-
--EXPECT--
19-
object(IntlDateFormatter)#1 (0) {
8+
try {
9+
new IntlDateFormatter(
10+
'xx',
11+
IntlDateFormatter::FULL,
12+
IntlDateFormatter::FULL,
13+
null,
14+
null,
15+
'w'
16+
);
17+
} catch (\IntlException $e) {
18+
echo $e->getMessage() . PHP_EOL;
2019
}
21-
object(IntlDateFormatter)#1 (0) {
20+
21+
Locale::setDefault('xx');
22+
try {
23+
new IntlDateFormatter(Locale::getDefault());
24+
} catch (\IntlException $e) {
25+
echo $e->getMessage();
2226
}
27+
--EXPECT--
28+
datefmt_create: invalid locale: U_ILLEGAL_ARGUMENT_ERROR
29+
datefmt_create: invalid locale: U_ILLEGAL_ARGUMENT_ERROR

ext/intl/tests/gh12727.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
numfmt creation failures
3+
--EXTENSIONS--
4+
intl
5+
--FILE--
6+
<?php
7+
8+
try {
9+
new NumberFormatter('xx', NumberFormatter::DECIMAL);
10+
} catch (ValueError $e) {
11+
echo $e->getMessage();
12+
}
13+
?>
14+
--EXPECT--
15+
NumberFormatter::__construct(): Argument #1 ($locale) is invalid

0 commit comments

Comments
 (0)