summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiklas Hambüchen <mail@nh2.me>2019-06-30 14:27:20 +0200
committerNiklas Hambüchen <mail@nh2.me>2019-06-30 14:31:39 +0200
commite6914acb75514fbb2bd2e736c4157e38abdb1ec0 (patch)
tree58e054b6535e9edfddd9e71d6a6f7a9ef8f2dcfd
parente9da224bce287653f96235bd6ae02da6f8f8b219 (diff)
urandom(): Refactor CPP, clarify warnings. See #279.
* 2 identical CPP #ifdef sections were merged for easier readibility. * Warnings and comments now include more concrete explanations on in which situations urandom() isn't thread-safe and what not being thread-safe means in that case. * Added TODO on how the situation can be fixed long-term.
-rw-r--r--packages/base/src/Internal/C/vector-aux.c31
1 files changed, 22 insertions, 9 deletions
diff --git a/packages/base/src/Internal/C/vector-aux.c b/packages/base/src/Internal/C/vector-aux.c
index 08dc835..73455a5 100644
--- a/packages/base/src/Internal/C/vector-aux.c
+++ b/packages/base/src/Internal/C/vector-aux.c
@@ -932,21 +932,34 @@ int vectorScan(char * file, int* n, double**pp){
932 932
933//////////////////////////////////////////////////////////////////////////////// 933////////////////////////////////////////////////////////////////////////////////
934 934
935#if defined (__APPLE__) || (__FreeBSD__) || defined(NO_RANDOM_R)
936/* FreeBSD and Mac OS X do not provide random_r(), thread safety cannot be
937 guaranteed.
938 For FreeBSD and Mac OS X, nrand48() is much better than random().
939 See: http://www.evanjones.ca/random-thread-safe.html
940*/
941#pragma message "randomVector is not thread-safe in OSX and FreeBSD or with NO_RANDOM_R"
942#endif
943
944#if defined (__APPLE__) || (__FreeBSD__) || defined(NO_RANDOM_R) || defined(_WIN32) || defined(WIN32) 935#if defined (__APPLE__) || (__FreeBSD__) || defined(NO_RANDOM_R) || defined(_WIN32) || defined(WIN32)
945/* Windows use thread-safe random 936/* Windows use thread-safe random
946 See: http://stackoverflow.com/questions/143108/is-windows-rand-s-thread-safe 937 See: http://stackoverflow.com/questions/143108/is-windows-rand-s-thread-safe
947*/ 938*/
948#if defined (__APPLE__) || (__FreeBSD__) || defined(NO_RANDOM_R) 939#if defined (__APPLE__) || (__FreeBSD__) || defined(NO_RANDOM_R)
949 940
941/* For FreeBSD, Mac OS X, and other libcs (like `musl`) that do not provide
942 random_r(), or if the use of random_r() is explicitly disabled, thread safety
943 cannot be guaranteed.
944 As per current understanding, this should at worst lead to less "random"
945 numbers being generated, in particular
946 * if another thread somebody calls lcong48() at the same time as nrand48()
947 is called
948 * in addition to that, for glibc with NO_RANDOM_R enabled when ndrand48()
949 is called for the first time by multiple threads in parallel due to the
950 initialisation function placed within it
951 See: http://www.evanjones.ca/random-thread-safe.html
952
953 For FreeBSD and Mac OS X, nrand48() is much better than random().
954 See: http://www.evanjones.ca/random-thread-safe.html
955
956 TODO: As mentioned in the linked article, this could be fixed:
957 "the best solution for truly portable applications is to include
958 your own random number generator implementation,
959 and not rely on the system's C library".
960*/
961#pragma message "randomVector is not thread-safe in OSX and FreeBSD or with NO_RANDOM_R; this likely leads to less random numbers at worst; see http://www.evanjones.ca/random-thread-safe.html"
962
950inline double urandom() { 963inline double urandom() {
951 /* the probalility of matching will be theoretically p^3(in fact, it is not) 964 /* the probalility of matching will be theoretically p^3(in fact, it is not)
952 p is matching probalility of random(). 965 p is matching probalility of random().