Skip to content

Commit

Permalink
fix memory leak issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bjin committed Nov 23, 2010
1 parent a1e65e4 commit 6517548
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
14 changes: 13 additions & 1 deletion http-connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ static void httpc_client_init(redsocks_client *client)
client->state = httpc_new;
}

static void httpc_instance_fini(redsocks_instance *instance)
{
http_auth *auth = (void*)(instance + 1);
free_null(auth->last_auth_query);
auth->last_auth_query = NULL;
}

static struct evbuffer *httpc_mkconnect(redsocks_client *client);

extern const char *auth_request_header;
Expand All @@ -56,10 +63,13 @@ static char *get_auth_request_header(struct evbuffer *buf)
char *line;
for (;;) {
line = evbuffer_readline(buf);
if (line == NULL || *line == '\0' || strchr(line, ':') == NULL)
if (line == NULL || *line == '\0' || strchr(line, ':') == NULL) {
free_null(line);
return NULL;
}
if (strncasecmp(line, auth_request_header, strlen(auth_request_header)) == 0)
return line;
free(line);
}
}

Expand Down Expand Up @@ -99,6 +109,7 @@ static void httpc_read_cb(struct bufferevent *buffev, void *_arg)
redsocks_drop_client(client);
dropped = 1;
} else {
free(line);
free_null(auth->last_auth_query);
char *ptr = auth_request;

Expand Down Expand Up @@ -272,6 +283,7 @@ relay_subsys http_connect_subsys =
.readcb = httpc_read_cb,
.writecb = httpc_write_cb,
.init = httpc_client_init,
.instance_fini = httpc_instance_fini,
};

/* vim:set tabstop=4 softtabstop=4 shiftwidth=4: */
Expand Down
23 changes: 20 additions & 3 deletions http-relay.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static int httpr_buffer_append(httpr_buffer *buff, const char *data, int len)

static void httpr_client_init(redsocks_client *client)
{
httpr_client *httpr = (void*)(client +1);
httpr_client *httpr = (void*)(client + 1);

client->state = httpr_new;
memset(httpr, 0, sizeof(*httpr));
Expand All @@ -108,21 +108,35 @@ static void httpr_client_init(redsocks_client *client)

static void httpr_client_fini(redsocks_client *client)
{
httpr_client *httpr = (void*)(client +1);
httpr_client *httpr = (void*)(client + 1);

free_null(httpr->firstline);
httpr->firstline = NULL;
free_null(httpr->host);
httpr->host = NULL;
httpr_buffer_fini(&httpr->client_buffer);
httpr_buffer_fini(&httpr->relay_buffer);
}

static void httpr_instance_fini(redsocks_instance *instance)
{
http_auth *auth = (void*)(instance + 1);
free_null(auth->last_auth_query);
auth->last_auth_query = NULL;
}

static char *get_auth_request_header(struct evbuffer *buf)
{
char *line;
for (;;) {
line = evbuffer_readline(buf);
if (line == NULL || *line == '\0' || strchr(line, ':') == NULL)
if (line == NULL || *line == '\0' || strchr(line, ':') == NULL) {
free_null(line);
return NULL;
}
if (strncasecmp(line, auth_request_header, strlen(auth_request_header)) == 0)
return line;
free(line);
}
}

Expand All @@ -136,6 +150,7 @@ static void httpr_relay_read_cb(struct bufferevent *buffev, void *_arg)

redsocks_touch_client(client);

httpr_buffer_fini(&httpr->relay_buffer);
httpr_buffer_init(&httpr->relay_buffer);

if (client->state == httpr_request_sent) {
Expand All @@ -160,6 +175,7 @@ static void httpr_relay_read_cb(struct bufferevent *buffev, void *_arg)

dropped = 1;
} else {
free(line);
char *auth_request = get_auth_request_header(buffev->input);

if (!auth_request) {
Expand Down Expand Up @@ -584,6 +600,7 @@ relay_subsys http_relay_subsys =
.readcb = httpr_relay_read_cb,
.writecb = httpr_relay_write_cb,
.client_eof = httpr_client_eof,
.instance_fini = httpr_instance_fini,
};

/* vim:set tabstop=4 softtabstop=4 shiftwidth=4: */
Expand Down
3 changes: 3 additions & 0 deletions redsocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,9 @@ static void redsocks_fini_instance(redsocks_instance *instance) {
}
}

if (instance->relay_ss->instance_fini)
instance->relay_ss->instance_fini(instance);

if (event_initialized(&instance->listener)) {
if (event_del(&instance->listener) != 0)
log_errno(LOG_WARNING, "event_del");
Expand Down
1 change: 1 addition & 0 deletions redsocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ typedef struct relay_subsys_t {
evbuffercb writecb;
void (*init)(struct redsocks_client_t *client);
void (*fini)(struct redsocks_client_t *client);
void (*instance_fini)(struct redsocks_client_t *client);
// connect_relay (if any) is called instead of redsocks_connect_relay after client connection acceptance
void (*connect_relay)(struct redsocks_client_t *client);
// client_eof is called while relay is not set up but EOF from client is received
Expand Down

0 comments on commit 6517548

Please sign in to comment.