commands: cmd_results->input is duplicated/freed.

This commit is contained in:
S. Christoffer Eliesen 2015-10-23 13:03:14 +02:00
parent 88a2ddbe83
commit 362413bd50
2 changed files with 11 additions and 5 deletions

View file

@ -17,8 +17,7 @@ enum cmd_status {
struct cmd_results { struct cmd_results {
enum cmd_status status; enum cmd_status status;
char *input;
const char *input;
char *error; char *error;
}; };

View file

@ -1249,7 +1249,7 @@ struct cmd_results *handle_command(char *_exec) {
if (results) { if (results) {
free_cmd_results(results); free_cmd_results(results);
} }
results = cmd_results_new(CMD_INVALID, strdup(cmd), "Unknown/invalid command"); results = cmd_results_new(CMD_INVALID, cmd, "Unknown/invalid command");
free_argv(argc, argv); free_argv(argc, argv);
goto cleanup; goto cleanup;
} }
@ -1291,7 +1291,7 @@ struct cmd_results *config_command(char *exec) {
} }
struct cmd_handler *handler = find_handler(argv[0]); struct cmd_handler *handler = find_handler(argv[0]);
if (!handler) { if (!handler) {
char *input = argv[0] ? strdup(argv[0]) : "(empty)"; char *input = argv[0] ? argv[0] : "(empty)";
results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command"); results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command");
goto cleanup; goto cleanup;
} }
@ -1314,7 +1314,11 @@ struct cmd_results *config_command(char *exec) {
struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *format, ...) { struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *format, ...) {
struct cmd_results *results = malloc(sizeof(struct cmd_results)); struct cmd_results *results = malloc(sizeof(struct cmd_results));
results->status = status; results->status = status;
results->input = input; // input is the command name if (input) {
results->input = strdup(input); // input is the command name
} else {
results->input = NULL;
}
if (format) { if (format) {
char *error = malloc(256); char *error = malloc(256);
va_list args; va_list args;
@ -1329,6 +1333,9 @@ struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, c
} }
void free_cmd_results(struct cmd_results *results) { void free_cmd_results(struct cmd_results *results) {
if (results->input) {
free(results->input);
}
if (results->error) { if (results->error) {
free(results->error); free(results->error);
} }