Skip to content

Commit 6d8b54b

Browse files
committed
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.
1 parent 1c9b3c6 commit 6d8b54b

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

ext/random/randomizer.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,11 @@ PHP_METHOD(Random_Randomizer, nextInt)
100100
ZEND_PARSE_PARAMETERS_NONE();
101101

102102
result = randomizer->algo->generate(randomizer->status);
103-
if (randomizer->status->last_generated_size > sizeof(zend_long)) {
104-
zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0);
103+
if (EG(exception)) {
105104
RETURN_THROWS();
106105
}
107-
if (EG(exception)) {
108-
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
106+
if (randomizer->status->last_generated_size > sizeof(zend_long)) {
107+
zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0);
109108
RETURN_THROWS();
110109
}
111110

ext/random/tests/03_randomizer/user_unsafe.phpt

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class EmptyStringEngine implements \Random\Engine {
1515
final class HeavilyBiasedEngine implements \Random\Engine {
1616
public function generate(): string
1717
{
18-
return "\xff\xff\xff\xff\xff\xff\xff\xff";
18+
return \str_repeat("\xff", PHP_INT_SIZE);
1919
}
2020
}
2121

@@ -32,25 +32,33 @@ foreach ([
3232
} catch (Throwable $e) {
3333
echo $e, PHP_EOL;
3434
}
35-
35+
3636
echo PHP_EOL, "-------", PHP_EOL, PHP_EOL;
37-
37+
38+
try {
39+
var_dump((new Randomizer(new $engine()))->nextInt());
40+
} catch (Throwable $e) {
41+
echo $e, PHP_EOL;
42+
}
43+
44+
echo PHP_EOL, "-------", PHP_EOL, PHP_EOL;
45+
3846
try {
3947
var_dump(bin2hex((new Randomizer(new $engine()))->getBytes(1)));
4048
} catch (Throwable $e) {
4149
echo $e, PHP_EOL;
4250
}
43-
51+
4452
echo PHP_EOL, "-------", PHP_EOL, PHP_EOL;
45-
53+
4654
try {
4755
var_dump((new Randomizer(new $engine()))->shuffleArray(\range(1, 10)));
4856
} catch (Throwable $e) {
4957
echo $e, PHP_EOL;
5058
}
51-
59+
5260
echo PHP_EOL, "-------", PHP_EOL, PHP_EOL;
53-
61+
5462
try {
5563
var_dump((new Randomizer(new $engine()))->shuffleBytes('foobar'));
5664
} catch (Throwable $e) {
@@ -73,6 +81,13 @@ Stack trace:
7381

7482
-------
7583

84+
Error: A random engine must return a non-empty string in %s:%d
85+
Stack trace:
86+
#0 %s(%d): Random\Randomizer->nextInt()
87+
#1 {main}
88+
89+
-------
90+
7691
Error: A random engine must return a non-empty string in %s:%d
7792
Stack trace:
7893
#0 %s(%d): Random\Randomizer->getBytes(1)
@@ -108,6 +123,10 @@ Stack trace:
108123

109124
-------
110125

126+
int(%d)
127+
128+
-------
129+
111130
string(2) "ff"
112131

113132
-------

0 commit comments

Comments
 (0)