struct winadj is unused now. G/c it.
[u/mdw/putty] / sshrand.c
index 43b8123..b728fd9 100644 (file)
--- a/sshrand.c
+++ b/sshrand.c
@@ -4,6 +4,7 @@
 
 #include "putty.h"
 #include "ssh.h"
+#include <assert.h>
 
 /* Collect environmental noise every 5 minutes */
 #define NOISE_REGULAR_INTERVAL (5*60*TICKSPERSEC)
@@ -40,6 +41,8 @@ struct RandPool {
 
     unsigned char incomingb[HASHINPUT];
     int incomingpos;
+
+    int stir_pending;
 };
 
 static struct RandPool pool;
@@ -52,6 +55,14 @@ static void random_stir(void)
     word32 digest[HASHSIZE / sizeof(word32)];
     int i, j, k;
 
+    /*
+     * noise_get_light will call random_add_noise, which may call
+     * back to here. Prevent recursive stirs.
+     */
+    if (pool.stir_pending)
+       return;
+    pool.stir_pending = TRUE;
+
     noise_get_light(random_add_noise);
 
     SHATransform((word32 *) pool.incoming, (word32 *) pool.incomingb);
@@ -115,6 +126,8 @@ static void random_stir(void)
     memcpy(pool.incoming, digest, sizeof(digest));
 
     pool.poolpos = sizeof(pool.incoming);
+
+    pool.stir_pending = FALSE;
 }
 
 void random_add_noise(void *noise, int length)
@@ -213,6 +226,10 @@ void random_ref(void)
 void random_unref(void)
 {
     random_active--;
+    assert(random_active >= 0);
+    if (random_active) return;
+
+    expire_timer_context(&pool);
 }
 
 int random_byte(void)