Fix a couple of code paths on which, if fxp_readdir returned an error,
[u/mdw/putty] / psftp.c
diff --git a/psftp.c b/psftp.c
index b6a2bb5..26f24f9 100644 (file)
--- a/psftp.c
+++ b/psftp.c
@@ -291,6 +291,11 @@ int sftp_get_file(char *fname, char *outfname, int recurse, int restart)
                    if (fxp_error_type() == SSH_FX_EOF)
                        break;
                    printf("%s: reading directory: %s\n", fname, fxp_error());
+
+                    req = fxp_close_send(dirhandle);
+                    pktin = sftp_wait_for_reply(req);
+                    fxp_close_recv(pktin, req);
+
                    sfree(ournames);
                    return 0;
                }
@@ -327,7 +332,8 @@ int sftp_get_file(char *fname, char *outfname, int recurse, int restart)
             * readdirs on the same remote directory return a
             * different order.
             */
-           qsort(ournames, nnames, sizeof(*ournames), sftp_name_compare);
+            if (nnames > 0)
+                qsort(ournames, nnames, sizeof(*ournames), sftp_name_compare);
 
            /*
             * If we're in restart mode, find the last filename on
@@ -570,7 +576,8 @@ int sftp_put_file(char *fname, char *outfname, int recurse, int restart)
         * same directory, just in case two readdirs on the same
         * local directory return a different order.
         */
-       qsort(ournames, nnames, sizeof(*ournames), bare_name_compare);
+        if (nnames > 0)
+            qsort(ournames, nnames, sizeof(*ournames), bare_name_compare);
 
        /*
         * If we're in restart mode, find the last filename on this
@@ -1093,7 +1100,8 @@ int sftp_cmd_ls(struct sftp_command *cmd)
         * Now we have our filenames. Sort them by actual file
         * name, and then output the longname parts.
         */
-       qsort(ournames, nnames, sizeof(*ournames), sftp_name_compare);
+        if (nnames > 0)
+            qsort(ournames, nnames, sizeof(*ournames), sftp_name_compare);
 
        /*
         * And print them.