Fix major memory leak in sftp_cmd_ls (thanks to Hans-Juergen Petrich
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Sun, 31 Mar 2002 16:26:13 +0000 (16:26 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Sun, 31 Mar 2002 16:26:13 +0000 (16:26 +0000)
for pointing it out).

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

psftp.c
sftp.c
sftp.h

diff --git a/psftp.c b/psftp.c
index 6dae40a..ae509f3 100644 (file)
--- a/psftp.c
+++ b/psftp.c
@@ -188,15 +188,15 @@ int sftp_cmd_quit(struct sftp_command *cmd)
  */
 static int sftp_ls_compare(const void *av, const void *bv)
 {
-    const struct fxp_name *a = (const struct fxp_name *) av;
-    const struct fxp_name *b = (const struct fxp_name *) bv;
-    return strcmp(a->filename, b->filename);
+    const struct fxp_name *const *a = (const struct fxp_name *const *) av;
+    const struct fxp_name *const *b = (const struct fxp_name *const *) bv;
+    return strcmp((*a)->filename, (*b)->filename);
 }
 int sftp_cmd_ls(struct sftp_command *cmd)
 {
     struct fxp_handle *dirh;
     struct fxp_names *names;
-    struct fxp_name *ournames;
+    struct fxp_name **ournames;
     int nnames, namesize;
     char *dir, *cdir;
     int i;
@@ -247,9 +247,8 @@ int sftp_cmd_ls(struct sftp_command *cmd)
            }
 
            for (i = 0; i < names->nnames; i++)
-               ournames[nnames++] = names->names[i];
+               ournames[nnames++] = fxp_dup_name(&names->names[i]);
 
-           names->nnames = 0;         /* prevent free_names */
            fxp_free_names(names);
        }
        fxp_close(dirh);
@@ -263,8 +262,11 @@ int sftp_cmd_ls(struct sftp_command *cmd)
        /*
         * And print them.
         */
-       for (i = 0; i < nnames; i++)
-           printf("%s\n", ournames[i].longname);
+       for (i = 0; i < nnames; i++) {
+           printf("%s\n", ournames[i]->longname);
+           fxp_free_name(ournames[i]);
+       }
+       sfree(ournames);
     }
 
     sfree(cdir);
diff --git a/sftp.c b/sftp.c
index 9ffd19c..e1f6017 100644 (file)
--- a/sftp.c
+++ b/sftp.c
@@ -923,3 +923,28 @@ void fxp_free_names(struct fxp_names *names)
     sfree(names->names);
     sfree(names);
 }
+
+/*
+ * Duplicate an fxp_name structure.
+ */
+struct fxp_name *fxp_dup_name(struct fxp_name *name)
+{
+    struct fxp_name *ret;
+    ret = smalloc(sizeof(struct fxp_name));
+    ret->filename = dupstr(name->filename);
+    ret->longname = dupstr(name->longname);
+    ret->attrs = name->attrs;         /* structure copy */
+    return ret;
+}
+
+/*
+ * Free up an fxp_name structure.
+ */
+void fxp_free_name(struct fxp_name *name)
+{
+    int i;
+
+    sfree(name->filename);
+    sfree(name->longname);
+    sfree(name);
+}
diff --git a/sftp.h b/sftp.h
index b48ee64..8f671ad 100644 (file)
--- a/sftp.h
+++ b/sftp.h
@@ -174,3 +174,9 @@ struct fxp_names *fxp_readdir(struct fxp_handle *handle);
  * Free up an fxp_names structure.
  */
 void fxp_free_names(struct fxp_names *names);
+
+/*
+ * Duplicate and free fxp_name structures.
+ */
+struct fxp_name *fxp_dup_name(struct fxp_name *name);
+void fxp_free_name(struct fxp_name *name);