commit b6559eb3e7b9da0096ed40aa5ddf271e9bcbb214
parent d1589c96fa7b0f4a54340682937727013fee4c84
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Tue, 27 Jun 2017 22:24:43 +0200
fix memleak in scandir()
Diffstat:
M | quark.c | | | 22 | ++++++++++++++++------ |
1 file changed, 16 insertions(+), 6 deletions(-)
diff --git a/quark.c b/quark.c
@@ -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 */
@@ -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) {
@@ -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 */
@@ -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