Replace RuntimeException in Randomizer::nextInt() by RandomException (#9305)

* Replace RuntimeException in Randomizer::nextInt() by RandomException

* Add ext/random/tests/03_randomizer/nextint_error.phpt
This commit is contained in:
Tim Düsterhus 2022-08-15 11:06:03 +02:00 committed by GitHub
parent 4b7a98754d
commit 3b48a2044d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 5 deletions

View file

@ -24,7 +24,6 @@
#include "ext/standard/php_array.h" #include "ext/standard/php_array.h"
#include "ext/standard/php_string.h" #include "ext/standard/php_string.h"
#include "ext/spl/spl_exceptions.h"
#include "Zend/zend_exceptions.h" #include "Zend/zend_exceptions.h"
static inline void randomizer_common_init(php_random_randomizer *randomizer, zend_object *engine_object) { static inline void randomizer_common_init(php_random_randomizer *randomizer, zend_object *engine_object) {
@ -102,7 +101,7 @@ PHP_METHOD(Random_Randomizer, nextInt)
RETURN_THROWS(); RETURN_THROWS();
} }
if (randomizer->status->last_generated_size > sizeof(zend_long)) { if (randomizer->status->last_generated_size > sizeof(zend_long)) {
zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0); zend_throw_exception(random_ce_Random_RandomException, "Generated value exceeds size of int", 0);
RETURN_THROWS(); RETURN_THROWS();
} }

View file

@ -31,7 +31,7 @@ foreach ($engines as $engine) {
for ($i = 0; $i < 1000; $i++) { for ($i = 0; $i < 1000; $i++) {
try { try {
$randomizer->nextInt(); $randomizer->nextInt();
} catch (\RuntimeException $e) { } catch (\Random\RandomException $e) {
if ($e->getMessage() !== 'Generated value exceeds size of int') { if ($e->getMessage() !== 'Generated value exceeds size of int') {
die($engine::class . ": nextInt: failure: {$e->getMessage()}"); die($engine::class . ": nextInt: failure: {$e->getMessage()}");
} }

View file

@ -42,7 +42,7 @@ try {
die("failure PcgOneseq128XslRr64 i: {$i} native: {$native} user: {$user}"); die("failure PcgOneseq128XslRr64 i: {$i} native: {$native} user: {$user}");
} }
} }
} catch (\RuntimeException $e) { } catch (\Random\RandomException $e) {
if ($e->getMessage() !== 'Generated value exceeds size of int') { if ($e->getMessage() !== 'Generated value exceeds size of int') {
throw $e; throw $e;
} }
@ -68,7 +68,7 @@ try {
die("failure Xoshiro256StarStar i: {$i} native: {$native} user: {$user}"); die("failure Xoshiro256StarStar i: {$i} native: {$native} user: {$user}");
} }
} }
} catch (\RuntimeException $e) { } catch (\Random\RandomException $e) {
if ($e->getMessage() !== 'Generated value exceeds size of int') { if ($e->getMessage() !== 'Generated value exceeds size of int') {
throw $e; throw $e;
} }

View file

@ -0,0 +1,18 @@
--TEST--
Random: Randomizer: nextInt() throws for too large values on 32 Bit
--SKIPIF--
<?php if (PHP_INT_SIZE == 8) die("skip 32-bit only"); ?>
--FILE--
<?php
$randomizer = new \Random\Randomizer(new \Random\Engine\Xoshiro256StarStar());
try {
var_dump($randomizer->nextInt());
} catch (\Random\RandomException $e) {
echo $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
Generated value exceeds size of int