Fix a segfault (addr->error was unpredictable following a dotted-dec lookup)
[u/mdw/putty] / puttygen.c
index 78e6465..2d6653a 100644 (file)
@@ -6,6 +6,7 @@
 #include <commctrl.h>
 #include <time.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 #define PUTTY_DO_GLOBALS
 
@@ -321,6 +322,8 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
       case WM_INITDIALOG:
         state = malloc(sizeof(*state));
         state->generation_thread_exists = FALSE;
+        state->collecting_entropy = FALSE;
+        state->entropy = NULL;
         state->key_exists = FALSE;
         SetWindowLong(hwnd, GWL_USERDATA, (LONG)state);
         {
@@ -384,7 +387,9 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
        return 1;
       case WM_MOUSEMOVE:
         state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
-        if (state->collecting_entropy) {
+        if (state->collecting_entropy &&
+            state->entropy &&
+            state->entropy_got < state->entropy_required) {
             state->entropy[state->entropy_got++] = lParam;
             state->entropy[state->entropy_got++] = GetMessageTime();
             SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS,
@@ -399,6 +404,7 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
                 random_add_heavynoise(state->entropy, state->entropy_size);
                 memset(state->entropy, 0, state->entropy_size);
                 free(state->entropy);
+                state->collecting_entropy = FALSE;
 
                 SetDlgItemText(hwnd, IDC_GENERATING, generating_msg);
                 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
@@ -420,7 +426,6 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
                     free(params);
                 } else {
                     state->generation_thread_exists = TRUE;
-                    state->collecting_entropy = FALSE;
                 }
             }
         }
@@ -450,6 +455,22 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
           case IDC_GENERATE:
             state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
             if (!state->generation_thread_exists) {
+                BOOL ok;
+                state->keysize = GetDlgItemInt(hwnd, IDC_BITS,
+                                               &ok, FALSE);
+                if (!ok) state->keysize = DEFAULT_KEYSIZE;
+                if (state->keysize < 256) {
+                    int ret = MessageBox(hwnd,
+                                         "PuTTYgen will not generate a key"
+                                         " smaller than 256 bits.\n"
+                                         "Key length reset to 256. Continue?",
+                                         "PuTTYgen Warning",
+                                         MB_ICONWARNING | MB_OKCANCEL);
+                    if (ret != IDOK)
+                        break;
+                    state->keysize = 256;
+                    SetDlgItemInt(hwnd, IDC_BITS, 256, FALSE);
+                }
                 hidemany(hwnd, nokey_ids, TRUE);
                 hidemany(hwnd, generating_ids, FALSE);
                 hidemany(hwnd, gotkey_ids, TRUE);
@@ -459,12 +480,6 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
                 state->key_exists = FALSE;
                 SetDlgItemText(hwnd, IDC_GENERATING, entropy_msg);
                 state->collecting_entropy = TRUE;
-               {
-                   BOOL ok;
-                   state->keysize = GetDlgItemInt(hwnd, IDC_BITS,
-                                                  &ok, FALSE);
-                   if (!ok) state->keysize = DEFAULT_KEYSIZE;
-               }
 
                 /*
                  * My brief statistical tests on mouse movements