Get rid of the variable 'advapi' in Pageant's WinMain, which was never
[u/mdw/putty] / windows / winpgnt.c
index e018159..6791160 100644 (file)
@@ -353,28 +353,27 @@ static void keylist_update(void)
                               0, (LPARAM) listentry);
        }
        for (i = 0; NULL != (skey = index234(ssh2keys, i)); i++) {
-           char listentry[512], *p;
-           int len;
+           char *listentry, *p;
+           int fp_len;
            /*
             * Replace two spaces in the fingerprint with tabs, for
             * nice alignment in the box.
             */
            p = skey->alg->fingerprint(skey->data);
-           strncpy(listentry, p, sizeof(listentry));
+            listentry = dupprintf("%s\t%s", p, skey->comment);
+            fp_len = strlen(listentry);
+            sfree(p);
+
            p = strchr(listentry, ' ');
-           if (p)
+           if (p && p < listentry + fp_len)
                *p = '\t';
            p = strchr(listentry, ' ');
-           if (p)
+           if (p && p < listentry + fp_len)
                *p = '\t';
-           len = strlen(listentry);
-           if (len < sizeof(listentry) - 2) {
-               listentry[len] = '\t';
-               strncpy(listentry + len + 1, skey->comment,
-                       sizeof(listentry) - len - 1);
-           }
+
            SendDlgItemMessage(keylist, 100, LB_ADDSTRING, 0,
                               (LPARAM) listentry);
+            sfree(listentry);
        }
        SendDlgItemMessage(keylist, 100, LB_SETCURSEL, (WPARAM) - 1, 0);
     }
@@ -812,8 +811,10 @@ static void *get_keylist1(int *length)
        retval = agent_query(request, 5, &vresponse, &resplen, NULL, NULL);
        assert(retval == 1);
        response = vresponse;
-       if (resplen < 5 || response[4] != SSH1_AGENT_RSA_IDENTITIES_ANSWER)
+       if (resplen < 5 || response[4] != SSH1_AGENT_RSA_IDENTITIES_ANSWER) {
+            sfree(response);
            return NULL;
+        }
 
        ret = snewn(resplen-5, unsigned char);
        memcpy(ret, response+5, resplen-5);
@@ -847,8 +848,10 @@ static void *get_keylist2(int *length)
        retval = agent_query(request, 5, &vresponse, &resplen, NULL, NULL);
        assert(retval == 1);
        response = vresponse;
-       if (resplen < 5 || response[4] != SSH2_AGENT_IDENTITIES_ANSWER)
+       if (resplen < 5 || response[4] != SSH2_AGENT_IDENTITIES_ANSWER) {
+            sfree(response);
            return NULL;
+        }
 
        ret = snewn(resplen-5, unsigned char);
        memcpy(ret, response+5, resplen-5);
@@ -943,12 +946,17 @@ static void answer_msg(void *msg)
                goto failure;
            p += i;
            i = ssh1_read_bignum(p, msgend - p, &reqkey.modulus);
-           if (i < 0)
+           if (i < 0) {
+                freebn(reqkey.exponent);
                goto failure;
+            }
            p += i;
            i = ssh1_read_bignum(p, msgend - p, &challenge);
-           if (i < 0)
+           if (i < 0) {
+                freebn(reqkey.exponent);
+                freebn(reqkey.modulus);
                goto failure;
+            }
            p += i;
            if (msgend < p+16) {
                freebn(reqkey.exponent);
@@ -1438,10 +1446,12 @@ static void prompt_add_keyfile(void)
     of.lpstrTitle = "Select Private Key File";
     of.Flags = OFN_ALLOWMULTISELECT | OFN_EXPLORER;
     if (request_file(keypath, &of, TRUE, FALSE)) {
-       if(strlen(filelist) > of.nFileOffset)
+       if(strlen(filelist) > of.nFileOffset) {
            /* Only one filename returned? */
-           add_keyfile(filename_from_str(filelist));
-       else {
+            Filename *fn = filename_from_str(filelist);
+           add_keyfile(fn);
+            filename_free(fn);
+        } else {
            /* we are returned a bunch of strings, end to
             * end. first string is the directory, the
             * rest the filenames. terminated with an
@@ -1451,7 +1461,9 @@ static void prompt_add_keyfile(void)
            char *filewalker = filelist + strlen(dir) + 1;
            while (*filewalker != '\0') {
                char *filename = dupcat(dir, "\\", filewalker, NULL);
-               add_keyfile(filename_from_str(filename));
+                Filename *fn = filename_from_str(filename);
+               add_keyfile(fn);
+                filename_free(fn);
                sfree(filename);
                filewalker += strlen(filewalker) + 1;
            }
@@ -1909,6 +1921,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
 #ifdef DEBUG_IPC
                        debug(("couldn't get user SID\n"));
 #endif
+                        CloseHandle(filemap);
                        return 0;
                     }
 
@@ -1916,6 +1929,8 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
 #ifdef DEBUG_IPC
                        debug(("couldn't get default SID\n"));
 #endif
+                        CloseHandle(filemap);
+                        sfree(ourself);
                        return 0;
                     }
 
@@ -1927,6 +1942,9 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
                        debug(("couldn't get owner info for filemap: %d\n",
                                rc));
 #endif
+                        CloseHandle(filemap);
+                        sfree(ourself);
+                        sfree(ourself2);
                        return 0;
                    }
 #ifdef DEBUG_IPC
@@ -1945,6 +1963,9 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
                    if (!EqualSid(mapowner, ourself) &&
                         !EqualSid(mapowner, ourself2)) {
                         CloseHandle(filemap);
+                        LocalFree(psd);
+                        sfree(ourself);
+                        sfree(ourself2);
                        return 0;      /* security ID mismatch! */
                     }
 #ifdef DEBUG_IPC
@@ -2018,7 +2039,6 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
 {
     WNDCLASS wndclass;
     MSG msg;
-    HMODULE advapi;
     char *command = NULL;
     int added_keys = 0;
     int argc, i;
@@ -2059,8 +2079,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
                   "Pageant Fatal Error", MB_ICONERROR | MB_OK);
        return 1;
 #endif
-    } else
-       advapi = NULL;
+    }
 
     /*
      * See if we can find our Help file.
@@ -2113,8 +2132,6 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
     for (i = 0; i < argc; i++) {
        if (!strcmp(argv[i], "-pgpfp")) {
            pgp_fingerprints();
-           if (advapi)
-               FreeLibrary(advapi);
            return 1;
        } else if (!strcmp(argv[i], "-c")) {
            /*
@@ -2128,7 +2145,9 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
                command = "";
            break;
        } else {
-           add_keyfile(filename_from_str(argv[i]));
+            Filename *fn = filename_from_str(argv[i]);
+           add_keyfile(fn);
+            filename_free(fn);
            added_keys = TRUE;
        }
     }
@@ -2162,8 +2181,6 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
            MessageBox(NULL, "Pageant is already running", "Pageant Error",
                       MB_ICONERROR | MB_OK);
        }
-       if (advapi)
-           FreeLibrary(advapi);
        return 0;
     }
 
@@ -2243,9 +2260,6 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
 
     if (keypath) filereq_free(keypath);
 
-    if (advapi)
-       FreeLibrary(advapi);
-
     cleanup_exit(msg.wParam);
     return msg.wParam;                /* just in case optimiser complains */
 }