Skip to content

Commit 133b9b0

Browse files
authoredJul 22, 2022
Avoid signed integer overflow in php_random_range() (#9066)
1 parent dfbe964 commit 133b9b0

File tree

3 files changed

+5
-6
lines changed

3 files changed

+5
-6
lines changed
 

‎NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ PHP NEWS
6262

6363
- Random:
6464
. Added new random extension. (Go Kudo)
65+
. Fixed bug GH-9066 (signed integer overflow). (zeriyoshi)
6566

6667
- SPL:
6768
. Widen iterator_to_array() and iterator_count()'s $iterator parameter to

‎ext/random/php_random.h

-2
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ extern PHPAPI const php_random_algo php_random_algo_xoshiro256starstar;
245245
extern PHPAPI const php_random_algo php_random_algo_secure;
246246
extern PHPAPI const php_random_algo php_random_algo_user;
247247

248-
# define PHP_RANDOM_ALGO_IS_DYNAMIC(algo) ((algo)->generate_size == 0)
249-
250248
typedef struct _php_random_engine {
251249
const php_random_algo *algo;
252250
php_random_status *status;

‎ext/random/random.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,13 @@ PHPAPI zend_object *php_random_engine_common_clone_object(zend_object *object)
307307
/* {{{ php_random_range */
308308
PHPAPI zend_long php_random_range(const php_random_algo *algo, php_random_status *status, zend_long min, zend_long max)
309309
{
310-
zend_ulong umax = max - min;
310+
zend_ulong umax = (zend_ulong) max - (zend_ulong) min;
311311

312-
if (PHP_RANDOM_ALGO_IS_DYNAMIC(algo) || algo->generate_size > sizeof(uint32_t) || umax > UINT32_MAX) {
313-
return (zend_long) rand_range64(algo, status, umax) + min;
312+
if (algo->generate_size == 0 || algo->generate_size > sizeof(uint32_t) || umax > UINT32_MAX) {
313+
return (zend_long) (rand_range64(algo, status, umax) + min);
314314
}
315315

316-
return (zend_long) rand_range32(algo, status, umax) + min;
316+
return (zend_long) (rand_range32(algo, status, umax) + min);
317317
}
318318
/* }}} */
319319

0 commit comments

Comments
 (0)