From 5e06b1b21d0fdd265e4e597fe4faa46120cad201 Mon Sep 17 00:00:00 2001 From: Peter Bui Date: Mon, 8 Aug 2011 21:44:39 -0400 Subject: [PATCH 1/3] Fix some potential memory leaks --- src/assignments.c | 4 +++- src/cfgparse.y | 1 + src/config.c | 1 + src/handlers.c | 3 ++- src/ipc.c | 5 ++++- 5 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/assignments.c b/src/assignments.c index f171dc3b..50f48526 100644 --- a/src/assignments.c +++ b/src/assignments.c @@ -39,7 +39,9 @@ void run_assignments(i3Window *window) { DLOG("execute command %s\n", current->dest.command); char *full_command; asprintf(&full_command, "[id=\"%d\"] %s", window->id, current->dest.command); - parse_cmd(full_command); + char *json_result = parse_cmd(full_command); + FREE(full_command); + FREE(json_result); } /* Store that we ran this assignment to not execute it again */ diff --git a/src/cfgparse.y b/src/cfgparse.y index 59b22c6c..abbe4da0 100644 --- a/src/cfgparse.y +++ b/src/cfgparse.y @@ -1101,6 +1101,7 @@ colorpixel: char *hex; if (asprintf(&hex, "#%s", $2) == -1) die("asprintf()"); + free($2); $$ = get_colorpixel(hex); free(hex); } diff --git a/src/config.c b/src/config.c index 24af35df..14fc6e02 100644 --- a/src/config.c +++ b/src/config.c @@ -193,6 +193,7 @@ static char *get_config_path(const char *override_configpath) { config_path = resolve_tilde("~/.i3/config"); if (path_exists(config_path)) return config_path; + free(config_path); /* 2: check for $XDG_CONFIG_HOME/i3/config */ if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL) diff --git a/src/handlers.c b/src/handlers.c index 89a6bd0a..b83ac83d 100644 --- a/src/handlers.c +++ b/src/handlers.c @@ -112,7 +112,8 @@ static int handle_key_press(xcb_key_press_event_t *event) { } } - parse_cmd(bind->command); + char *json_result = parse_cmd(bind->command); + free(json_result); return 1; } diff --git a/src/ipc.c b/src/ipc.c index b2cd482c..d798ffa0 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -147,7 +147,8 @@ IPC_HANDLER(command) { char *command = scalloc(message_size + 1); strncpy(command, (const char*)message, message_size); LOG("IPC: received: *%s*\n", command); - const char *reply = parse_cmd((const char*)command); + char *reply = parse_cmd((const char*)command); + char *save_reply = reply; free(command); /* If no reply was provided, we just use the default success message */ @@ -155,6 +156,8 @@ IPC_HANDLER(command) { reply = "{\"success\":true}"; ipc_send_message(fd, (const unsigned char*)reply, I3_IPC_REPLY_TYPE_COMMAND, strlen(reply)); + + FREE(save_reply); } static void dump_rect(yajl_gen gen, const char *name, Rect r) { From 95416175cdf347df6c7ea78c65db7578ad259409 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Tue, 9 Aug 2011 08:41:14 +0200 Subject: [PATCH 2/3] Bugfix: use FREE to correctly handle NULL replies --- src/handlers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/handlers.c b/src/handlers.c index b83ac83d..f19b53c8 100644 --- a/src/handlers.c +++ b/src/handlers.c @@ -113,7 +113,7 @@ static int handle_key_press(xcb_key_press_event_t *event) { } char *json_result = parse_cmd(bind->command); - free(json_result); + FREE(json_result); return 1; } From cd1add1f3c0c7b638a20b810822838c5ecb7bc2d Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Tue, 9 Aug 2011 08:41:42 +0200 Subject: [PATCH 3/3] =?UTF-8?q?Bugfix:=20don=E2=80=99t=20free=20the=20old?= =?UTF-8?q?=20json=5Foutput,=20the=20caller=20does=20that?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cmdparse.y | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmdparse.y b/src/cmdparse.y index 32558f2e..9b63ff0c 100644 --- a/src/cmdparse.y +++ b/src/cmdparse.y @@ -81,13 +81,13 @@ int cmdyywrap() { } char *parse_cmd(const char *new) { + json_output = NULL; LOG("COMMAND: *%s*\n", new); cmdyy_scan_string(new); match_init(¤t_match); context = scalloc(sizeof(struct context)); context->filename = "cmd"; - FREE(json_output); if (cmdyyparse() != 0) { fprintf(stderr, "Could not parse command\n"); asprintf(&json_output, "{\"success\":false, \"error\":\"%s at position %d\"}",