From 79bde4e5bf84c48252cb7b76a969c13cff81fb20 Mon Sep 17 00:00:00 2001 From: Andriy Voskoboinyk Date: Thu, 4 Sep 2014 19:49:50 +0300 Subject: [PATCH] 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; }