From b1633b2eb1d23e15c1abc2b52f66939785949ee0 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Tue, 5 Nov 2013 20:07:09 +0100 Subject: [PATCH] updated code to avoid memory leaks. --- src/main-misc.c | 3 ++- src/ocpasswd.c | 8 ++++---- src/str.c | 14 +++++++++++++- src/str.h | 1 + src/worker-vpn.c | 4 ++-- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/main-misc.c b/src/main-misc.c index 3649ef2a..215c2e44 100644 --- a/src/main-misc.c +++ b/src/main-misc.c @@ -36,6 +36,7 @@ #include #include #include "ipc.h" +#include "str.h" #include "setproctitle.h" #include #include @@ -178,7 +179,7 @@ unsigned i; cfg.routes = NULL; cfg.routes_size = 0; } else { - proc->config.routes = realloc(proc->config.routes, (proc->config.routes_size + cfg.routes_size) * sizeof(proc->config.routes[0])); + proc->config.routes = safe_realloc(proc->config.routes, (proc->config.routes_size + cfg.routes_size) * sizeof(proc->config.routes[0])); if (proc->config.routes == NULL) return ERR_MEM; diff --git a/src/ocpasswd.c b/src/ocpasswd.c index 8cced78a..3363dae0 100644 --- a/src/ocpasswd.c +++ b/src/ocpasswd.c @@ -288,7 +288,7 @@ int main(int argc, char **argv) username = argv[0]; else { fprintf(stderr, "Please specify a user\n"); - return -1; + exit(1); } if (HAVE_OPT(PASSWD)) @@ -314,18 +314,18 @@ int main(int argc, char **argv) passwd = getpass("Enter password: "); if (passwd == NULL) { fprintf(stderr, "Please specify a password\n"); - return -1; + exit(1); } p2 = strdup(passwd); passwd = getpass("Re-enter password: "); if (passwd == NULL) { fprintf(stderr, "Please specify a password\n"); - return -1; + exit(1); } if (p2 == NULL || strcmp(passwd, p2) != 0) { fprintf(stderr, "Passwords do not match\n"); - return -1; + exit(1); } free(p2); diff --git a/src/str.c b/src/str.c index ce8539af..85be0170 100644 --- a/src/str.c +++ b/src/str.c @@ -42,6 +42,18 @@ void str_clear(str_st * str) str->length = 0; } +void *safe_realloc(void *ptr, size_t size) +{ + void* tmp, *ret; + + tmp = ptr; + ret = realloc(ptr, size); + if (ret == NULL) { + free(tmp); + } + return ret; +} + #define MIN_CHUNK 64 /* This function makes sure there is an additional byte in dest; */ @@ -70,7 +82,7 @@ int str_append_size(str_st * dest, size_t data_size) MAX(data_size, MIN_CHUNK) + MAX(dest->max_length, MIN_CHUNK); - dest->allocd = realloc(dest->allocd, new_len+1); + dest->allocd = safe_realloc(dest->allocd, new_len+1); if (dest->allocd == NULL) return ERR_MEM; dest->max_length = new_len; diff --git a/src/str.h b/src/str.h index 444758c7..545a570d 100644 --- a/src/str.h +++ b/src/str.h @@ -57,6 +57,7 @@ int str_append_size(str_st *, size_t data_size); int str_append_data_prefix1(str_st *, const void *data, size_t data_size); int str_read_str_prefix1(str_st * src, char **data, size_t *data_size); int str_read_data(str_st * src, void *data, size_t data_size); +void *safe_realloc(void *ptr, size_t size); #define str_append_str_prefix1(s, str) (((str)==NULL)?str_append_data_prefix1(s, NULL, 0):str_append_data_prefix1(s, str, strlen(str))) diff --git a/src/worker-vpn.c b/src/worker-vpn.c index 9b11fe15..4ac8efcc 100644 --- a/src/worker-vpn.c +++ b/src/worker-vpn.c @@ -384,7 +384,7 @@ int body_cb(http_parser* parser, const char *at, size_t length) struct http_req_st *req = &ws->req; char* tmp; - tmp = realloc(req->body, req->body_length+length+1); + tmp = safe_realloc(req->body, req->body_length+length+1); if (tmp == NULL) return 1; @@ -1012,7 +1012,7 @@ bandwidth_st b_rx; if (ws->buffer_size <= ws->conn_mtu+mtu_overhead) { oclog(ws, LOG_WARNING, "buffer size is smaller than MTU (%u < %u); adjusting", ws->buffer_size, ws->conn_mtu); ws->buffer_size = ws->conn_mtu+mtu_overhead; - ws->buffer = realloc(ws->buffer, ws->buffer_size); + ws->buffer = safe_realloc(ws->buffer, ws->buffer_size); if (ws->buffer == NULL) goto exit; }