Skip to content

Commit 887b98f

Browse files
committed
CDRIVER-4234 Detect arc4random_buf using cmake
Detect the availability of arc4random_buf when running cmake rather than using a hardcoded list of platforms on which it is available. Fixes build failure on Mac OS X 10.6 where it is not available. Also now make arc4random_buf the first choice to use if it is available. Previously, if rand_r was not available, the older rand would be used even if arc4random_buf was available.
1 parent 80f7105 commit 887b98f

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/libbson/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ else ()
7373
set (BSON_HAVE_GMTIME_R 1)
7474
endif ()
7575

76+
CHECK_SYMBOL_EXISTS (arc4random_buf stdlib.h BSON_HAVE_ARC4RANDOM_BUF)
77+
if (NOT BSON_HAVE_ARC4RANDOM_BUF)
78+
set (BSON_HAVE_ARC4RANDOM_BUF 0)
79+
else ()
80+
set (BSON_HAVE_ARC4RANDOM_BUF 1)
81+
endif ()
82+
7683
CHECK_FUNCTION_EXISTS (rand_r BSON_HAVE_RAND_R)
7784
if (NOT BSON_HAVE_RAND_R)
7885
set (BSON_HAVE_RAND_R 0)

src/libbson/src/bson/bson-config.h.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@
114114
#endif
115115

116116

117+
/*
118+
* Define to 1 if you have arc4random_buf available on your platform.
119+
*/
120+
#define BSON_HAVE_ARC4RANDOM_BUF @BSON_HAVE_ARC4RANDOM_BUF@
121+
#if BSON_HAVE_ARC4RANDOM_BUF != 1
122+
# undef BSON_HAVE_ARC4RANDOM_BUF
123+
#endif
124+
125+
117126
/*
118127
* Define to 1 if you have rand_r available on your platform.
119128
*/

src/libbson/src/bson/bson-context.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,14 @@ static int32_t
225225
_get_rand (unsigned int *pseed)
226226
{
227227
int32_t result = 0;
228-
#ifndef BSON_HAVE_RAND_R
228+
#ifdef BSON_HAVE_ARC4RANDOM_BUF
229+
arc4random_buf (&result, sizeof (result));
230+
#elif defined(BSON_HAVE_RAND_R)
231+
result = rand_r (pseed);
232+
#else
229233
/* ms's runtime is multithreaded by default, so no rand_r */
230234
/* no rand_r on android either */
231235
result = rand ();
232-
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
233-
defined(__OpenBSD__) || defined(__APPLE__)
234-
arc4random_buf (&result, sizeof (result));
235-
#else
236-
result = rand_r (pseed);
237236
#endif
238237
return result;
239238
}

0 commit comments

Comments
 (0)