Skip to content

Commit 54e5ecb

Browse files
authored
CDRIVER-4234 Detect arc4random_buf using cmake (#911)
1 parent 1523c42 commit 54e5ecb

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

src/libbson/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ else ()
8383
set (BSON_HAVE_GMTIME_R 1)
8484
endif ()
8585

86+
CHECK_SYMBOL_EXISTS (arc4random_buf stdlib.h BSON_HAVE_ARC4RANDOM_BUF)
87+
if (NOT BSON_HAVE_ARC4RANDOM_BUF)
88+
set (BSON_HAVE_ARC4RANDOM_BUF 0)
89+
else ()
90+
set (BSON_HAVE_ARC4RANDOM_BUF 1)
91+
endif ()
92+
8693
CHECK_FUNCTION_EXISTS (rand_r BSON_HAVE_RAND_R)
8794
if (NOT BSON_HAVE_RAND_R)
8895
set (BSON_HAVE_RAND_R 0)

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,28 @@
113113
# undef BSON_HAVE_SYSCALL_TID
114114
#endif
115115

116+
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+
126+
/*
127+
* Define to 1 if you have rand_r available on your platform.
128+
*/
116129
#define BSON_HAVE_RAND_R @BSON_HAVE_RAND_R@
117130
#if BSON_HAVE_RAND_R != 1
118131
# undef BSON_HAVE_RAND_R
119132
#endif
120133

134+
135+
/*
136+
* Define to 1 if you have strlcpy available on your platform.
137+
*/
121138
#define BSON_HAVE_STRLCPY @BSON_HAVE_STRLCPY@
122139
#if BSON_HAVE_STRLCPY != 1
123140
# undef BSON_HAVE_STRLCPY

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)