php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49330 counter doesn't count properly
Submitted: 2009-08-22 16:22 UTC Modified: 2009-08-24 11:21 UTC
From: john at sinteur dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.10 OS: FreeBSD, Mac OS
Private report: No CVE-ID: None
 [2009-08-22 16:22 UTC] john at sinteur dot com
Description:
------------
Counting doesn't appear to wrap properly at 32 bits





Reproduce code:
---------------
$xxx=0;

while (true) {

$xxx++;
if ($xxx%10000000 == 0)
        print $xxx . "\n";

}

Expected result:
----------------
10000000
20000000
30000000
40000000
50000000
...(etc)
2120000000
2130000000
2140000000
2150000000
2160000000
2170000000
2180000000


Actual result:
--------------
10000000
20000000
30000000
40000000
50000000
...(etc)
2120000000
2130000000
2140000000
2154967296
2164967296
2174967296
2184967296


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-24 11:21 UTC] sjoerd@php.net
Thank you for your report.

The behavior you report is not a bug. If PHP encounters a number beyond the bounds of the integer  type, it will be interpreted as a float instead. That is why it is possible to have $xxx bigger than PHP_INT_MAX. Operands of modulus are converted to integers (by stripping the decimal part) before processing. That is why the $xxx is wrapped around for the modulo operator.

PHP_INT_MAX % $a == 2147483647 % $a
(PHP_INT_MAX + 1) % $a == -2147483648 % $a


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 18:01:29 2024 UTC