Skip to content

Commit e31bfe4

Browse files
Added more validation for parameters and fixed coding style
1 parent a2dd8e9 commit e31bfe4

File tree

6 files changed

+71
-42
lines changed

6 files changed

+71
-42
lines changed

ext/intl/listformatter/listformatter.stub.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
/** @generate-class-entries */
44

5-
/** @not-serializable */
6-
/** @strict-properties */
5+
/**
6+
* @not-serializable
7+
* @strict-properties
8+
*/
79
final class IntlListFormatter {
810

9-
/** @cvalue ULISTFMT_TYPE_OR */
11+
/** @cvalue ULISTFMT_TYPE_OR */
1012
public const int TYPE_OR = UNKNOWN;
1113

12-
/** @cvalue ULISTFMT_TYPE_UNITS */
14+
/** @cvalue ULISTFMT_TYPE_UNITS */
1315
public const int TYPE_UNITS = UNKNOWN;
1416

1517
/** @cvalue ULISTFMT_TYPE_AND */
@@ -24,27 +26,11 @@ final class IntlListFormatter {
2426
/** @cvalue ULISTFMT_WIDTH_NARROW */
2527
public const int WIDTH_NARROW = UNKNOWN;
2628

27-
/**
28-
* @param string $locale
29-
* @param int $type
30-
* @param int $width
31-
*/
3229
public function __construct(string $locale, int $type = IntlListFormatter::TYPE_AND, int $width = IntlListFormatter::WIDTH_WIDE) {}
3330

34-
/**
35-
* @param array $strings
36-
*
37-
* @return string|false
38-
*/
3931
public function format(array $strings): string|false {}
4032

41-
/**
42-
* @return int
43-
*/
4433
public function getErrorCode(): int {}
4534

46-
/**
47-
* @return string
48-
*/
4935
public function getErrorMessage(): string {}
50-
}
36+
}

ext/intl/listformatter/listformatter_arginfo.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/intl/listformatter/listformatter_class.c

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "listformatter_class.h"
2020
#include "intl_convert.h"
2121

22-
2322
static zend_object_handlers listformatter_handlers;
2423

