Skip to content

Disallow passing array as number to NumberFormatter::format() #5181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,16 @@ static const char *zend_parse_arg_impl(int arg_num, zval *arg, va_list *va, cons
}
break;

case 'n':
{
zval **p = va_arg(*va, zval **);

if (!zend_parse_arg_number(arg, p, check_null)) {
return "number";
}
}
break;

case 's':
{
char **p = va_arg(*va, char **);
Expand Down Expand Up @@ -793,7 +803,7 @@ static int zend_parse_va_args(int num_args, const char *type_spec, va_list *va,
case 'f': case 'A':
case 'H': case 'p':
case 'S': case 'P':
case 'L':
case 'L': case 'n':
max_num_args++;
break;

Expand Down
1 change: 1 addition & 0 deletions docs/parameter-parsing-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ f - function or array containing php method call info (returned as
h - array (returned as HashTable*)
H - array or HASH_OF(object) (returned as HashTable*)
l - long (zend_long)
n - long or double (zval*)
o - object of any type (zval*)
O - object of specific type given by class entry (zval*, zend_class_entry)
p - valid path (string without null bytes in the middle) and its length (char*, size_t)
Expand Down
4 changes: 2 additions & 2 deletions ext/intl/formatter/formatter.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function __construct(string $locale, int $style, string $pattern = "") {}
public static function create(string $locale, int $style, string $pattern = "") {}

/** @return string|false */
public function format($value, int $type = NumberFormatter::TYPE_DEFAULT) {}
public function format(int|float $value, int $type = NumberFormatter::TYPE_DEFAULT) {}

/** @return int|float|false */
public function parse(string $value, int $type = NumberFormatter::TYPE_DOUBLE, &$position = null) {}
Expand Down Expand Up @@ -58,7 +58,7 @@ public function getErrorMessage() {}

function numfmt_create(string $locale, int $style, string $pattern = ""): ?NumberFormatter {}

function numfmt_format(NumberFormatter $fmt, $value, int $type = NumberFormatter::TYPE_DEFAULT): string|false {}
function numfmt_format(NumberFormatter $fmt, int|float $value, int $type = NumberFormatter::TYPE_DEFAULT): string|false {}

function numfmt_parse(NumberFormatter $fmt, string $value, int $type = NumberFormatter::TYPE_DOUBLE, &$position = null): int|float|false {}

Expand Down
4 changes: 2 additions & 2 deletions ext/intl/formatter/formatter_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ZEND_END_ARG_INFO()
#define arginfo_class_NumberFormatter_create arginfo_class_NumberFormatter___construct

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_NumberFormatter_format, 0, 0, 1)
ZEND_ARG_INFO(0, value)
ZEND_ARG_TYPE_MASK(0, value, MAY_BE_LONG|MAY_BE_DOUBLE)
ZEND_ARG_TYPE_INFO(0, type, IS_LONG, 0)
ZEND_END_ARG_INFO()

Expand Down Expand Up @@ -73,7 +73,7 @@ ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_numfmt_format, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_OBJ_INFO(0, fmt, NumberFormatter, 0)
ZEND_ARG_INFO(0, value)
ZEND_ARG_TYPE_MASK(0, value, MAY_BE_LONG|MAY_BE_DOUBLE)
ZEND_ARG_TYPE_INFO(0, type, IS_LONG, 0)
ZEND_END_ARG_INFO()

Expand Down
8 changes: 1 addition & 7 deletions ext/intl/formatter/formatter_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ PHP_FUNCTION( numfmt_format )
FORMATTER_METHOD_INIT_VARS;

/* Parse parameters. */
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Oz|l",
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "On|l",
&object, NumberFormatter_ce_ptr, &number, &type ) == FAILURE )
{
RETURN_THROWS();
Expand All @@ -48,12 +48,6 @@ PHP_FUNCTION( numfmt_format )
/* Fetch the object. */
FORMATTER_METHOD_FETCH_OBJECT;

if(Z_TYPE_P(number) != IS_ARRAY) {
convert_scalar_to_number_ex(number);
} else {
convert_to_long(number);
}

if(type == FORMAT_TYPE_DEFAULT) {
switch(Z_TYPE_P(number)) {
case IS_LONG:
Expand Down
21 changes: 0 additions & 21 deletions ext/intl/tests/bug48227.phpt

This file was deleted.

2 changes: 2 additions & 0 deletions ext/intl/tests/bug53735.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var_dump($f->format(0.26));
--EXPECTF--
string(%d) "5,50 kr%A"
string(%d) "5,50 kr%A"

Notice: A non well formed numeric value encountered in %s on line %d
string(%d) "5,00 kr%A"
string(5) "23,25"
string(3) "26%"
18 changes: 0 additions & 18 deletions ext/intl/tests/bug79212.phpt

This file was deleted.