Use unsigned subtraction in php_random_int()

This subtraction may overflow the signed domain, which is UB. Use
an unsigned subtraction instead.
This commit is contained in:
Nikita Popov 2019-09-03 11:05:47 +02:00
parent ac356ba81f
commit 265af40a0a
2 changed files with 6 additions and 3 deletions

View File

@ -235,7 +235,7 @@ PHPAPI int php_random_int(zend_long min, zend_long max, zend_long *result, zend_
return SUCCESS;
}
umax = max - min;
umax = (zend_ulong) max - (zend_ulong) min;
if (php_random_bytes(&trial, sizeof(trial), should_throw) == FAILURE) {
return FAILURE;

View File

@ -2,7 +2,6 @@
Test normal operation of random_int()
--FILE--
<?php
//-=-=-=-
var_dump(is_int(random_int(10, 100)));
@ -10,11 +9,15 @@ $x = random_int(10, 100);
var_dump($x >= 10 && $x <= 100);
var_dump(random_int(-1000, -1) < 0);
var_dump(random_int(-1, PHP_INT_MAX) >= -1);
var_dump(is_int(random_int(PHP_INT_MIN, PHP_INT_MAX)));
var_dump(random_int(42,42));
?>
--EXPECT--
--EXPECTF--
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)