diff options
author | 2015-08-10 13:16:38 -0700 | |
---|---|---|
committer | 2015-08-10 13:16:38 -0700 | |
commit | 820dda1be6b5792507325da84a88e579daff9fc0 (patch) | |
tree | aeb799f4d613ca9ddb406cf9fdf5e1eaa23a04ef | |
parent | Fix some errors with config loading (diff) | |
download | sway-820dda1be6b5792507325da84a88e579daff9fc0.tar.gz sway-820dda1be6b5792507325da84a88e579daff9fc0.tar.zst sway-820dda1be6b5792507325da84a88e579daff9fc0.zip |
fixed small memory leak. v2
-rw-r--r-- | sway/commands.c | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/sway/commands.c b/sway/commands.c index 3f9cc73f..2ce24fa4 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -346,9 +346,10 @@ struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *lin | |||
346 | int handle_command(struct sway_config *config, char *exec) { | 346 | int handle_command(struct sway_config *config, char *exec) { |
347 | sway_log(L_INFO, "Handling command '%s'", exec); | 347 | sway_log(L_INFO, "Handling command '%s'", exec); |
348 | char *ptr, *cmd; | 348 | char *ptr, *cmd; |
349 | int ret; | ||
350 | |||
349 | if ((ptr = strchr(exec, ' ')) == NULL) { | 351 | if ((ptr = strchr(exec, ' ')) == NULL) { |
350 | cmd = malloc(strlen(exec) + 1); | 352 | cmd = exec; |
351 | strcpy(cmd, exec); | ||
352 | } else { | 353 | } else { |
353 | int index = ptr - exec; | 354 | int index = ptr - exec; |
354 | cmd = malloc(index + 1); | 355 | cmd = malloc(index + 1); |
@@ -358,18 +359,22 @@ int handle_command(struct sway_config *config, char *exec) { | |||
358 | struct cmd_handler *handler = find_handler(handlers, sizeof(handlers) / sizeof(struct cmd_handler), cmd); | 359 | struct cmd_handler *handler = find_handler(handlers, sizeof(handlers) / sizeof(struct cmd_handler), cmd); |
359 | if (handler == NULL) { | 360 | if (handler == NULL) { |
360 | sway_log(L_ERROR, "Unknown command '%s'", cmd); | 361 | sway_log(L_ERROR, "Unknown command '%s'", cmd); |
361 | return 0; // TODO: return error, probably | 362 | ret = 0; // TODO: return error, probably |
362 | } | 363 | } else { |
363 | int argc; | 364 | int argc; |
364 | char **argv = split_directive(exec + strlen(handler->command), &argc); | 365 | char **argv = split_directive(exec + strlen(handler->command), &argc); |
365 | int ret = handler->handle(config, argc, argv); | 366 | int i; |
366 | int i; | 367 | ret = handler->handle(config, argc, argv); |
367 | for (i = 0; i < argc; ++i) { | 368 | for (i = 0; i < argc; ++i) { |
368 | free(argv[i]); | 369 | free(argv[i]); |
370 | } | ||
371 | free(argv); | ||
372 | if (ret != 0) { | ||
373 | sway_log(L_ERROR, "Command failed: %s", cmd); | ||
374 | } | ||
369 | } | 375 | } |
370 | free(argv); | 376 | if(ptr) { |
371 | if (ret != 0) { | 377 | free(cmd); |
372 | sway_log(L_ERROR, "Command failed: %s", cmd); | ||
373 | } | 378 | } |
374 | return ret; | 379 | return ret; |
375 | } | 380 | } |