Skip to content

Commit 687ca14

Browse files
committed
Remove ->last_unsafe from php_random_status
Whenever ->last_unsafe is set to `true` an exception has been thrown. Thus we can replace the check for `->last_unsafe` with a check for `EG(exception)` which is a much more natural way to ommunicate an error up the chain.
1 parent baebb73 commit 687ca14

File tree

8 files changed

+13
-24
lines changed

8 files changed

+13
-24
lines changed

ext/random/engine_mt19937.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ PHP_METHOD(Random_Engine_Mt19937, generate)
301301

302302
generated = engine->algo->generate(engine->status);
303303
size = engine->status->last_generated_size;
304-
if (engine->status->last_unsafe) {
304+
if (EG(exception)) {
305305
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
306306
RETURN_THROWS();
307307
}

ext/random/engine_secure.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ static uint64_t generate(php_random_status *status)
2929
{
3030
zend_ulong r = 0;
3131

32-
if (php_random_bytes_throw(&r, sizeof(zend_ulong)) == FAILURE) {
33-
status->last_unsafe = true;
34-
}
32+
php_random_bytes_throw(&r, sizeof(zend_ulong));
3533

3634
return r;
3735
}
@@ -40,9 +38,7 @@ static zend_long range(php_random_status *status, zend_long min, zend_long max)
4038
{
4139
zend_long result = 0;
4240

43-
if (php_random_int_throw(min, max, &result) == FAILURE) {
44-
status->last_unsafe = true;
45-
}
41+
php_random_int_throw(min, max, &result);
4642

4743
return result;
4844
}

ext/random/engine_user.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ static uint64_t generate(php_random_status *status)
3131
zend_call_known_instance_method_with_0_params(s->generate_method, s->object, &retval);
3232

3333
if (EG(exception)) {
34-
status->last_unsafe = true;
3534
return 0;
3635
}
3736

@@ -51,7 +50,6 @@ static uint64_t generate(php_random_status *status)
5150
}
5251
} else {
5352
zend_throw_error(NULL, "A random engine must return a non-empty string");
54-
status->last_unsafe = true;
5553
return 0;
5654
}
5755

