From 30d7a84e44bb478e360a31202b06045f446e11be Mon Sep 17 00:00:00 2001 From: Kent Kostelac Date: Mon, 28 Aug 2023 23:21:49 +0200 Subject: [PATCH] Handling windows exceptiuon where stdlib.h on MSYS and mingw has RAND_MAX = 0x7fff. --- .gitignore | 1 + skeletons/asn_random_fill.c | 15 ++++++++++----- skeletons/converter-example.c | 5 +++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index eb5db3010..cdfc82e9f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ *.trs *.core .tmp.* +*.exe # Generated by `autoreconf` config/ diff --git a/skeletons/asn_random_fill.c b/skeletons/asn_random_fill.c index 29653623a..15771b6c7 100644 --- a/skeletons/asn_random_fill.c +++ b/skeletons/asn_random_fill.c @@ -42,17 +42,22 @@ asn_random_between(intmax_t lb, intmax_t rb) { uintmax_t range = asn__intmax_range(lb, rb); uintmax_t value = 0; uintmax_t got_entropy = 0; - (void)intmax_max; - assert(RAND_MAX > 0xffffff); /* Seen 7ffffffd! */ + int max = 0xffffff; + +#ifdef __WIN32__ + max = RAND_MAX-1; +#endif + + assert(RAND_MAX > max); /* Seen 7ffffffd! */ assert(range < intmax_max); for(; got_entropy < range;) { - got_entropy = (got_entropy << 24) | 0xffffff; + got_entropy = (got_entropy << 24) | max; #ifdef HAVE_RANDOM - value = (value << 24) | (random() % 0xffffff); + value = (value << 24) | (random() % max); #else - value = (value << 24) | (rand() % 0xffffff); + value = (value << 24) | (rand() % max); #endif } diff --git a/skeletons/converter-example.c b/skeletons/converter-example.c index e9b01d363..5fad908ca 100644 --- a/skeletons/converter-example.c +++ b/skeletons/converter-example.c @@ -70,6 +70,11 @@ static void junk_bytes_with_probability(uint8_t *, size_t, double prob); #define RANDOPT "R:" static ssize_t random_max_size = 0; /* Size of the random data */ +#if defined(__WIN32__) && defined(JUNKTEST) +#define random rand +#define srandom srand +#endif + #if !defined(__FreeBSD__) && !(defined(__APPLE__) && defined(__MACH__)) static void srandomdev(void) {