From d2b533328d4b9f6148dfe1997ae8517d6ea0ca68 Mon Sep 17 00:00:00 2001 From: Emil Mikulic Date: Sat, 24 Nov 2012 16:02:20 +1100 Subject: [PATCH] Fix memory leaks in config_parser. push_token() doesn't take ownership of its str argument. --- src/config_parser.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/config_parser.c b/src/config_parser.c index 6f96cc5f..80d436b1 100644 --- a/src/config_parser.c +++ b/src/config_parser.c @@ -91,7 +91,7 @@ static struct stack_entry stack[10]; * single array, since the number of entries we have to store is very small. * */ -static void push_string(const char *identifier, char *str) { +static void push_string(const char *identifier, const char *str) { for (int c = 0; c < 10; c++) { if (stack[c].identifier != NULL && strcmp(stack[c].identifier, identifier) != 0) @@ -99,11 +99,13 @@ static void push_string(const char *identifier, char *str) { if (stack[c].identifier == NULL) { /* Found a free slot, let’s store it here. */ stack[c].identifier = identifier; - stack[c].val.str = str; + stack[c].val.str = sstrdup(str); stack[c].type = STACK_STR; } else { /* Append the value. */ - sasprintf(&(stack[c].val.str), "%s,%s", stack[c].val.str, str); + char *prev = stack[c].val.str; + sasprintf(&(stack[c].val.str), "%s,%s", prev, str); + free(prev); } return; } @@ -352,7 +354,7 @@ struct ConfigResult *parse_config(const char *input, struct context *context) { if (token->name[0] == '\'') { if (strncasecmp(walk, token->name + 1, strlen(token->name) - 1) == 0) { if (token->identifier != NULL) - push_string(token->identifier, sstrdup(token->name + 1)); + push_string(token->identifier, token->name + 1); walk += strlen(token->name) - 1; next_state(token); token_handled = true; @@ -424,6 +426,7 @@ struct ConfigResult *parse_config(const char *input, struct context *context) { } if (token->identifier) push_string(token->identifier, str); + free(str); /* If we are at the end of a quoted string, skip the ending * double quote. */ if (*walk == '"') -- 2.39.5