ext/random/php_random.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ PHPAPI int php_random_int(zend_long min, zend_long max, zend_long *result, bool
201201

202202
typedef struct _php_random_status_ {
203203
size_t last_generated_size;
204-
bool last_unsafe;
205204
void *state;
206205
} php_random_status;
207206

ext/random/random.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static inline uint32_t rand_range32(const php_random_algo *algo, php_random_stat
9696
r = algo->generate(status);
9797
result = result | (r << (total_size * 8));
9898
total_size += status->last_generated_size;
99-
if (status->last_unsafe) {
99+
if (EG(exception)) {
100100
return 0;
101101
}
102102
} while (total_size < sizeof(uint32_t));
@@ -122,7 +122,6 @@ static inline uint32_t rand_range32(const php_random_algo *algo, php_random_stat
122122
/* If the requirements cannot be met in a cycles, return fail */
123123
if (++count > RANDOM_RANGE_ATTEMPTS) {
124124
zend_throw_error(NULL, "Failed to generate an acceptable random number in %d attempts", RANDOM_RANGE_ATTEMPTS);
125-
status->last_unsafe = true;
126125
return 0;
127126
}
128127

@@ -132,7 +131,7 @@ static inline uint32_t rand_range32(const php_random_algo *algo, php_random_stat
132131
r = algo->generate(status);
133132
result = result | (r << (total_size * 8));
134133
total_size += status->last_generated_size;
135-
if (status->last_unsafe) {
134+
if (EG(exception)) {
136135
return 0;
137136
}
138137
} while (total_size < sizeof(uint32_t));
@@ -153,7 +152,7 @@ static inline uint64_t rand_range64(const php_random_algo *algo, php_random_stat
153152
r = algo->generate(status);
154153
result = result | (r << (total_size * 8));
155154
total_size += status->last_generated_size;
156-
if (status->last_unsafe) {
155+
if (EG(exception)) {
157156
return 0;
158157
}
159158
} while (total_size < sizeof(uint64_t));
@@ -179,7 +178,6 @@ static inline uint64_t rand_range64(const php_random_algo *algo, php_random_stat
179178
/* If the requirements cannot be met in a cycles, return fail */
180179
if (++count > RANDOM_RANGE_ATTEMPTS) {
181180
zend_throw_error(NULL, "Failed to generate an acceptable random number in %d attempts", RANDOM_RANGE_ATTEMPTS);
182-
status->last_unsafe = true;
183181
return 0;
184182
}
185183

@@ -189,7 +187,7 @@ static inline uint64_t rand_range64(const php_random_algo *algo, php_random_stat
189187
r = algo->generate(status);
190188
result = result | (r << (total_size * 8));
191189
total_size += status->last_generated_size;
192-
if (status->last_unsafe) {
190+
if (EG(exception)) {
193191
return 0;
194192
}
195193
} while (total_size < sizeof(uint64_t));
@@ -245,7 +243,6 @@ PHPAPI php_random_status *php_random_status_alloc(const php_random_algo *algo, c
245243
php_random_status *status = pecalloc(1, sizeof(php_random_status), persistent);
246244

247245
status->last_generated_size = algo->generate_size;
248-
status->last_unsafe = false;
249246
status->state = algo->state_size > 0 ? pecalloc(1, algo->state_size, persistent) : NULL;
250247

251248
return status;
@@ -254,7 +251,6 @@ PHPAPI php_random_status *php_random_status_alloc(const php_random_algo *algo, c
254251
PHPAPI php_random_status *php_random_status_copy(const php_random_algo *algo, php_random_status *old_status, php_random_status *new_status)
255252
{
256253
new_status->last_generated_size = old_status->last_generated_size;
257-
new_status->last_unsafe = old_status->last_unsafe;
258254
new_status->state = memcpy(new_status->state, old_status->state, algo->state_size);
259255

260256
return new_status;

ext/random/randomizer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ PHP_METHOD(Random_Randomizer, getInt)
105105
zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0);
106106
RETURN_THROWS();
107107
}
108-
if (randomizer->status->last_unsafe) {
108+
if (EG(exception)) {
109109
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
110110
RETURN_THROWS();
111111
}
@@ -123,7 +123,7 @@ PHP_METHOD(Random_Randomizer, getInt)
123123
}
124124

125125
result = randomizer->algo->range(randomizer->status, min, max);
126-
if (randomizer->status->last_unsafe) {
126+
if (EG(exception)) {
127127
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
128128
RETURN_THROWS();
129129
}
@@ -155,7 +155,7 @@ PHP_METHOD(Random_Randomizer, getBytes)
155155

156156
while (total_size < required_size) {
157157
result = randomizer->algo->generate(randomizer->status);
158-
if (randomizer->status->last_unsafe) {
158+
if (EG(exception)) {
159159
zend_string_free(retval);
160160
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
161161
RETURN_THROWS();

ext/standard/array.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,7 +2936,7 @@ PHPAPI bool php_array_data_shuffle(const php_random_algo *algo, php_random_statu
29362936
}
29372937
while (--n_left) {
29382938
rnd_idx = algo->range(status, 0, n_left);
2939-
if (status->last_unsafe) {
2939+
if (EG(exception)) {
29402940
return false;
29412941
}
29422942
if (rnd_idx != n_left) {
@@ -2964,7 +2964,7 @@ PHPAPI bool php_array_data_shuffle(const php_random_algo *algo, php_random_statu
29642964
}
29652965
while (--n_left) {
29662966
rnd_idx = algo->range(status, 0, n_left);
2967-
if (status->last_unsafe) {
2967+
if (EG(exception)) {
29682968
return false;
29692969
}
29702970
if (rnd_idx != n_left) {

ext/standard/string.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5755,7 +5755,7 @@ PHPAPI bool php_binary_string_shuffle(const php_random_algo *algo, php_random_st
57555755

57565756
while (--n_left) {
57575757
rnd_idx = algo->range(status, 0, n_left);
5758-
if (status->last_unsafe) {
5758+
if (EG(exception)) {
57595759
return false;
57605760
}
57615761
if (rnd_idx != n_left) {

0 commit comments

Comments
 (0)