Skip to content

Commit 98881f3

Browse files
committed
adding code doc
1 parent af551f6 commit 98881f3

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

ext/random/random.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,14 +506,26 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
506506
}
507507
#elif HAVE_DECL_ARC4RANDOM_BUF && ((defined(__OpenBSD__) && OpenBSD >= 201405) || (defined(__NetBSD__) && __NetBSD_Version__ >= 700000001 && __NetBSD_Version__ < 1000000000) || \
508508
defined(__APPLE__) || defined(__GLIBC__))
509+
/*
510+
* OpenBSD until there is a valid equivalent
511+
* or NetBSD before the 10.x release
512+
* falls back to arc4random_buf
513+
* giving a decent output, the main benefit
514+
* is being (realtively) failsafe.
515+
* Older macOs releases fall also into this
516+
* category for reasons explained above.
517+
*/
509518
arc4random_buf(bytes, size);
510519
#else
511520
size_t read_bytes = 0;
512521
ssize_t n;
513522
# if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || (defined(__DragonFly__) && __DragonFly_version >= 500700) || \
514523
defined(__sun) || (defined(__NetBSD__) && __NetBSD_Version__ >= 1000000000)
515-
/* Linux getrandom(2) syscall or FreeBSD/DragonFlyBSD getrandom(2) function*/
516-
/* Keep reading until we get enough entropy */
524+
/* Linux getrandom(2) syscall or FreeBSD/DragonFlyBSD/NetBSD getrandom(2) function
525+
* Being a syscall, implemented in the kernel, getrandom offers higher quality output
526+
* compared to the arc4random api albeit a fallback to /dev/urandom is considered.
527+
* Keep reading until we get enough entropy
528+
*/
517529
while (read_bytes < size) {
518530
errno = 0;
519531

0 commit comments

Comments
 (0)