Bug #75664 [Com]: sprintf %u results in 0 for numbers with more digits than PHP_INT_MAX
| From: | erik at evanv dot nl | Date: | Mon, 11 Dec 2017 19:47:54 +0000 |
| Subject: | Bug #75664 [Com]: sprintf %u results in 0 for numbers with more digits than PHP_INT_MAX | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-213053@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75664&edit=1
ID: 75664
Comment by: erik at evanv dot nl
Reported by: erik at evanv dot nl
Summary: sprintf %u results in 0 for numbers with more digits
than PHP_INT_MAX
Status: Not a bug
Type: Bug
Package: Strings related
Operating System: linux 4.14.4
PHP Version: 7.2.0
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
I hope you can consider adding an error for this. Silently overflowing to zero is not something you
expect to be possible in a managed language.
This behavior caused data corruption in my company's product. I was certain it was caused by a
C program we were using but to my surprise it was this.
Previous Comments:
------------------------------------------------------------------------
[2017-12-11 15:25:06] cmb@php.net
Actually, this boils down to
(int) (PHP_INT_MAX*2) being
undefined[1], so this is not a bug.
[1] <http://www.php.net/manual/en/language.types.integer.php#language.types.integer.casting.from-float>
------------------------------------------------------------------------
[2017-12-10 23:35:07] erik at evanv dot nl
Description:
------------
Code speaks for itself. This can lead to data corruption.
A decent solution would be to display unknown least significant digits as 0 just like %f.
Alternative would be an ArithmeticError.
Test script:
---------------
php > $var = PHP_INT_MAX * 2;
php > echo sprintf("%f", $var);
18446744073709551616.000000
php > echo sprintf("%u", $var);
0
Expected result:
----------------
18446744073709551616
Actual result:
--------------
0
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75664&edit=1