php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75664 sprintf %u results in 0 for numbers with more digits than PHP_INT_MAX
Submitted: 2017-12-10 23:35 UTC Modified: 2017-12-11 22:25 UTC
From: erik at evanv dot nl Assigned: cmb (profile)
Status: Not a bug Package: Strings related
PHP Version: 7.2.0 OS: linux 4.14.4
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: erik at evanv dot nl
New email:
PHP Version: OS:

 

 [2017-12-10 23:35 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-12-11 15:25 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2017-12-11 15:25 UTC] 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-11 19:47 UTC] erik at evanv dot nl
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.
 [2017-12-11 22:25 UTC] cmb@php.net
I have to admit that the present behavior is not optimal.
However, changing it would require the RFC process.  Feel free to
start it; see <https://wiki.php.net/rfc/howto>.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Dec 14 04:01:23 2019 UTC