Clean up nested exceptions without value-add in ext/random (#9211)

* Remove exception in Randomizer::shuffleBytes()

The only way that `php_binary_string_shuffle` fails is when the engine itself
fails. With the currently available list of engines we have:

- Mt19937            : Infallible.
- PcgOneseq128XslRr64: Infallible.
- Xoshiro256StarStar : Infallible.
- Secure             : Practically infallible on modern systems.
                       Exception messages were cleaned up in GH-9169.
- User               : Error when returning an empty string.
                       Error when seriously biased (range() fails).
                       And whatever Throwable the userland developer decides to use.

So the existing engines are either infallible or throw an Exception/Error with
a high quality message themselves, making this exception not a value-add and
possibly confusing.

* Remove exception in Randomizer::shuffleArray()

Same reasoning as in the previous commit applies.

* Remove exception in Randomizer::getInt()

Same reasoning as in the previous commit applies.

* Remove exception in Randomizer::nextInt()

Same reasoning as in the previous commit applies, except that it won't throw on
a seriously biased user engine, as `range()` is not used.

* Remove exception in Randomizer::getBytes()

Same reasoning as in the previous commit applies.

* Remove exception in Mt19937::generate()

This implementation is shared across all native engines. Thus the same
reasoning as the previous commits applies, except that the User engine does not
use this method. Thus is only applicable to the Secure engine, which is the
only fallible native engine.

* [ci skip] Add cleanup of Randomizer exceptions to NEWS
This commit is contained in:
Tim Düsterhus 2022-08-02 17:29:36 +02:00 committed by GitHub
parent 150456eaa2
commit 54e406cc50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 52 deletions

View file

@ -100,12 +100,11 @@ PHP_METHOD(Random_Randomizer, nextInt)
ZEND_PARSE_PARAMETERS_NONE();
result = randomizer->algo->generate(randomizer->status);
if (randomizer->status->last_generated_size > sizeof(zend_long)) {
zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0);
if (EG(exception)) {
RETURN_THROWS();
}
if (EG(exception)) {
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
if (randomizer->status->last_generated_size > sizeof(zend_long)) {
zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0);
RETURN_THROWS();
}
@ -132,7 +131,6 @@ PHP_METHOD(Random_Randomizer, getInt)
result = randomizer->algo->range(randomizer->status, min, max);
if (EG(exception)) {
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
RETURN_THROWS();
}
@ -165,7 +163,6 @@ PHP_METHOD(Random_Randomizer, getBytes)
result = randomizer->algo->generate(randomizer->status);
if (EG(exception)) {
zend_string_free(retval);
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
RETURN_THROWS();
}
for (size_t i = 0; i < randomizer->status->last_generated_size; i++) {
@ -193,7 +190,6 @@ PHP_METHOD(Random_Randomizer, shuffleArray)
ZVAL_DUP(return_value, array);
if (!php_array_data_shuffle(randomizer->algo, randomizer->status, return_value)) {
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
RETURN_THROWS();
}
}
@ -215,7 +211,6 @@ PHP_METHOD(Random_Randomizer, shuffleBytes)
RETVAL_STRINGL(ZSTR_VAL(bytes), ZSTR_LEN(bytes));
if (!php_binary_string_shuffle(randomizer->algo, randomizer->status, Z_STRVAL_P(return_value), (zend_long) Z_STRLEN_P(return_value))) {
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
RETURN_THROWS();
}
}