Skip to content

Commit 77f99cc

Browse files
committed
Use arc4random where present
1 parent 513d5c9 commit 77f99cc

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

ext/standard/config.m4

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,11 @@ dnl Check for atomic operation API availability in Solaris
592592
dnl
593593
AC_CHECK_HEADERS([atomic.h])
594594

595+
dnl
596+
dnl Check for arc4random on BSD systems
597+
dnl
598+
AC_CHECK_DECLS([arc4random_buf])
599+
595600
dnl
596601
dnl Setup extension sources
597602
dnl

ext/standard/random.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ static int php_random_bytes(void *bytes, size_t size)
3939
php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data");
4040
return FAILURE;
4141
}
42+
#else
43+
#if HAVE_DECL_ARC4RANDOM_BUF
44+
arc4random_buf(bytes, size);
4245
#else
4346
int fd = -1;
4447
size_t read_bytes = 0;
@@ -47,8 +50,8 @@ static int php_random_bytes(void *bytes, size_t size)
4750
#else
4851
#if HAVE_DEV_URANDOM
4952
fd = open("/dev/urandom", O_RDONLY);
50-
#endif
51-
#endif
53+
#endif // URANDOM
54+
#endif // ARANDOM
5255
if (fd < 0) {
5356
php_error_docref(NULL, E_WARNING, "Cannot open source device");
5457
return FAILURE;
@@ -67,7 +70,8 @@ static int php_random_bytes(void *bytes, size_t size)
6770
php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data");
6871
return FAILURE;
6972
}
70-
#endif
73+
#endif // !ARC4RANDOM_BUF
74+
#endif // !WIN32
7175

7276
return SUCCESS;
7377
}

0 commit comments

Comments
 (0)