Skip to content

Commit

Permalink
Fix potential crash when executing commands in bindings
Browse files Browse the repository at this point in the history
The function run_prompt_command may reallocate the run_request,
invalidating the pointer to the current request if it was inside
run_request. The resulting use-after-free would cause occasional crashes.

Fixes #1001
  • Loading branch information
krobelus committed Mar 30, 2020
1 parent 972e625 commit 7e76387
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion include/tig/prompt.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ bool prompt_menu(const char *prompt, const struct menu_item *items, int *selecte

enum request run_prompt_command(struct view *view, const char *argv[]);
enum request open_prompt(struct view *view);
enum request exec_run_request(struct view *view, struct run_request *req);
enum request exec_run_request(struct view *view, enum request request, struct run_request *req);

#endif
/* vim: set ts=8 sw=8 noexpandtab: */
8 changes: 6 additions & 2 deletions src/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ run_prompt_command(struct view *view, const char *argv[])
argv[1] = cmd;
report("Failed to execute command: %s", get_status_message(code));
} else {
request = exec_run_request(view, &req);
request = exec_run_request(view, REQ_UNKNOWN, &req);
argv[1] = cmd;
return request;
}
Expand Down Expand Up @@ -1088,7 +1088,7 @@ run_prompt_command(struct view *view, const char *argv[])
}

enum request
exec_run_request(struct view *view, struct run_request *req)
exec_run_request(struct view *view, enum request request, struct run_request *req)
{
const char **argv = NULL;
bool confirmed = false;
Expand All @@ -1108,6 +1108,10 @@ exec_run_request(struct view *view, struct run_request *req)
if (req->flags.internal) {
result_request = run_prompt_command(view, argv);

if (request != REQ_UNKNOWN) {
req = get_run_request(request);
}

} else {
confirmed = !req->flags.confirm;

Expand Down
2 changes: 1 addition & 1 deletion src/tig.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ open_run_request(struct view *view, enum request request)
return REQ_NONE;
}

return exec_run_request(view, req);
return exec_run_request(view, request, req);
}

/*
Expand Down

0 comments on commit 7e76387

Please sign in to comment.