[hackers] [quark] fix memleak in scandir() || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Tue, 27 Jun 2017 23:31:29 +0200 (CEST)

commit b6559eb3e7b9da0096ed40aa5ddf271e9bcbb214
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
AuthorDate: Tue Jun 27 22:24:43 2017 +0200
Commit: Laslo Hunhold <dev_AT_frign.de>
CommitDate: Tue Jun 27 23:31:08 2017 +0200

    fix memleak in scandir()

diff --git a/quark.c b/quark.c
index d25bf1d..3358fff 100644
--- a/quark.c
+++ b/quark.c
_AT_@ -342,7 +342,7 @@ senddir(int fd, char *name, struct request *r)
 {
         struct dirent **e;
         size_t i;
- int dirlen;
+ int dirlen, s;
         static char t[TIMESTAMP_LEN];
 
         /* read directory */
_AT_@ -358,7 +358,8 @@ senddir(int fd, char *name, struct request *r)
                     "Content-Type: text/html\r\n"
                     "\r\n",
                     S_OK, status_str[S_OK], timestamp(0, t)) < 0) {
- return S_REQUEST_TIMEOUT;
+ s = S_REQUEST_TIMEOUT;
+ goto cleanup;
         }
 
         if (r->method == M_GET) {
_AT_@ -368,7 +369,8 @@ senddir(int fd, char *name, struct request *r)
                             "<title>Index of %s</title></head>\n"
                             "\t<body>\n\t\t<a href=\"..\">..</a>",
                             name) < 0) {
- return S_REQUEST_TIMEOUT;
+ s = S_REQUEST_TIMEOUT;
+ goto cleanup;
                 }
 
                 /* listing */
_AT_@ -381,17 +383,25 @@ senddir(int fd, char *name, struct request *r)
                         /* entry line */
                         if (dprintf(fd, "<br />\n\t\t<a href=\"%s\">%s%s</a>",
                                     e[i]->d_name, e[i]->d_name, filetype(e[i]->d_type)) < 0) {
- return S_REQUEST_TIMEOUT;
+ s = S_REQUEST_TIMEOUT;
+ goto cleanup;
                         }
                 }
 
                 /* listing footer */
                 if (dprintf(fd, "\n\t</body>\n</html>\n") < 0) {
- return S_REQUEST_TIMEOUT;
+ s = S_REQUEST_TIMEOUT;
+ goto cleanup;
                 }
         }
+ s = S_OK;
 
- return S_OK;
+cleanup:
+ while (dirlen--)
+ free(e[dirlen]);
+ free(e);
+
+ return s;
 }
 
 static enum status
Received on Tue Jun 27 2017 - 23:31:29 CEST

This archive was generated by hypermail 2.3.0 : Tue Jun 27 2017 - 23:36:55 CEST