2524
/* {{{ listformatter_free_obj */
@@ -48,26 +47,45 @@ static zend_object *listformatter_create_object(zend_class_entry *class_type)
4847
PHP_METHOD(IntlListFormatter, __construct)
4948
{
5049
ListFormatter_object *obj = Z_INTL_LISTFORMATTER_P(ZEND_THIS);
51-
zend_string *locale;
50+
char* locale;
51+
size_t locale_len = 0;
5252
zend_long type = ULISTFMT_TYPE_AND;
5353
zend_long width = ULISTFMT_WIDTH_WIDE;
5454
ZEND_PARSE_PARAMETERS_START(1, 3)
55-
Z_PARAM_STR(locale)
55+
Z_PARAM_STRING(locale, locale_len)
5656
Z_PARAM_OPTIONAL
5757
Z_PARAM_LONG(type)
5858
Z_PARAM_LONG(width)
5959
ZEND_PARSE_PARAMETERS_END();
6060

61-
if (strlen(uloc_getISO3Language(ZSTR_VAL(locale))) == 0) {
62-
zend_argument_value_error(1, "\"%s\" is invalid", ZSTR_VAL(locale));
63-
RETURN_THROWS();
64-
}
61+
if(locale_len == 0) {
62+
locale = (char *)intl_locale_get_default();
63+
}
64+
65+
if (strlen(uloc_getISO3Language(locale)) == 0) {
66+
zend_argument_value_error(1, "\"%s\" is invalid", locale);
67+
RETURN_THROWS();
68+
}
69+
70+
if (locale_len > INTL_MAX_LOCALE_LEN) {
71+
zend_argument_value_error(1, "Locale string too long, should be no longer than %d characters", INTL_MAX_LOCALE_LEN);
72+
RETURN_THROWS();
73+
}
6574

6675
UErrorCode status = U_ZERO_ERROR;
67-
if (U_ICU_VERSION_MAJOR_NUM >= 67) {
76+
#if U_ICU_VERSION_MAJOR_NUM >= 67
77+
if (type != ULISTFMT_TYPE_AND && type != ULISTFMT_TYPE_OR && type != ULISTFMT_TYPE_UNITS) {
78+
zend_argument_value_error(2, "must be one of IntlListFormatter::TYPE_AND, IntlListFormatter::TYPE_OR, or IntlListFormatter::TYPE_UNITS");
79+
RETURN_THROWS();
80+
}
81+
82+
if (width != ULISTFMT_WIDTH_WIDE && width != ULISTFMT_WIDTH_SHORT && width != ULISTFMT_WIDTH_NARROW) {
83+
zend_argument_value_error(3, "must be one of IntlListFormatter::WIDTH_WIDE, IntlListFormatter::WIDTH_SHORT, or IntlListFormatter::WIDTH_NARROW");
84+
RETURN_THROWS();
85+
}
6886

69-
LISTFORMATTER_OBJECT(obj) = ulistfmt_openForType(ZSTR_VAL(locale), type, width, &status);
70-
} else {
87+
LISTFORMATTER_OBJECT(obj) = ulistfmt_openForType(locale, type, width, &status);
88+
#else
7189
if (type != ULISTFMT_TYPE_AND) {
7290
zend_argument_value_error(2, "ICU 66 and below only support IntlListFormatter::TYPE_AND");
7391
RETURN_THROWS();
@@ -78,8 +96,9 @@ PHP_METHOD(IntlListFormatter, __construct)
7896
RETURN_THROWS();
7997
}
8098

81-
LISTFORMATTER_OBJECT(obj) = ulistfmt_open(ZSTR_VAL(locale), &status);
82-
}
99+
LISTFORMATTER_OBJECT(obj) = ulistfmt_open(locale, &status);
100+
#endif
101+
83102
if (U_FAILURE(status)) {
84103
intl_error_set(NULL, status, "Constructor failed", 0);
85104
zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);

ext/intl/tests/listformatter/listformatter_error.phpt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ try {
1414
echo PHP_EOL;
1515

1616
try {
17-
$formatter = new IntlListFormatter('ro', IntlListFormatter::TYPE_AND, 23);
18-
} catch (IntlException $exception) {
19-
echo $exception->getMessage() .PHP_EOL;
20-
echo intl_get_error_code();
17+
$formatter = new IntlListFormatter('ro_thisiswaytooooooooooooooooooooooooooooooooooooooooooooolongtobevaliditneedstobeatleast157characterstofailthevalidationinthelistformattercodeimplementation', IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_WIDE);
18+
} catch(ValueError $exception) {
19+
echo $exception->getMessage();
2120
}
2221

22+
echo PHP_EOL;
23+
2324
$formatter = new IntlListFormatter('ro', IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_WIDE);
2425

25-
echo PHP_EOL;
2626
try {
2727
echo $formatter->format([new stdClass()]);
2828
} catch(Error $error) {
@@ -31,6 +31,5 @@ try {
3131

3232
--EXPECT--
3333
IntlListFormatter::__construct(): Argument #1 ($locale) "f" is invalid
34-
Constructor failed
35-
1
34+
IntlListFormatter::__construct(): Argument #1 ($locale) Locale string too long, should be no longer than 156 characters
3635
Object of class stdClass could not be converted to string

ext/intl/tests/listformatter/listformatter_with_paramaters.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ IntlListFormatter: Test AND, OR and Width parameters
33
--EXTENSIONS--
44
intl
55
--SKIPIF--
6-
<?php if (version_compare(INTL_ICU_VERSION, '67.0') >= 0) die('skip for ICU < 67.0'); ?>
6+
<?php if (version_compare(INTL_ICU_VERSION, '67.0') <= 0) die('skip for ICU < 67.0'); ?>
77
--FILE--
88
<?php
99

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
IntlListFormatter: Test invalid parameters for TYPE and WIDTH
3+
--EXTENSIONS--
4+
intl
5+
--SKIPIF--
6+
<?php if (version_compare(INTL_ICU_VERSION, '67.0') <= 0) die('skip for ICU < 67.0'); ?>
7+
--FILE--
8+
<?php
9+
10+
try {
11+
$formatter = new IntlListFormatter('ro', 23232323);
12+
} catch(ValueError $exception) {
13+
echo $exception->getMessage();
14+
}
15+
16+
echo PHP_EOL;
17+
18+
try {
19+
$formatter = new IntlListFormatter('ro', IntlListFormatter::TYPE_AND, 2323232);
20+
} catch(ValueError $exception) {
21+
echo $exception->getMessage();
22+
}
23+
--EXPECT--
24+
IntlListFormatter::__construct(): Argument #2 ($type) must be one of IntlListFormatter::TYPE_AND, IntlListFormatter::TYPE_OR, or IntlListFormatter::TYPE_UNITS
25+
IntlListFormatter::__construct(): Argument #3 ($width) must be one of IntlListFormatter::WIDTH_WIDE, IntlListFormatter::WIDTH_SHORT, or IntlListFormatter::WIDTH_NARROW

0 commit comments

Comments
 (0)