Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Fixed issue with Math\Rand::getInteger() on ranges close to PHP_INT_MAX #5614

Merged
merged 3 commits into from
Mar 5, 2014
Merged

Fixed issue with Math\Rand::getInteger() on ranges close to PHP_INT_MAX #5614

merged 3 commits into from
Mar 5, 2014

Conversation

denixport
Copy link
Contributor

When min - max range is close or equal to PHP_INT_MAX log() produces number of bits
equal to machine size integer. So $filter = (int) ((1 << $bits) - 1); evaluates to zero.
In this case getInteger(0, PHP_INT_MAX) returns only zeros.

PR includes correct bit calculation and test

do {
$rnd = hexdec(bin2hex(self::getBytes($bytes, $strong)));
$rnd = $rnd & $filter;
$rnd = hexdec(bin2hex(self::getBytes($bytes, $strong)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use static::getBytes instead of self::getBytes

@denixport
Copy link
Contributor Author

@samsonasik @ezimuel PTAL

@weierophinney weierophinney added this to the 2.2.6 milestone Mar 3, 2014
@ezimuel ezimuel merged commit cc11994 into zendframework:master Mar 5, 2014
@Maks3w
Copy link
Member

Maks3w commented May 1, 2015

Is it normal $values to be negative?

I've changed the test for assert $values to be greater than 0

1) ZendTest\Math\RandTest::testIntegerRangeOverflow
Failed asserting that -5.1650883406387E+20 is greater than 0.

https://travis-ci.org/zendframework/zf2/jobs/60848303#L1081

@Maks3w
Copy link
Member

Maks3w commented May 1, 2015

Ok, I see what is happenning. $values probably has a value which produce a int overflow as result of operate with big numbers.

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants