summaryrefslogtreecommitdiff
path: root/evutil_rand.c
diff options
context:
space:
mode:
authorGreg Hazel <ghazel@gmail.com>2012-05-29 12:39:12 -0700
committerNick Mathewson <nickm@torproject.org>2012-05-31 00:48:24 -0400
commitbff5f9407341030c77a29fc1c398083ccdb4e277 (patch)
tree794fddf375f71e79800640bc90edceff4e3b8c49 /evutil_rand.c
parentfaa92253e96172a4b2002a6bbc5385a7d710238f (diff)
downloadlibevent-bff5f9407341030c77a29fc1c398083ccdb4e277.tar.gz
check for arc4random_buf at runtime, on OS X
(Tweaked by nickm: Fix up the arcr4andom_buf OSX hack so that the fallback case isn't compiled into the code when we have arc4random_buf() and we are not on OSX. Also add a comment explaining what's up.)
Diffstat (limited to 'evutil_rand.c')
-rw-r--r--evutil_rand.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/evutil_rand.c b/evutil_rand.c
index 3cc163d3..e2bf605f 100644
--- a/evutil_rand.c
+++ b/evutil_rand.c
@@ -58,10 +58,22 @@ evutil_secure_rng_global_setup_locks_(const int enable_locks)
static void
ev_arc4random_buf(void *buf, size_t n)
{
-#ifdef _EVENT_HAVE_ARC4RANDOM_BUF
+ unsigned char *b = buf;
+#if defined(_EVENT_HAVE_ARC4RANDOM_BUF) && !defined(__APPLE__)
return arc4random_buf(buf, n);
#else
- unsigned char *b = buf;
+
+#if defined(_EVENT_HAVE_ARC4RANDOM_BUF)
+ /* OSX 10.7 introducd arc4random_buf, so if you build your program
+ * there, you'll get surprised when older versions of OSX fail to run.
+ * To solve this, we can check whether the function pointer is set,
+ * and fall back otherwise. (OSX does this using some linker
+ * trickery.)
+ */
+ if (arc4random_buf) {
+ return arc4random_buf(buf, n);
+ }
+#endif
/* Make sure that we start out with b at a 4-byte alignment; plenty
* of CPUs care about this for 32-bit access. */
if (n >= 4 && ((ev_uintptr_t)b) & 3) {