Skip to content

Commit 570e758

Browse files
author
Mike Pall
committed
Handle old OSX/iOS without getentropy().
1 parent 60ac12e commit 570e758

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/lj_prng.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,28 @@ static PRGR libfunc_rgr;
107107
#if LJ_TARGET_LINUX
108108
/* Avoid a dependency on glibc 2.25+ and use the getrandom syscall instead. */
109109
#include <sys/syscall.h>
110-
#elif LJ_TARGET_OSX || LJ_TARGET_BSD || LJ_TARGET_SOLARIS || LJ_TARGET_CYGWIN
110+
#else
111+
112+
#if LJ_TARGET_OSX
113+
#include <Availability.h>
114+
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200 || \
115+
__IPHONE_OS_VERSION_MIN_REQUIRED >= 100000
116+
#define LJ_TARGET_HAS_GETENTROPY 1
117+
#endif
118+
#elif LJ_TARGET_BSD || LJ_TARGET_SOLARIS || LJ_TARGET_CYGWIN
119+
#define LJ_TARGET_HAS_GETENTROPY 1
120+
#endif
121+
122+
#if LJ_TARGET_HAS_GETENTROPY
111123
extern int getentropy(void *buf, size_t len);
112124
#ifdef __ELF__
113125
__attribute__((weak))
114126
#endif
115127
;
116128
#endif
117129

130+
#endif
131+
118132
/* For the /dev/urandom fallback. */
119133
#include <fcntl.h>
120134
#include <unistd.h>
@@ -181,7 +195,7 @@ int LJ_FASTCALL lj_prng_seed_secure(PRNGState *rs)
181195
if (syscall(SYS_getrandom, rs->u, sizeof(rs->u), 0) == (long)sizeof(rs->u))
182196
goto ok;
183197

184-
#elif LJ_TARGET_OSX || LJ_TARGET_BSD || LJ_TARGET_SOLARIS || LJ_TARGET_CYGWIN
198+
#elif LJ_TARGET_HAS_GETENTROPY
185199

186200
#ifdef __ELF__
187201
if (getentropy && getentropy(rs->u, sizeof(rs->u)) == 0)

0 commit comments

Comments
 (0)