[prev in list] [next in list] [prev in thread] [next in thread] 

List:       privoxy-commits
Subject:    [privoxy-commits] current jcc.c, 1.111, 1.112 jcc.h, 1.19,
From:       noreply () sourceforge ! net
Date:       2006-12-26 17:31:44
Message-ID: E1GzG9Q-0000e5-N7 () mail ! sourceforge ! net
[Download RAW message or body]

Update of /cvsroot/ijbswa/current
In directory sc8-pr-cvs2.sourceforge.net:/tmp/cvs-serv31767

Modified Files:
	jcc.c jcc.h miscutil.c 
Log Message:
Mutex protect rand() if POSIX threading
is used, warn the user if that's not possible
and stop using it on _WIN32 where it could
cause crashes.


Index: jcc.c
===================================================================
RCS file: /cvsroot/ijbswa/current/jcc.c,v
retrieving revision 1.111
retrieving revision 1.112
diff -u -d -r1.111 -r1.112
--- jcc.c	23 Dec 2006 16:15:06 -0000	1.111
+++ jcc.c	26 Dec 2006 17:31:41 -0000	1.112
@@ -33,6 +33,12 @@
  *
  * Revisions   :
  *    $Log$
+ *    Revision 1.112  2006/12/26 17:31:41  fabiankeil
+ *    Mutex protect rand() if POSIX threading
+ *    is used, warn the user if that's not possible
+ *    and stop using it on _WIN32 where it could
+ *    cause crashes.
+ *
  *    Revision 1.111  2006/12/23 16:15:06  fabiankeil
  *    Don't prevent core dumps by catching SIGABRT.
  *    It's rude and makes debugging unreasonable painful.
@@ -848,6 +854,11 @@
 #ifndef HAVE_GETHOSTBYNAME_R
 pthread_mutex_t gethostbyname_mutex;
 #endif /* ndef HAVE_GETHOSTBYNAME_R */
+
+#ifndef HAVE_RANDOM
+pthread_mutex_t rand_mutex;
+#endif /* ndef HAVE_RANDOM */
+
 #endif /* FEATURE_PTHREAD */
 
 #if defined(unix) || defined(__EMX__)
@@ -903,7 +914,7 @@
  * Function    :  sig_handler 
  *
  * Description :  Signal handler for different signals.
- *                Exit gracefully on ABRT, TERM and  INT
+ *                Exit gracefully on TERM and INT
  *                or set a flag that will cause the errlog
  *                to be reopened by the main thread on HUP.
  *
