[php-src] PR #11649: number_format: prevent integer overflow on big decimal number
| From: | marc-mabe | Date: | Sun, 09 Jul 2023 15:28:36 +0000 |
| Subject: | [php-src] PR #11649: number_format: prevent integer overflow on big decimal number | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-22967@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/11649
Author: marc-mabe
Passing big numbers as decimal places to
number_format was resulting in integer
overflow with unexpected results.
Unlike round, which falls back to INT_MAX/MIN, the function number_format
needs to know for how many spaces after the decimal point the number needs to be formatted. This is
why I changed the argument from int to zend_long which is an internal API
BC break.
For passing big negative decimals I was able to provide a test but unfortunately passing big
positive decimal places results in OOM error (as expected). On my system with
memory_limit=0 and enough free mem I was able to test it manually:
```php
<?php
var_dump(number_format(1.2345, PHP_INT_MIN, '', ''));
$s = number_format(1.2345, 2147483648, '', '');
fwrite(STDOUT, $s, 100);
fwrite(STDOUT, "\n");
```
Before change:
```
$ php -d 'memory_limit=-1' /tmp/test.php
string(1) "1"
1
```
After change:
```
$ ./sapi/cli/php -d 'memory_limit=-1' /tmp/test.php
string(1) "0"
1234499999999999930722083263390231877565383911132812500000000000000000000000000000000000000000000000
```