|  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
 [2017-12-10 23:35 UTC] erik at evanv dot nl
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);
php > echo sprintf("%u", $var);

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-12-11 15:25 UTC]
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2017-12-11 15:25 UTC]
Actually, this boils down to `(int) (PHP_INT_MAX*2)` being
undefined[1], so this is not a bug.

[1] <>
 [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]
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 <>.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Feb 28 08:01:29 2024 UTC