Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 15, 2022
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
3 changes: 1 addition & 2 deletions ext/random/randomizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "ext/standard/php_array.h"
#include "ext/standard/php_string.h"

#include "ext/spl/spl_exceptions.h"
#include "Zend/zend_exceptions.h"

static inline void randomizer_common_init(php_random_randomizer *randomizer, zend_object *engine_object) {
Expand Down Expand Up @@ -102,7 +101,7 @@ PHP_METHOD(Random_Randomizer, nextInt)
RETURN_THROWS();
}
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();
}

Expand Down
2 changes: 1 addition & 1 deletion ext/random/tests/03_randomizer/basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ foreach ($engines as $engine) {
for ($i = 0; $i < 1000; $i++) {
try {
$randomizer->nextInt();
} catch (\RuntimeException $e) {
} catch (\Random\RandomException $e) {
if ($e->getMessage() !== 'Generated value exceeds size of int') {
die($engine::class . ": nextInt: failure: {$e->getMessage()}");
}
Expand Down
4 changes: 2 additions & 2 deletions ext/random/tests/03_randomizer/compatibility_user.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ try {
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') {
throw $e;
}
Expand All @@ -68,7 +68,7 @@ try {
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') {
throw $e;
}
Expand Down
18 changes: 18 additions & 0 deletions ext/random/tests/03_randomizer/nextint_error.phpt
Original file line number Diff line number Diff line change
@@ -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