From: Timo Kokkonen Date: Thu, 16 Jan 2014 20:11:47 +0000 (+0200) Subject: config.c: Handle multiple variables correcly X-Git-Url: http://git.itanic.dy.fi/?p=log-plotter;a=commitdiff_plain;h=0a59c3e87ef985e0af68e552500de1c3a022b53b config.c: Handle multiple variables correcly If we happen to have multiple variables in the string we are handling, we must continue handling the string always at the end of the previous replaced variable. Otherwise we may end up skipping the beginning of the next variable. Signed-off-by: Timo Kokkonen --- diff --git a/config.c b/config.c index 1dcc9c8..8b84d58 100644 --- a/config.c +++ b/config.c @@ -247,7 +247,7 @@ static char *get_value_for_variable(char *variable, static int replace_variable_with_value(char *str, size_t len, char *variable_start, char *variable_end, - const char *value) + const char *value, char **endptr) { int value_len = strlen(value); char *from, *to; @@ -265,6 +265,8 @@ static int replace_variable_with_value(char *str, size_t len, from = variable_end + 1; to = variable_start + value_len; + *endptr = to; + while (*from) { *to = *from; from++; @@ -301,6 +303,8 @@ static int replace_variable_with_value(char *str, size_t len, to--; } + *endptr = from; + /* * use memcpy to avoid NULL terminating prematurely * the target string @@ -343,7 +347,6 @@ int replace_variables_with_values(char *str, size_t len, variable_end = ptr; *ptr = 0; - ptr++; value = get_value_for_variable(variable_start, cfg); if (!value) { @@ -358,7 +361,7 @@ int replace_variables_with_values(char *str, size_t len, continue; } overflow = replace_variable_with_value(str, len, replace_start, - variable_end, value); + variable_end, value, &ptr); } if (overflow)