From 67f8087b6f8573c0e3426e0f22c89787a27a298e Mon Sep 17 00:00:00 2001 From: prefect Date: Sat, 4 Feb 2006 19:12:09 +0000 Subject: [PATCH] Don't hack around low RAND_MAX values when RAND_MAX is high. This was SVN commit r3469. --- source/lib/lib.cpp | 22 +++++++++++++++++++--- source/lib/sysdep/unix/udbg.cpp | 8 +++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/source/lib/lib.cpp b/source/lib/lib.cpp index 2d2775cf84..228492912b 100755 --- a/source/lib/lib.cpp +++ b/source/lib/lib.cpp @@ -515,10 +515,26 @@ int match_wildcardw(const wchar_t* s, const wchar_t* w) // return random integer in [min, max). // avoids several common pitfalls; see discussion at // http://www.azillionmonkeys.com/qed/random.html + +// Silly heuristic: glibc has RAND_MAX of 2^31 - 1 while MS CRT has 2^15-1 or something... +#if RAND_MAX < 65536 +static const uint XRAND_MAX = (RAND_MAX+1)*(RAND_MAX+1) - 1; + +uint fullrand() +{ + return rand()*(RAND_MAX+1) + rand(); +} +#else +static const uint XRAND_MAX = RAND_MAX; + +uint fullrand() +{ + return rand(); +} +#endif + uint rand(uint min, uint max) { - const uint XRAND_MAX = (RAND_MAX+1)*(RAND_MAX+1) - 1; - const uint range = (max-min); // huge interval or min > max if(range > XRAND_MAX) @@ -532,7 +548,7 @@ uint rand(uint min, uint max) // generate random number in [0, range) uint x; do - x = rand()*(RAND_MAX+1) + rand(); + x = fullrand(); while(x >= range * inv_range); x /= inv_range; diff --git a/source/lib/sysdep/unix/udbg.cpp b/source/lib/sysdep/unix/udbg.cpp index b1d91080a1..4b1e2bcfb1 100755 --- a/source/lib/sysdep/unix/udbg.cpp +++ b/source/lib/sysdep/unix/udbg.cpp @@ -392,10 +392,12 @@ LibError debug_resolve_symbol(void* ptr_of_interest, char* sym_name, char* file, h = strrchr (ctx.filename, '/'); if (h != NULL) ctx.filename = h + 1; - } - strncpy(file, ctx.filename, DBG_FILE_LEN); - file[DBG_FILE_LEN]=0; + strncpy(file, ctx.filename, DBG_FILE_LEN); + file[DBG_FILE_LEN]=0; + } + else + strcpy(file, "none"); } if (line)