It's not actually legal by the C standard to call qsort with a null
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Thu, 11 Jul 2013 17:24:39 +0000 (17:24 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Thu, 11 Jul 2013 17:24:39 +0000 (17:24 +0000)
array pointer, _even_ if you're asking it to sort zero elements so
that in principle it should never dereference that pointer. Fix the
four instances in PSCP/PSFTP where this was previously occurring.

git-svn-id: svn://svn.tartarus.org/sgt/putty@9912 cda61777-01e9-0310-a592-d414129be87e

pscp.c
psftp.c

diff --git a/pscp.c b/pscp.c
index 93111d3..0dbe1e3 100644 (file)
--- a/pscp.c
+++ b/pscp.c
@@ -765,7 +765,8 @@ void scp_sftp_listdir(char *dirname)
         * Now we have our filenames. Sort them by actual file
         * name, and then output the longname parts.
         */
-       qsort(ournames, nnames, sizeof(*ournames), sftp_ls_compare);
+        if (nnames > 0)
+            qsort(ournames, nnames, sizeof(*ournames), sftp_ls_compare);
 
        /*
         * And print them.
diff --git a/psftp.c b/psftp.c
index b6a2bb5..65d0dc5 100644 (file)
--- a/psftp.c
+++ b/psftp.c
@@ -327,7 +327,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 +571,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 +1095,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.