Edit report at https://bugs.php.net/bug.php?id=64450&edit=1

 ID:                 64450
 Comment by:         a...@php.net
 Reported by:        stefano dot baccianella at gmail dot com
 Summary:            mt_rand causes overflow within certain max value
 Status:             Wont fix
 Type:               Bug
 Package:            *General Issues
 Operating System:   Windows 7 64bit
 PHP Version:        5.4.13
 Assigned To:        ab
 Block user comment: N
 Private report:     N

 New Comment:

Linux or BSD 64 bit i mean.


Previous Comments:
------------------------------------------------------------------------
[2013-03-26 19:44:39] a...@php.net

Well, Windows stands completely alongside. long on both x86 and x64 Windows is 
32 
bit, so no matter which PHP build is used. The code we deal with here only 
works 
correct on system having sizeof(long) >= sizeof(double), that are for instance 
64 
bit Linux or BSD.

------------------------------------------------------------------------
[2013-03-26 08:11:43] stefano dot baccianella at gmail dot com

Yeah, true. I was misled by the fact that my system is 64bit but PHP for 
windows 
is compiled on 32bit.
I think that an RFC for 5.6 should be enough.
Thank you

------------------------------------------------------------------------
[2013-03-25 23:22:53] a...@php.net

I'm not sure something like that would be accepted as the valid domain is 
clearly 
documented and such a change is still a BC breach. What might work is a change 
request including an RFC for 5.6, otherwise a PECL ext.

------------------------------------------------------------------------
[2013-03-25 20:47:13] stefano dot baccianella at gmail dot com

Ok, now it's clear. 
Maybe as a minor fix I can suggest to check if MAX is greater than PHP_INT_MAX 
and 
return a value instead of an error (FALSE value?) or an error with an 
explanation, 
in both cases it's better than an overflow.

Thank you very much for your time

------------------------------------------------------------------------
[2013-03-25 20:40:52] a...@php.net

Please consider the following snippet

while(true) {
        $max = pow(10, 13); // 10000000000000
        $rand = mt_rand(0, $max);
        if ($rand > PHP_INT_MAX) {
                break;
        }
}

It *seems* to work, but you'll never reach the break condition. On some systems 
that 
might be PHP_INT_MAX*2, not sure. Thus, the whole range between pow(10, 13) and 
PHP_INT_MAX will never be returned.

Additionally please be aware - we deal with a pseudo randomness here, it works 
as 
expected in 32 bit range only with 32 bit long. The implementation seems to run 
unexpected overflows on some places like pow(10, 12) you've reported, but it 
just 
physically won't work with floats.

Thanks for reporting. A better solution is definitely needed for this 
functionality, 
not just a bug fix.

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


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=64450


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

Reply via email to