Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions ext/random/engine_pcgoneseq128xslrr64.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,31 @@ PHP_METHOD(Random_Engine_PcgOneseq128XslRr64, __construct)
zend_string *str_seed = NULL;
zend_long int_seed = 0;
bool seed_is_null = true;
uint32_t i, j;
uint64_t t[2];

ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL;
Z_PARAM_STR_OR_LONG_OR_NULL(str_seed, int_seed, seed_is_null);
ZEND_PARSE_PARAMETERS_END();

if (seed_is_null) {
if (php_random_bytes_throw(&state->state, sizeof(php_random_uint128_t)) == FAILURE) {
if (php_random_bytes_throw(&state->state, sizeof(state->state)) == FAILURE) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this is determined at compile time because the compiler knows the struct field type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sizeof is always compile time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is technically incorrect sizeof of a VLA is a runtime construct. :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL, but VLA are terrible anyway 😄

zend_throw_exception(random_ce_Random_RandomException, "Failed to generate a random seed", 0);
RETURN_THROWS();
}
} else {
if (str_seed) {
/* char (byte: 8 bit) * 16 = 128 bits */
if (ZSTR_LEN(str_seed) == 16) {
uint64_t t[2];

/* Endianness safe copy */
for (i = 0; i < 2; i++) {
for (uint32_t i = 0; i < 2; i++) {
t[i] = 0;
for (j = 0; j < 8; j++) {
for (uint32_t j = 0; j < 8; j++) {
t[i] += ((uint64_t) (unsigned char) ZSTR_VAL(str_seed)[(i * 8) + j]) << (j * 8);
}
}

seed128(engine->status, php_random_uint128_constant(t[0], t[1]));
} else {
zend_argument_value_error(1, "must be a 16 byte (128 bit) string");
Expand Down
2 changes: 1 addition & 1 deletion ext/random/engine_xoshiro256starstar.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ PHP_METHOD(Random_Engine_Xoshiro256StarStar, __construct)

if (seed_is_null) {
do {
if (php_random_bytes_throw(&state->state, 32) == FAILURE) {
if (php_random_bytes_throw(&state->state, sizeof(state->state)) == FAILURE) {
zend_throw_exception(random_ce_Random_RandomException, "Failed to generate a random seed", 0);
RETURN_THROWS();
}
Expand Down