[glib] grand: Only use rand_s() when targetting Visual Studio >= 2005



commit bb6a77afa328f16568a41b26de75f255dcfc9daf
Author: Sebastian Dröge <sebastian centricular com>
Date:   Sat Sep 13 16:31:03 2014 +0300

    grand: Only use rand_s() when targetting Visual Studio >= 2005
    
    It did not exist before. Fall back to the current time plus
    process id on older targets. This makes GLib work again on
    Windows XP.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=736458

 glib/grand.c |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)
---
diff --git a/glib/grand.c b/glib/grand.c
index 6262cd2..ac1053d 100644
--- a/glib/grand.c
+++ b/glib/grand.c
@@ -56,6 +56,7 @@
 
 #ifdef G_OS_WIN32
 #include <stdlib.h>
+#include <process.h> /* For getpid() */
 #endif
 
 /**
@@ -261,10 +262,23 @@ g_rand_new (void)
       seed[3] = getppid ();
     }
 #else /* G_OS_WIN32 */
+  /* rand_s() is only available since Visual Studio 2005 */
+#if defined(_MSC_VER) && _MSC_VER >= 1400
   gint i;
 
   for (i = 0; i < G_N_ELEMENTS (seed); i++)
     rand_s (&seed[i]);
+#else
+#warning Using insecure seed for random number generation because of missing rand_s() in Windows XP
+  GTimeVal now;
+
+  g_get_current_time (&now);
+  seed[0] = now.tv_sec;
+  seed[1] = now.tv_usec;
+  seed[2] = getpid ();
+  seed[3] = 0;
+#endif
+
 #endif
 
   return g_rand_new_with_seed_array (seed, 4);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]