Bugfix: Correctly count variables when parsing the configfile (Thanks dbp)

This commit is contained in:
Michael Stapelberg 2010-11-26 11:25:02 +01:00
parent bb5f4eea88
commit ad8065e366
1 changed files with 8 additions and 1 deletions

View File

@ -119,14 +119,21 @@ void parse_file(const char *f) {
* how much extra bytes it requires when replaced. */ * how much extra bytes it requires when replaced. */
struct Variable *current, *nearest; struct Variable *current, *nearest;
int extra_bytes = 0; int extra_bytes = 0;
/* We need to copy the buffer because we need to invalidate the
* variables (otherwise we will count them twice, which is bad when
* 'extra' is negative) */
char *bufcopy = sstrdup(buf);
SLIST_FOREACH(current, &variables, variables) { SLIST_FOREACH(current, &variables, variables) {
int extra = (strlen(current->value) - strlen(current->key)); int extra = (strlen(current->value) - strlen(current->key));
char *next; char *next;
for (next = buf; for (next = buf;
(next = strcasestr(buf + (next - buf), current->key)) != NULL; (next = strcasestr(buf + (next - buf), current->key)) != NULL;
next += strlen(current->key)) next += strlen(current->key)) {
*next = '_';
extra_bytes += extra; extra_bytes += extra;
}
} }
FREE(bufcopy);
/* Then, allocate a new buffer and copy the file over to the new one, /* Then, allocate a new buffer and copy the file over to the new one,
* but replace occurences of our variables */ * but replace occurences of our variables */