Skip to content

Commit

Permalink
random: Do not hardcode the target type when invoking the CSPRNG (#13308
Browse files Browse the repository at this point in the history
)

Instead derive the number of bytes to retrieve from the variable that is being
filled.
  • Loading branch information
TimWolla authored Feb 2, 2024
1 parent eb23857 commit 7ed21e6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions ext/random/engine_mt19937.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ PHPAPI void php_random_mt19937_seed_default(php_random_status_state_mt19937 *sta
{
zend_long seed = 0;

if (php_random_bytes_silent(&seed, sizeof(zend_long)) == FAILURE) {
if (php_random_bytes_silent(&seed, sizeof(seed)) == FAILURE) {
seed = GENERATE_SEED();
}

Expand Down Expand Up @@ -277,7 +277,7 @@ PHP_METHOD(Random_Engine_Mt19937, __construct)

if (seed_is_null) {
/* MT19937 has a very large state, uses CSPRNG for seeding only */
if (php_random_bytes_throw(&seed, sizeof(zend_long)) == FAILURE) {
if (php_random_bytes_throw(&seed, sizeof(seed)) == FAILURE) {
zend_throw_exception(random_ce_Random_RandomException, "Failed to generate a random seed", 0);
RETURN_THROWS();
}
Expand Down
2 changes: 1 addition & 1 deletion ext/random/engine_secure.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static php_random_result generate(php_random_status *status)
{
zend_ulong r = 0;

php_random_bytes_throw(&r, sizeof(zend_ulong));
php_random_bytes_throw(&r, sizeof(r));

return (php_random_result){
.size = sizeof(zend_ulong),
Expand Down

0 comments on commit 7ed21e6

Please sign in to comment.