#50630 [Com]: Modulo on strings/floats won't work on huge numbers

From: Date: Wed, 06 Jan 2010 06:46:19 +0000
Subject: #50630 [Com]: Modulo on strings/floats won't work on huge numbers
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-146373@lists.php.net to get a copy of this message
 ID:               50630
 Comment by:       federico dot lebron at gmail dot com
 Reported By:      bananen-joe at bananen-joe dot de
 Status:           Open
 Bug Type:         Scripting Engine problem
 Operating System: win32 only - Windows XP SP3
 PHP Version:      5.3.1
 New Comment:

The size of 9999999990 is larger than PHP_INT_MAX, so when converting
to 
integer (since you asked for modulo, and it gets converted to integer),

you're getting undefined behavior, as per 
http://php.net/manual/en/language.types.integer.php
. No warnings or 
notices will be triggered in such a case.

Internally (and on this machine, with PHP compiled as 32 bit), 
strtol("9999999990", NULL, 10) is being called for the string case, 
which is 2147483647. This is noted in the intval function's 
documentation, so it's expected. For the float case, (long) (unsigned 
long) (long long int) 9999999990 is being casted, which is 1410065398,

but this is undefined behavior.


Previous Comments:
------------------------------------------------------------------------

[2010-01-01 22:25:24] bananen-joe at bananen-joe dot de

Description:
------------
If you use the modulo operator on huge numbers (which cannot be
converted into integers) you get a strange result.
Even not error is reported.

Reproduce code:
---------------
error_reporting(E_ALL);
$number = '9999999990';
echo "$number % 10 = ", $number % 10, "<br>";
$number = 9999999990;
echo "$number % 10 = ", $number % 10, "<br>";
$number = '99990';
echo "$number % 10 = ", $number % 10, "<br>";
$number = (float) 99990;
echo "$number % 10 = ", $number % 10, "<br>";

Expected result:
----------------
9999999990 % 10 = 0 (or even an error notice/warning)
9999999990 % 10 = 0 (or even an error notice/warning)
99990 % 10 = 0
99990 % 10 = 0

Actual result:
--------------
9999999990 % 10 = 7
9999999990 % 10 = 8
99990 % 10 = 0
99990 % 10 = 0


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=50630&edit=1



Thread (3 messages)

« previous php.bugs (#146373) next »