Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Topic fix memory leaks #365

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions cmd_behave.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ struct events {
{ NULL, 0 },
};

/* So we can invoke xdotool from within this command */
extern int context_execute(context_t *context);

int cmd_behave(context_t *context) {
int ret = 0;
char *cmd = *context->argv;
Expand Down
8 changes: 5 additions & 3 deletions cmd_behave_screen_edge.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ int cmd_behave_screen_edge(context_t *context) {
/* Only create a context copy if we need one */
if (need_new_context) {
tmpcontext = calloc(1, sizeof(context_t));
if (tmpcontext == NULL) {
fprintf(stderr, "%s: error: failed to allocate memory\n", cmd);
exit(EXIT_FAILURE);
}
memcpy(tmpcontext, context, sizeof(context_t));
}

Expand Down Expand Up @@ -304,9 +308,7 @@ int cmd_behave_screen_edge(context_t *context) {
} /* if quiesce */
} /* if trigger == True */

if (tmpcontext != NULL) {
free(tmpcontext);
}
free(tmpcontext);

if (ret != XDO_SUCCESS) {
printf("Command failed.\n");
Expand Down
17 changes: 14 additions & 3 deletions cmd_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ int cmd_exec(context_t *context) {
}

command = calloc(context->argc + 1, sizeof(char *));
if (command == NULL) {
fprintf(stderr, "%s: error: failed to allocate memory\n", cmd);
return EXIT_FAILURE;
}

for (i=0; i < context->argc; i++) {
if (arity > 0 && i == arity) {
Expand All @@ -94,6 +98,15 @@ int cmd_exec(context_t *context) {
}

command[i] = strdup(context->argv[i]);
if (command[i] == NULL) {
fprintf(stderr, "exec: error: failed to allocate memory\n");
for (;i;i--) {
free(command[i]);
}
free(terminator);
free(command);
return EXIT_FAILURE;
}
command_count = i + 1; /* i starts at 0 */
xdotool_debug(context, "Exec arg[%d]: %s", i, command[i]);
}
Expand All @@ -116,9 +129,7 @@ int cmd_exec(context_t *context) {
}

consume_args(context, command_count);
if (terminator != NULL) {
free(terminator);
}
free(terminator);

for (i=0; i < command_count; i++) {
free(command[i]);
Expand Down
1 change: 1 addition & 0 deletions cmd_mousedown.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ int cmd_mousedown(context_t *context) {
}); /* window_each(...) */

consume_args(context, 1);
free(window_arg);

return ret;
}
2 changes: 2 additions & 0 deletions cmd_mousemove.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ int cmd_mousemove(context_t *context) {
}
}); /* window_each(...) */

free(window_arg);

return ret;
}

Expand Down
4 changes: 1 addition & 3 deletions cmd_mouseup.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ int cmd_mouseup(context_t *context) {
}
}); /* window_each(...) */

if (window_arg != NULL) {
free(window_arg);
}
free(window_arg);
consume_args(context, 1);
return ret;
}
Expand Down
8 changes: 2 additions & 6 deletions cmd_search.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,7 @@ int cmd_search(context_t *context) {
}

do {
if (list != NULL) {
free(list);
}
free(list);

xdo_search_windows(context->xdo, &search, &list, &nwindows);

Expand All @@ -214,9 +212,7 @@ int cmd_search(context_t *context) {
} while (op_sync && nwindows == 0);

/* Free old list as it's malloc'd by xdo_search_windows */
if (context->windows != NULL) {
free(context->windows);
}
free(context->windows);
context->windows = list;
context->nwindows = nwindows;

Expand Down
40 changes: 36 additions & 4 deletions cmd_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,18 @@ int cmd_type(context_t *context) {
break;
case opt_terminator:
terminator = strdup(optarg);
if (terminator == NULL) {
fprintf(stderr, "type: error: failed to allocate memory\n");
return EXIT_FAILURE;
}
break;
case opt_file:
file = strdup(optarg);
break;
file = strdup(optarg);
if (file == NULL) {
fprintf(stderr, "type: error: failed to allocate memory\n");
return EXIT_FAILURE;
}
break;
default:
fprintf(stderr, usage, cmd);
return EXIT_FAILURE;
Expand All @@ -101,7 +109,7 @@ int cmd_type(context_t *context) {
if (context->argc == 0 && file == NULL) {
fprintf(stderr, "You specified the wrong number of args.\n");
fprintf(stderr, usage, cmd);
return 1;
return EXIT_FAILURE;
}

if (arity > 0 && terminator != NULL) {
Expand All @@ -117,6 +125,10 @@ int cmd_type(context_t *context) {

if (file != NULL) {
data = calloc(1 + context->argc, sizeof(char *));
if (data == NULL) {
fprintf(stderr, "Failure allocating for '%s': %s\n", file, strerror(errno));
return EXIT_FAILURE;
}

/* determine whether reading from a file or from stdin */
if (!strcmp(file, "-")) {
Expand Down Expand Up @@ -157,6 +169,10 @@ int cmd_type(context_t *context) {
}
else {
data = calloc(context->argc, sizeof(char *));
if (data == NULL) {
fprintf(stderr, "type: error: failed to allocate memory\n");
return EXIT_FAILURE;
}
}

/* Apply any --arity or --terminator */
Expand All @@ -174,6 +190,17 @@ int cmd_type(context_t *context) {
}

data[data_count] = strdup(context->argv[i]);
if (data[data_count] == NULL) {
fprintf(stderr, "type: error: failed to allocate memory\n");
ret = 0; /* EXIT_FAILURE */
for (i = 0; i < data_count; i++)
free(data[i]);
free(data);
free(file);
free(terminator);
return EXIT_FAILURE;
}

xdotool_debug(context, "Exec arg[%d]: %s", i, data[data_count]);
data_count++;
args_count++;
Expand Down Expand Up @@ -202,9 +229,14 @@ int cmd_type(context_t *context) {
}
}); /* window_each(...) */

consume_args(context, args_count);

for (i = 0; i < data_count; i++)
free(data[i]);
free(data);
free(file);
free(terminator);

consume_args(context, args_count);
return ret > 0;
}

Loading