Skip to content

Commit e304d97

Browse files
committed
Throw RandomException when CSPRNG fails
1 parent db53776 commit e304d97

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

ext/random/random.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
475475
/* Defer to CryptGenRandom on Windows */
476476
if (php_win32_get_random_bytes(bytes, size) == FAILURE) {
477477
if (should_throw) {
478-
zend_throw_exception(zend_ce_exception, "Failed to retrieve randomness from the operating system (BCryptGenRandom)", 0);
478+
zend_throw_exception(random_ce_Random_RandomException, "Failed to retrieve randomness from the operating system (BCryptGenRandom)", 0);
479479
}
480480
return FAILURE;
481481
}
@@ -488,7 +488,7 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
488488
*/
489489
if (CCRandomGenerateBytes(bytes, size) != kCCSuccess) {
490490
if (should_throw) {
491-
zend_throw_exception(zend_ce_exception, "Failed to retrieve randomness from the operating system (CCRandomGenerateBytes)", 0);
491+
zend_throw_exception(random_ce_Random_RandomException, "Failed to retrieve randomness from the operating system (CCRandomGenerateBytes)", 0);
492492
}
493493
return FAILURE;
494494
}
@@ -553,9 +553,9 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
553553
if (fd < 0) {
554554
if (should_throw) {
555555
if (errno != 0) {
556-
zend_throw_exception_ex(zend_ce_exception, 0, "Cannot open /dev/urandom: %s", strerror(errno));
556+
zend_throw_exception_ex(random_ce_Random_RandomException, 0, "Cannot open /dev/urandom: %s", strerror(errno));
557557
} else {
558-
zend_throw_exception_ex(zend_ce_exception, 0, "Cannot open /dev/urandom");
558+
zend_throw_exception_ex(random_ce_Random_RandomException, 0, "Cannot open /dev/urandom");
559559
}
560560
}
561561
return FAILURE;
@@ -573,9 +573,9 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
573573
close(fd);
574574
if (should_throw) {
575575
if (errno != 0) {
576-
zend_throw_exception_ex(zend_ce_exception, 0, "Error reading from /dev/urandom: %s", strerror(errno));
576+
zend_throw_exception_ex(random_ce_Random_RandomException, 0, "Error reading from /dev/urandom: %s", strerror(errno));
577577
} else {
578-
zend_throw_exception_ex(zend_ce_exception, 0, "Error reading from /dev/urandom");
578+
zend_throw_exception_ex(random_ce_Random_RandomException, 0, "Error reading from /dev/urandom");
579579
}
580580
}
581581
return FAILURE;
@@ -594,9 +594,9 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
594594
if (read_bytes < size) {
595595
if (should_throw) {
596596
if (errno != 0) {
597-
zend_throw_exception_ex(zend_ce_exception, 0, "Could not gather sufficient random data: %s", strerror(errno));
597+
zend_throw_exception_ex(random_ce_Random_RandomException, 0, "Could not gather sufficient random data: %s", strerror(errno));
598598
} else {
599-
zend_throw_exception_ex(zend_ce_exception, 0, "Could not gather sufficient random data");
599+
zend_throw_exception_ex(random_ce_Random_RandomException, 0, "Could not gather sufficient random data");
600600
}
601601
}
602602
return FAILURE;

0 commit comments

Comments
 (0)