Skip to content

Commit f15cfba

Browse files
committed
Merge branch 'PHP-8.0' into PHP-8.1
2 parents aadb24e + d830a1f commit f15cfba

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

NEWS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ PHP NEWS
3939
modifier). (Pierrick)
4040

4141
- Standard:
42-
. Fixed the crypt_sha256/512 api build with clang > 12. (David Carier)
42+
. Fixed the crypt_sha256/512 api build with clang > 12. (David Carlier)
43+
. Uses CCRandomGenerateBytes instead of arc4random_buf on macOs. (David Carlier).
4344

4445
07 Jul 2022, PHP 8.1.8
4546

ext/standard/config.m4

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,12 @@ dnl Check for arc4random on BSD systems
406406
dnl
407407
AC_CHECK_DECLS([arc4random_buf])
408408

409+
dnl
410+
dnl Check for CCRandomGenerateBytes
411+
dnl header absent in previous macOs releases
412+
dnl
413+
AC_CHECK_HEADERS([CommonCrypto/CommonRandom.h])
414+
409415
dnl
410416
dnl Check for argon2
411417
dnl

ext/standard/random.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
# include <sys/random.h>
3636
# endif
3737
#endif
38+
#if HAVE_COMMONCRYPTO_COMMONRANDOM_H
39+
# include <CommonCrypto/CommonCryptoError.h>
40+
# include <CommonCrypto/CommonRandom.h>
41+
#endif
3842

3943
#if __has_feature(memory_sanitizer)
4044
# include <sanitizer/msan_interface.h>
@@ -94,6 +98,19 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
9498
}
9599
return FAILURE;
96100
}
101+
#elif HAVE_COMMONCRYPTO_COMMONRANDOM_H
102+
/*
103+
* Purposely prioritized upon arc4random_buf for modern macOs releases
104+
* arc4random api on this platform uses `ccrng_generate` which returns
105+
* a status but silented to respect the "no fail" arc4random api interface
106+
* the vast majority of the time, it works fine ; but better make sure we catch failures
107+
*/
108+
if (CCRandomGenerateBytes(bytes, size) != kCCSuccess) {
109+
if (should_throw) {
110+
zend_throw_exception(zend_ce_exception, "Error generating bytes", 0);
111+
}
112+
return FAILURE;
113+
}
97114
#elif HAVE_DECL_ARC4RANDOM_BUF && ((defined(__OpenBSD__) && OpenBSD >= 201405) || (defined(__NetBSD__) && __NetBSD_Version__ >= 700000001) || defined(__APPLE__))
98115
arc4random_buf(bytes, size);
99116
#else

0 commit comments

Comments
 (0)