From 79bde4e5bf84c48252cb7b76a969c13cff81fb20 Mon Sep 17 00:00:00 2001 From: Andriy Voskoboinyk Date: Thu, 4 Sep 2014 19:49:50 +0300 Subject: [PATCH 1/4] fix potential memory leak and move cleanup to the end of the function --- src/friendlist.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/friendlist.c b/src/friendlist.c index 2ef628b..f3ff622 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -130,11 +130,12 @@ static int save_blocklist(char *path) exit_toxic_err("Failed in save_blocklist", FATALERR_MEMORY); int i; + int ret = -1; int count = 0; for (i = 0; i < Blocked.max_idx; ++i) { if (count > Blocked.num_blocked) - return -1; + goto on_error; if (Blocked.list[i].active) { BlockedFriend tmp; @@ -155,17 +156,14 @@ static int save_blocklist(char *path) FILE *fp = fopen(path, "wb"); - if (fp == NULL) { - free(data); - return -1; - } + if (fp == NULL) + goto on_error; - int ret = 0; - - if (fwrite(data, len, 1, fp) != 1) - ret = -1; + if (fwrite(data, len, 1, fp) == 1) + ret = 0; fclose(fp); +on_error: free(data); return ret; } From a862874740f7778ea498cdc28220833f6a1a983e Mon Sep 17 00:00:00 2001 From: Andriy Voskoboinyk Date: Thu, 4 Sep 2014 20:43:05 +0300 Subject: [PATCH 2/4] mark exit_toxic_* functions as noreturn --- src/toxic.c | 4 ++-- src/toxic.h | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/toxic.c b/src/toxic.c index 35c59fb..4da2f68 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -102,7 +102,7 @@ static void init_signal_catchers(void) signal(SIGSEGV, catch_SIGSEGV); } -void exit_toxic_success(Tox *m) +noreturn void exit_toxic_success(Tox *m) { store_data(m, DATA_FILE); close_all_file_senders(m); @@ -124,7 +124,7 @@ void exit_toxic_success(Tox *m) exit(EXIT_SUCCESS); } -void exit_toxic_err(const char *errmsg, int errcode) +noreturn void exit_toxic_err(const char *errmsg, int errcode) { if (errmsg == NULL) errmsg = "No error message"; diff --git a/src/toxic.h b/src/toxic.h index 317c0d0..7109d25 100644 --- a/src/toxic.h +++ b/src/toxic.h @@ -36,6 +36,7 @@ #endif #include +#include #include #include @@ -85,8 +86,8 @@ typedef enum _FATAL_ERRS { Uncomment if necessary */ /* #define URXVT_FIX */ -void exit_toxic_success(Tox *m); -void exit_toxic_err(const char *errmsg, int errcode); +noreturn void exit_toxic_success(Tox *m); +noreturn void exit_toxic_err(const char *errmsg, int errcode); int store_data(Tox *m, char *path); From 667410e87906197d8438982fe1b9ace227089b56 Mon Sep 17 00:00:00 2001 From: Andriy Voskoboinyk Date: Fri, 5 Sep 2014 13:17:10 +0300 Subject: [PATCH 3/4] fix another potential memory leak --- src/line_info.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/line_info.c b/src/line_info.c index b0887e2..f216aec 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -107,15 +107,6 @@ static void line_info_root_fwd(struct history *hst) hst->line_root = tmp; } -/* adds a line_info line to queue */ -static void line_info_add_queue(struct history *hst, struct line_info *line) -{ - if (hst->queue_sz >= MAX_QUEUE) - return; - - hst->queue[hst->queue_sz++] = line; -} - /* returns ptr to queue item 0 and removes it from queue */ static struct line_info *line_info_ret_queue(struct history *hst) { @@ -140,6 +131,10 @@ void line_info_add(ToxWindow *self, char *tmstmp, char *name1, char *name2, uint uint8_t colour, const char *msg, ...) { struct history *hst = self->chatwin->hst; + + if (hst->queue_sz >= MAX_QUEUE) + return; + struct line_info *new_line = calloc(1, sizeof(struct line_info)); if (new_line == NULL) @@ -205,7 +200,7 @@ void line_info_add(ToxWindow *self, char *tmstmp, char *name1, char *name2, uint new_line->bold = bold; new_line->colour = colour; - line_info_add_queue(hst, new_line); + hst->queue[hst->queue_sz++] = new_line; } /* adds a single queue item to hst if possible. only called once per call to line_info_print() */ From 1ff97161fbfc82f90d63a96c2455295d662eae93 Mon Sep 17 00:00:00 2001 From: Andriy Voskoboinyk Date: Fri, 5 Sep 2014 14:15:25 +0300 Subject: [PATCH 4/4] temporarily revert a862874740f7778ea498cdc28220833f6a1a983e. --- src/toxic.c | 4 ++-- src/toxic.h | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/toxic.c b/src/toxic.c index 4da2f68..35c59fb 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -102,7 +102,7 @@ static void init_signal_catchers(void) signal(SIGSEGV, catch_SIGSEGV); } -noreturn void exit_toxic_success(Tox *m) +void exit_toxic_success(Tox *m) { store_data(m, DATA_FILE); close_all_file_senders(m); @@ -124,7 +124,7 @@ noreturn void exit_toxic_success(Tox *m) exit(EXIT_SUCCESS); } -noreturn void exit_toxic_err(const char *errmsg, int errcode) +void exit_toxic_err(const char *errmsg, int errcode) { if (errmsg == NULL) errmsg = "No error message"; diff --git a/src/toxic.h b/src/toxic.h index 7109d25..317c0d0 100644 --- a/src/toxic.h +++ b/src/toxic.h @@ -36,7 +36,6 @@ #endif #include -#include #include #include @@ -86,8 +85,8 @@ typedef enum _FATAL_ERRS { Uncomment if necessary */ /* #define URXVT_FIX */ -noreturn void exit_toxic_success(Tox *m); -noreturn void exit_toxic_err(const char *errmsg, int errcode); +void exit_toxic_success(Tox *m); +void exit_toxic_err(const char *errmsg, int errcode); int store_data(Tox *m, char *path);