Skip to content

Commit

Permalink
Avoid signed integer overflow in php_random_range() (#9066)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeriyoshi committed Jul 22, 2022
1 parent dfbe964 commit 133b9b0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ PHP NEWS

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

- SPL:
. Widen iterator_to_array() and iterator_count()'s $iterator parameter to
Expand Down
2 changes: 0 additions & 2 deletions ext/random/php_random.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,6 @@ extern PHPAPI const php_random_algo php_random_algo_xoshiro256starstar;
extern PHPAPI const php_random_algo php_random_algo_secure;
extern PHPAPI const php_random_algo php_random_algo_user;

# define PHP_RANDOM_ALGO_IS_DYNAMIC(algo) ((algo)->generate_size == 0)

typedef struct _php_random_engine {
const php_random_algo *algo;
php_random_status *status;
Expand Down
8 changes: 4 additions & 4 deletions ext/random/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,13 @@ PHPAPI zend_object *php_random_engine_common_clone_object(zend_object *object)
/* {{{ php_random_range */
PHPAPI zend_long php_random_range(const php_random_algo *algo, php_random_status *status, zend_long min, zend_long max)
{
zend_ulong umax = max - min;
zend_ulong umax = (zend_ulong) max - (zend_ulong) min;

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

return (zend_long) rand_range32(algo, status, umax) + min;
return (zend_long) (rand_range32(algo, status, umax) + min);
}
/* }}} */

Expand Down

0 comments on commit 133b9b0

Please sign in to comment.