@@ -2073,9 +2084,7 @@
 #endif
 {
    int argc_pos = 0;
-#ifdef HAVE_RANDOM
-   unsigned int random_seed;
-#endif /* ifdef HAVE_RANDOM */
+   int random_seed;
 #ifdef unix
    struct passwd *pw = NULL;
    struct group *grp = NULL;
@@ -2242,11 +2251,18 @@
 #ifndef HAVE_GETHOSTBYNAME_R
    pthread_mutex_init(&gethostbyname_mutex,0);
 #endif /* ndef HAVE_GETHOSTBYNAME_R */
+
+#ifndef HAVE_RANDOM
+   pthread_mutex_init(&rand_mutex,0);
+#endif /* ndef HAVE_RANDOM */
+
 #endif /* FEATURE_PTHREAD */
 
+   random_seed = (int)time(NULL);
 #ifdef HAVE_RANDOM
-   random_seed = (unsigned int)time(NULL);
    srandom(random_seed);
+#else
+   srand(random_seed);
 #endif /* ifdef HAVE_RANDOM */
 
    /*

Index: jcc.h
===================================================================
RCS file: /cvsroot/ijbswa/current/jcc.h,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- jcc.h	6 Dec 2006 19:41:39 -0000	1.19
+++ jcc.h	26 Dec 2006 17:31:41 -0000	1.20
@@ -35,6 +35,12 @@
  *
  * Revisions   :
  *    $Log$
+ *    Revision 1.20  2006/12/26 17:31:41  fabiankeil
+ *    Mutex protect rand() if POSIX threading
+ *    is used, warn the user if that's not possible
+ *    and stop using it on _WIN32 where it could
+ *    cause crashes.
+ *
  *    Revision 1.19  2006/12/06 19:41:39  fabiankeil
  *    Privoxy is now able to run as intercepting
  *    proxy in combination with any packet filter
@@ -191,6 +197,11 @@
 #ifndef HAVE_GETHOSTBYNAME_R
 extern pthread_mutex_t gethostbyname_mutex;
 #endif /* ndef HAVE_GETHOSTBYNAME_R */
+
+#ifndef HAVE_RANDOM
+extern pthread_mutex_t rand_mutex;
+#endif /* ndef HAVE_RANDOM */
+
 #endif /* FEATURE_PTHREAD */
 
 /* Functions */

Index: miscutil.c
===================================================================
RCS file: /cvsroot/ijbswa/current/miscutil.c,v
retrieving revision 1.44
retrieving revision 1.45
diff -u -d -r1.44 -r1.45
--- miscutil.c	7 Nov 2006 12:46:43 -0000	1.44
+++ miscutil.c	26 Dec 2006 17:31:41 -0000	1.45
@@ -36,6 +36,12 @@
  *
  * Revisions   :
  *    $Log$
+ *    Revision 1.45  2006/12/26 17:31:41  fabiankeil
+ *    Mutex protect rand() if POSIX threading
+ *    is used, warn the user if that's not possible
+ *    and stop using it on _WIN32 where it could
+ *    cause crashes.
+ *
  *    Revision 1.44  2006/11/07 12:46:43  fabiankeil
  *    Silence compiler warning on NetBSD 3.1.
  *
@@ -1084,25 +1090,37 @@
 long int pick_from_range(long int range)
 {
    long int number;
-#ifndef HAVE_RANDOM
-   unsigned int weak_seed;
-
-   weak_seed = (unsigned int)((unsigned int)time(NULL) | (unsigned int)range);
-   srand(weak_seed);
+#ifdef HAVE_RANDOM
+   number = random() % range + 1; 
+#elif defined(FEATURE_PTHREAD)
+   pthread_mutex_lock(&rand_mutex);
+   number = rand() % (long int)(range + 1);
+   pthread_mutex_unlock(&rand_mutex);
+#else
+#ifdef _WIN32
    /*
-    * Some rand implementations aren't that random and return mostly
-    * lower numbers. Low entropy doesn't matter for the header times, 
-    * but higher "random" factors are prefered.
+    * On Windows and mingw32 srand() has to be called in every
+    * rand()-using thread, but can cause crashes if it's not
+    * mutex protected.
+    *
+    * Currently we don't have mutexes for mingw32, and for
+    * our purpose this cludge is probably preferable to crashes.
     */
-   number = (rand() * 12345) % (long int)(range + 1);
-   /* Overflows don't matter either, positive numbers do. */
-   if(number<0)
-   {
-      number*= -1;
-   }
+   log_error(LOG_LEVEL_INFO, "No thread-safe PRNG available? Using weak \'randomization\' factor.");
+   number = (range + GetCurrentThreadId() % range) / 2;
 #else
-   number = random() % range + 1; 
-#endif /* (ifndef HAVE_RANDOM) */
+   /*
+    * XXX: Which platforms reach this and are there
+    * better options than just using rand() and hoping
+    * that it's safe?
+    */
+   log_error(LOG_LEVEL_INFO, "No thread-safe PRNG available? Header time randomization might cause "
+      "crashes, predictable results or even combine these fine options.");
+   number = rand() % (long int)(range + 1);
+#endif /* def _WIN32 */ 
+
+#endif /* (def HAVE_RANDOM) */
+
    return (number);
 }
 


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
ijbswa-commits mailing list
ijbswa-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ijbswa-commits
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic