Skip to content

Fix #79212: NumberFormatter::format() may detect wrong type #5141

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 2 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
30 changes: 15 additions & 15 deletions ext/intl/formatter/formatter_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ PHP_FUNCTION( numfmt_format )
/* Fetch the object. */
FORMATTER_METHOD_FETCH_OBJECT;

if(type == FORMAT_TYPE_DEFAULT) {
if(Z_TYPE_P(number) == IS_STRING) {
convert_scalar_to_number_ex(number);
}

if(Z_TYPE_P(number) == IS_LONG) {
/* take INT32 on 32-bit, int64 on 64-bit */
type = (sizeof(zend_long) == 8)?FORMAT_TYPE_INT64:FORMAT_TYPE_INT32;
} else if(Z_TYPE_P(number) == IS_DOUBLE) {
type = FORMAT_TYPE_DOUBLE;
} else {
type = FORMAT_TYPE_INT32;
}
if(Z_TYPE_P(number) != IS_ARRAY) {
convert_scalar_to_number_ex(number);
} else {
convert_to_long(number);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest treating this as an error condition instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can break BC here for PHP-7.3 and 7.4 (passing an array is currently supported, and the docs even implicitly support that: "Can be integer or float, other values will be converted to a numeric value."). I think changing this for master, would be prudent, though.

}

if(Z_TYPE_P(number) != IS_DOUBLE && Z_TYPE_P(number) != IS_LONG) {
convert_scalar_to_number(number );
if(type == FORMAT_TYPE_DEFAULT) {
switch(Z_TYPE_P(number)) {
case IS_LONG:
/* take INT32 on 32-bit, int64 on 64-bit */
type = (sizeof(zend_long) == 8)?FORMAT_TYPE_INT64:FORMAT_TYPE_INT32;
break;
case IS_DOUBLE:
type = FORMAT_TYPE_DOUBLE;
break;
EMPTY_SWITCH_DEFAULT_CASE();
}
}

switch(type) {
Expand Down
18 changes: 18 additions & 0 deletions ext/intl/tests/bug79212.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Bug #79212 (NumberFormatter::format() may detect wrong type)
--SKIPIF--
<?php
if (!extension_loaded('intl')) die('skip intl extension not available');
if (!extension_loaded('gmp')) die('skip gmp extension not available');
?>
--FILE--
<?php
$fmt = new NumberFormatter('en_US', NumberFormatter::PATTERN_DECIMAL);
var_dump($fmt->format(gmp_init('823749273428379492374')));

$fmt = new NumberFormatter('en_US', NumberFormatter::PATTERN_DECIMAL);
var_dump($fmt->format([1], NumberFormatter::TYPE_INT64));
?>
--EXPECT--
string(21) "823749273428379400000"
string(1) "1"