Skip to content

Commit 730e3d0

Browse files
committed
Issue json-c#594 - provide an OVERRIDE_GET_RANDOM_SEED cmake variable to override json_c_get_random_seed() for embedded platforms where time(NULL) doesn't work.
Example: mkdir build && cd build cmake -DOVERRIDE_GET_RANDOM_SEED='do { extern uint32_t getMsTicks(void); int ms = getMsTicks() * 433494437; return ms; } while(0)' ..
1 parent 64de4b6 commit 730e3d0

File tree

4 files changed

+9
-0
lines changed

4 files changed

+9
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ option(DISABLE_THREAD_LOCAL_STORAGE "Disable using Thread-Local Storage (HAVE_
9696
option(DISABLE_WERROR "Avoid treating compiler warnings as fatal errors." OFF)
9797
option(ENABLE_RDRAND "Enable RDRAND Hardware RNG Hash Seed." OFF)
9898
option(ENABLE_THREADING "Enable partial threading support." OFF)
99+
option(OVERRIDE_GET_RANDOM_SEED "Override json_c_get_random_seed() with custom code." OFF)
100+
99101

100102
if (UNIX OR MINGW OR CYGWIN)
101103
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ DISABLE_THREAD_LOCAL_STORAGE | Bool | Disable use of Thread-Local Storage (HAV
107107
DISABLE_WERROR | Bool | Disable use of -Werror.
108108
ENABLE_RDRAND | Bool | Enable RDRAND Hardware RNG Hash Seed.
109109
ENABLE_THREADING | Bool | Enable partial threading support.
110+
OVERRIDE_GET_RANDOM_SEED | String | A block of code to use instead of the default implementation of json_c_get_random_seed(), e.g. on embedded platforms where not even the fallback to time() works. Must be a single line.
110111

111112
Pass these options as `-D` on CMake's command-line.
112113

cmake/config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
/* Enable RDRAND Hardware RNG Hash Seed */
33
#cmakedefine ENABLE_RDRAND "@ENABLE_RDRAND@"
44

5+
/* Override json_c_get_random_seed() with custom code */
6+
#cmakedefine OVERRIDE_GET_RANDOM_SEED @OVERRIDE_GET_RANDOM_SEED@
7+
58
/* Enable partial threading support */
69
#cmakedefine ENABLE_THREADING "@@"
710

random_seed.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ static int get_time_seed(void)
276276

277277
int json_c_get_random_seed(void)
278278
{
279+
#ifdef OVERRIDE_GET_RANDOM_SEED
280+
OVERRIDE_GET_RANDOM_SEED;
281+
#endif
279282
#if defined HAVE_RDRAND && HAVE_RDRAND
280283
if (has_rdrand())
281284
return get_rdrand_seed();

0 commit comments

Comments
 (0)