Skip to content

Commit f18d9b2

Browse files
author
Rob Sanders
committed
Fix additional places strdup/free used for filters, several places no check of return value being done
1 parent 408a2eb commit f18d9b2

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

libcli.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ static char *join_words(int argc, char **argv) {
820820
}
821821

822822
p = malloc(len + 1);
823+
if (!p) return NULL;
823824
p[0] = 0;
824825

825826
for (i = 0; i < argc; i++) {
@@ -1940,8 +1941,7 @@ int cli_match_filter(UNUSED(struct cli_def *cli), const char *string, void *data
19401941
int r = CLI_ERROR;
19411942

19421943
if (!string) {
1943-
if (state->flags & MATCH_REGEX)
1944-
regfree(&state->match.re);
1944+
if (state->flags & MATCH_REGEX) regfree(&state->match.re);
19451945

19461946
free(state);
19471947
return CLI_OK;
@@ -1971,19 +1971,20 @@ struct cli_range_filter_state {
19711971

19721972
int cli_range_filter_init(struct cli_def *cli, int argc, char **argv, struct cli_filter *filt) {
19731973
struct cli_range_filter_state *state;
1974-
char *from = strdup(cli_get_optarg_value(cli, "range_start", NULL));
1975-
char *to = strdup(cli_get_optarg_value(cli, "range_end", NULL));
1974+
char *from = cli_get_optarg_value(cli, "range_start", NULL);
1975+
char *to = cli_get_optarg_value(cli, "range_end", NULL);
19761976

19771977
// Do not have to check from/to since we would not have gotten here if we were missing a required argument.
1978+
// Note that since those pointers are not NULL, we don't have to strdup them or free them - just use the pointer
1979+
// from the command line processing and continue
1980+
19781981
filt->filter = cli_range_filter;
19791982
filt->data = state = calloc(sizeof(struct cli_range_filter_state), 1);
19801983
if (state) {
19811984
state->from = from;
19821985
state->to = to;
19831986
return CLI_OK;
19841987
} else {
1985-
free_z(from);
1986-
free_z(to);
19871988
return CLI_ERROR;
19881989
}
19891990
}
@@ -1993,8 +1994,6 @@ int cli_range_filter(UNUSED(struct cli_def *cli), const char *string, void *data
19931994
int r = CLI_ERROR;
19941995

19951996
if (!string) {
1996-
free_z(state->from);
1997-
free_z(state->to);
19981997
free_z(state);
19991998
return CLI_OK;
20001999
}
@@ -2465,7 +2464,7 @@ int cli_int_execute_buildmode(struct cli_def *cli) {
24652464
cmdline = strdup(cli_command_name(cli, cli->buildmode->command));
24662465
if (!cmdline) {
24672466
cli_error(cli, "Unable to allocate memory to process buildmode commandline");
2468-
rc = CLI_ERROR;
2467+
rc = CLI_ERROR;
24692468
}
24702469
for (optarg = cli->buildmode->command->optargs; rc == CLI_OK && optarg; optarg = optarg->next) {
24712470
value = NULL;
@@ -3402,6 +3401,13 @@ static void cli_int_parse_optargs(struct cli_def *cli, struct cli_pipeline_stage
34023401
if (oaptr->flags & CLI_CMD_REMAINDER_OF_LINE) {
34033402
char *combined = NULL;
34043403
combined = join_words(stage->num_words - word_idx, stage->words + word_idx);
3404+
if (!combined) {
3405+
cli_error(cli, "%sUnable to allocate memory for command processing", lastchar == '\0' ? "" : "\n");
3406+
cli_reprompt(cli);
3407+
stage->error_word = stage->words[word_idx];
3408+
stage->status = CLI_ERROR;
3409+
goto done;
3410+
}
34053411
set_value_return = cli_set_optarg_value(cli, oaptr->name, combined, 0);
34063412
free_z(combined);
34073413
} else {

0 commit comments

Comments
 (0)