Skip to content

Commit 265af40

Browse files
committed
Use unsigned subtraction in php_random_int()
This subtraction may overflow the signed domain, which is UB. Use an unsigned subtraction instead.
1 parent ac356ba commit 265af40

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

ext/standard/random.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ PHPAPI int php_random_int(zend_long min, zend_long max, zend_long *result, zend_
235235
return SUCCESS;
236236
}
237237

238-
umax = max - min;
238+
umax = (zend_ulong) max - (zend_ulong) min;
239239

240240
if (php_random_bytes(&trial, sizeof(trial), should_throw) == FAILURE) {
241241
return FAILURE;

ext/standard/tests/random/random_int.phpt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
Test normal operation of random_int()
33
--FILE--
44
<?php
5-
//-=-=-=-
65

76
var_dump(is_int(random_int(10, 100)));
87

98
$x = random_int(10, 100);
109
var_dump($x >= 10 && $x <= 100);
1110

1211
var_dump(random_int(-1000, -1) < 0);
12+
var_dump(random_int(-1, PHP_INT_MAX) >= -1);
13+
var_dump(is_int(random_int(PHP_INT_MIN, PHP_INT_MAX)));
1314

1415
var_dump(random_int(42,42));
1516

1617
?>
17-
--EXPECT--
18+
--EXPECTF--
19+
bool(true)
20+
bool(true)
1821
bool(true)
1922
bool(true)
2023
bool(true)

0 commit comments

Comments
 (0)