Skip to content

Commit

Permalink
Fix memory leak
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Oct 6, 2020
1 parent 9999253 commit 28c7e74
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
2 changes: 2 additions & 0 deletions example/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ int main(int argc, char **argv)
srv->free(srv);
free(srv);

ev_loop_destroy(loop);

return 0;
}

4 changes: 2 additions & 2 deletions src/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ static bool run_plugins(struct uh_connection *conn)
struct uh_str path = conn->get_path(conn);

while (p) {
if (strlen(p->path) == path.len && !strncmp(path.p, p->path, path.len)) {
p->handler(conn);
if (strlen(p->h->path) == path.len && !strncmp(path.p, p->h->path, path.len)) {
p->h->handler(conn);
return true;
}
p = p->next;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void test_handler(struct uh_connection *conn)
conn->chunk_end(conn);
}

struct uh_plugin uh_plugin = {
struct uh_plugin_handler uh_plugin_handler = {
.path = "/test",
.handler = test_handler
};
33 changes: 28 additions & 5 deletions src/uhttpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void conn_free(struct uh_connection *conn);
static void uh_server_free(struct uh_server *srv)
{
struct uh_connection *conn = srv->conns;
struct uh_plugin *p = srv->plugins;

ev_io_stop(srv->loop, &srv->ior);

Expand All @@ -54,6 +55,16 @@ static void uh_server_free(struct uh_server *srv)
conn = next;
}

#ifdef HAVE_DLOPEN
while (p) {
struct uh_plugin *temp = p;
dlclose(p->dlh);
p = p->next;
free(temp);
break;
}
#endif

#if UHTTPD_SSL_SUPPORT
uh_ssl_ctx_free(srv->ssl_ctx);
#endif
Expand Down Expand Up @@ -118,26 +129,38 @@ static int uh_server_ssl_init(struct uh_server *srv, const char *cert, const cha
static int uh_load_plugin(struct uh_server *srv, const char *path)
{
#ifdef HAVE_DLOPEN
struct uh_plugin_handler *h;
struct uh_plugin *p;
void *dlh;

dlh = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
dlh = dlopen(path, RTLD_NOW | RTLD_LOCAL);
if (!dlh) {
uh_log_err("dlopen fail: %s\n", dlerror());
return -1;
}

p = dlsym(dlh, "uh_plugin");
if (!p) {
uh_log_err("not found symbol 'uh_plugin'\n");
h = dlsym(dlh, "uh_plugin_handler");
if (!h) {
dlclose(dlh);
uh_log_err("not found symbol 'uh_plugin_handler'\n");
return -1;
}

if (!p->path || !p->path[0] || !p->handler) {
if (!h->path || !h->path[0] || !h->handler) {
dlclose(dlh);
uh_log_err("invalid plugin\n");
return -1;
}

p = calloc(1, sizeof(struct uh_plugin));
if (!p) {
uh_log_err("calloc: %s\n", strerror(errno));
return -1;
}

p->h = h;
p->dlh = dlh;

if (!srv->plugins) {
srv->plugins = p;
return 0;
Expand Down
7 changes: 6 additions & 1 deletion src/uhttpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@
#include "config.h"
#include "log.h"

struct uh_plugin {
struct uh_plugin_handler {
const char *path;
void (*handler)(struct uh_connection *conn);
};

struct uh_plugin {
struct uh_plugin_handler *h;
void *dlh;
struct uh_plugin *prev;
struct uh_plugin *next;
};
Expand Down

0 comments on commit 28c7e74

Please sign in to comment.