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 33fbf08..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
@@ -342,11 +348,8 @@ int sftp_get_file(char *fname, char *outfname, int recurse, int restart)
                 while (i < nnames) {
                     char *nextoutfname;
                     int ret;
-                    if (outfname)
-                        nextoutfname = dir_file_cat(outfname,
-                                                    ournames[i]->filename);
-                    else
-                        nextoutfname = dupstr(ournames[i]->filename);
+                    nextoutfname = dir_file_cat(outfname,
+                                                ournames[i]->filename);
                     ret = (file_type(nextoutfname) == FILE_TYPE_NONEXISTENT);
                     sfree(nextoutfname);
                     if (ret)
@@ -368,11 +371,7 @@ int sftp_get_file(char *fname, char *outfname, int recurse, int restart)
                int ret;
                
                nextfname = dupcat(fname, "/", ournames[i]->filename, NULL);
-               if (outfname)
-                   nextoutfname = dir_file_cat(outfname,
-                                               ournames[i]->filename);
-               else
-                   nextoutfname = dupstr(ournames[i]->filename);
+                nextoutfname = dir_file_cat(outfname, ournames[i]->filename);
                ret = sftp_get_file(nextfname, nextoutfname, recurse, restart);
                restart = FALSE;       /* after first partial file, do full */
                sfree(nextoutfname);
@@ -577,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
@@ -614,10 +614,7 @@ int sftp_put_file(char *fname, char *outfname, int recurse, int restart)
            char *nextfname, *nextoutfname;
            int ret;
 
-           if (fname)
-               nextfname = dir_file_cat(fname, ournames[i]);
-           else
-               nextfname = dupstr(ournames[i]);
+            nextfname = dir_file_cat(fname, ournames[i]);
            nextoutfname = dupcat(outfname, "/", ournames[i], NULL);
            ret = sftp_put_file(nextfname, nextoutfname, recurse, restart);
            restart = FALSE;           /* after first partial file, do full */
@@ -1103,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.