26
26
27
27
#include "php.h"
28
28
29
- #include "ext/spl/spl_exceptions.h"
30
29
#include "Zend/zend_exceptions.h"
31
30
32
31
#include "php_random.h"
@@ -74,8 +73,13 @@ PHPAPI zend_class_entry *random_ce_Random_Engine_Mt19937;
74
73
PHPAPI zend_class_entry * random_ce_Random_Engine_PcgOneseq128XslRr64 ;
75
74
PHPAPI zend_class_entry * random_ce_Random_Engine_Xoshiro256StarStar ;
76
75
PHPAPI zend_class_entry * random_ce_Random_Engine_Secure ;
76
+
77
77
PHPAPI zend_class_entry * random_ce_Random_Randomizer ;
78
78
79
+ PHPAPI zend_class_entry * random_ce_Random_RandomError ;
80
+ PHPAPI zend_class_entry * random_ce_Random_BrokenRandomEngineError ;
81
+ PHPAPI zend_class_entry * random_ce_Random_RandomException ;
82
+
79
83
static zend_object_handlers random_engine_mt19937_object_handlers ;
80
84
static zend_object_handlers random_engine_pcgoneseq128xslrr64_object_handlers ;
81
85
static zend_object_handlers random_engine_xoshiro256starstar_object_handlers ;
@@ -121,7 +125,7 @@ static inline uint32_t rand_range32(const php_random_algo *algo, php_random_stat
121
125
while (UNEXPECTED (result > limit )) {
122
126
/* If the requirements cannot be met in a cycles, return fail */
123
127
if (++ count > RANDOM_RANGE_ATTEMPTS ) {
124
- zend_throw_error (NULL , "Failed to generate an acceptable random number in %d attempts" , RANDOM_RANGE_ATTEMPTS );
128
+ zend_throw_error (random_ce_Random_BrokenRandomEngineError , "Failed to generate an acceptable random number in %d attempts" , RANDOM_RANGE_ATTEMPTS );
125
129
return 0 ;
126
130
}
127
131
@@ -177,7 +181,7 @@ static inline uint64_t rand_range64(const php_random_algo *algo, php_random_stat
177
181
while (UNEXPECTED (result > limit )) {
178
182
/* If the requirements cannot be met in a cycles, return fail */
179
183
if (++ count > RANDOM_RANGE_ATTEMPTS ) {
180
- zend_throw_error (NULL , "Failed to generate an acceptable random number in %d attempts" , RANDOM_RANGE_ATTEMPTS );
184
+ zend_throw_error (random_ce_Random_BrokenRandomEngineError , "Failed to generate an acceptable random number in %d attempts" , RANDOM_RANGE_ATTEMPTS );
181
185
return 0 ;
182
186
}
183
187
@@ -470,7 +474,7 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
470
474
/* Defer to CryptGenRandom on Windows */
471
475
if (php_win32_get_random_bytes (bytes , size ) == FAILURE ) {
472
476
if (should_throw ) {
473
- zend_throw_exception (zend_ce_exception , "Failed to retrieve randomness from the operating system (BCryptGenRandom)" , 0 );
477
+ zend_throw_exception (random_ce_Random_RandomException , "Failed to retrieve randomness from the operating system (BCryptGenRandom)" , 0 );
474
478
}
475
479
return FAILURE ;
476
480
}
@@ -483,7 +487,7 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
483
487
*/
484
488
if (CCRandomGenerateBytes (bytes , size ) != kCCSuccess ) {
485
489
if (should_throw ) {
486
- zend_throw_exception (zend_ce_exception , "Failed to retrieve randomness from the operating system (CCRandomGenerateBytes)" , 0 );
490
+ zend_throw_exception (random_ce_Random_RandomException , "Failed to retrieve randomness from the operating system (CCRandomGenerateBytes)" , 0 );
487
491
}
488
492
return FAILURE ;
489
493
}
@@ -548,9 +552,9 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
548
552
if (fd < 0 ) {
549
553
if (should_throw ) {
550
554
if (errno != 0 ) {
551
- zend_throw_exception_ex (zend_ce_exception , 0 , "Cannot open /dev/urandom: %s" , strerror (errno ));
555
+ zend_throw_exception_ex (random_ce_Random_RandomException , 0 , "Cannot open /dev/urandom: %s" , strerror (errno ));
552
556
} else {
553
- zend_throw_exception_ex (zend_ce_exception , 0 , "Cannot open /dev/urandom" );
557
+ zend_throw_exception_ex (random_ce_Random_RandomException , 0 , "Cannot open /dev/urandom" );
554
558
}
555
559
}
556
560
return FAILURE ;
@@ -568,9 +572,9 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
568
572
close (fd );
569
573
if (should_throw ) {
570
574
if (errno != 0 ) {
571
- zend_throw_exception_ex (zend_ce_exception , 0 , "Error reading from /dev/urandom: %s" , strerror (errno ));
575
+ zend_throw_exception_ex (random_ce_Random_RandomException , 0 , "Error reading from /dev/urandom: %s" , strerror (errno ));
572
576
} else {
573
- zend_throw_exception_ex (zend_ce_exception , 0 , "Error reading from /dev/urandom" );
577
+ zend_throw_exception_ex (random_ce_Random_RandomException , 0 , "Error reading from /dev/urandom" );
574
578
}
575
579
}
576
580
return FAILURE ;
@@ -589,9 +593,9 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
589
593
if (read_bytes < size ) {
590
594
if (should_throw ) {
591
595
if (errno != 0 ) {
592
- zend_throw_exception_ex (zend_ce_exception , 0 , "Could not gather sufficient random data: %s" , strerror (errno ));
596
+ zend_throw_exception_ex (random_ce_Random_RandomException , 0 , "Could not gather sufficient random data: %s" , strerror (errno ));
593
597
} else {
594
- zend_throw_exception_ex (zend_ce_exception , 0 , "Could not gather sufficient random data" );
598
+ zend_throw_exception_ex (random_ce_Random_RandomException , 0 , "Could not gather sufficient random data" );
595
599
}
596
600
}
597
601
return FAILURE ;
@@ -832,6 +836,15 @@ PHP_MINIT_FUNCTION(random)
832
836
/* Random\CryptoSafeEngine */
833
837
random_ce_Random_CryptoSafeEngine = register_class_Random_CryptoSafeEngine (random_ce_Random_Engine );
834
838
839
+ /* Random\RandomError */
840
+ random_ce_Random_RandomError = register_class_Random_RandomError (zend_ce_error );
841
+
842
+ /* Random\BrokenRandomEngineError */
843
+ random_ce_Random_BrokenRandomEngineError = register_class_Random_BrokenRandomEngineError (random_ce_Random_RandomError );
844
+
845
+ /* Random\RandomException */
846
+ random_ce_Random_RandomException = register_class_Random_RandomException (zend_ce_exception );
847
+
835
848
/* Random\Engine\Mt19937 */
836
849
random_ce_Random_Engine_Mt19937 = register_class_Random_Engine_Mt19937 (random_ce_Random_Engine );
837
850
random_ce_Random_Engine_Mt19937 -> create_object = php_random_engine_mt19937_new ;
0 commit comments