@@ -82,12 +82,14 @@ PHP_MSHUTDOWN_FUNCTION(random)
82
82
83
83
/* {{{ */
84
84
85
- static int php_random_bytes (void * bytes , size_t size )
85
+ PHPAPI int php_random_bytes (void * bytes , size_t size , zend_bool should_throw )
86
86
{
87
87
#if PHP_WIN32
88
88
/* Defer to CryptGenRandom on Windows */
89
89
if (php_win32_get_random_bytes (bytes , size ) == FAILURE ) {
90
- zend_throw_exception (zend_ce_exception , "Could not gather sufficient random data" , 0 );
90
+ if (should_throw ) {
91
+ zend_throw_exception (zend_ce_exception , "Could not gather sufficient random data" , 0 );
92
+ }
91
93
return FAILURE ;
92
94
}
93
95
#elif HAVE_DECL_ARC4RANDOM_BUF && ((defined(__OpenBSD__ ) && OpenBSD >= 201405 ) || (defined(__NetBSD__ ) && __NetBSD_Version__ >= 700000001 ))
@@ -122,7 +124,9 @@ static int php_random_bytes(void *bytes, size_t size)
122
124
php_random_bytes should be terminated by the exception instead
123
125
of proceeding to demand more entropy.
124
126
*/
125
- zend_throw_exception (zend_ce_exception , "Could not gather sufficient random data" , errno );
127
+ if (should_throw ) {
128
+ zend_throw_exception (zend_ce_exception , "Could not gather sufficient random data" , errno );
129
+ }
126
130
return FAILURE ;
127
131
}
128
132
@@ -139,7 +143,9 @@ static int php_random_bytes(void *bytes, size_t size)
139
143
fd = open ("/dev/urandom" , O_RDONLY );
140
144
#endif
141
145
if (fd < 0 ) {
142
- zend_throw_exception (zend_ce_exception , "Cannot open source device" , 0 );
146
+ if (should_throw ) {
147
+ zend_throw_exception (zend_ce_exception , "Cannot open source device" , 0 );
148
+ }
143
149
return FAILURE ;
144
150
}
145
151
/* Does the file exist and is it a character device? */
@@ -151,7 +157,9 @@ static int php_random_bytes(void *bytes, size_t size)
151
157
# endif
152
158
) {
153
159
close (fd );
154
- zend_throw_exception (zend_ce_exception , "Error reading from source device" , 0 );
160
+ if (should_throw ) {
161
+ zend_throw_exception (zend_ce_exception , "Error reading from source device" , 0 );
162
+ }
155
163
return FAILURE ;
156
164
}
157
165
RANDOM_G (fd ) = fd ;
@@ -166,7 +174,9 @@ static int php_random_bytes(void *bytes, size_t size)
166
174
}
167
175
168
176
if (read_bytes < size ) {
169
- zend_throw_exception (zend_ce_exception , "Could not gather sufficient random data" , 0 );
177
+ if (should_throw ) {
178
+ zend_throw_exception (zend_ce_exception , "Could not gather sufficient random data" , 0 );
179
+ }
170
180
return FAILURE ;
171
181
}
172
182
#endif
@@ -193,7 +203,7 @@ PHP_FUNCTION(random_bytes)
193
203
194
204
bytes = zend_string_alloc (size , 0 );
195
205
196
- if (php_random_bytes (ZSTR_VAL (bytes ), size ) == FAILURE ) {
206
+ if (php_random_bytes_throw (ZSTR_VAL (bytes ), size ) == FAILURE ) {
197
207
zend_string_release (bytes );
198
208
return ;
199
209
}
@@ -228,7 +238,7 @@ PHP_FUNCTION(random_int)
228
238
229
239
umax = max - min ;
230
240
231
- if (php_random_bytes (& result , sizeof (result )) == FAILURE ) {
241
+ if (php_random_bytes_throw (& result , sizeof (result )) == FAILURE ) {
232
242
return ;
233
243
}
234
244
@@ -247,7 +257,7 @@ PHP_FUNCTION(random_int)
247
257
248
258
/* Discard numbers over the limit to avoid modulo bias */
249
259
while (result > limit ) {
250
- if (php_random_bytes (& result , sizeof (result )) == FAILURE ) {
260
+ if (php_random_bytes_throw (& result , sizeof (result )) == FAILURE ) {
251
261
return ;
252
262
}
253
263
}
0 commit comments