From 54187ed0f082d145955a932f67259668dd038f65 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Thu, 14 Oct 2010 06:35:04 +0000 Subject: config parser: do not assume that realloc return same pointer We need to update the parser->line pointer on realloc and do not initialize the token array til after the potensial realloc in bb_get_chunk_with_continuation(). While here, also replace a realloc() with malloc() where pointer always is NULL. Signed-off-by: Natanael Copa Signed-off-by: Bernhard Reutner-Fischer --- libc/misc/internals/parse_config.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'libc/misc/internals') diff --git a/libc/misc/internals/parse_config.c b/libc/misc/internals/parse_config.c index 8fa324e10..6734f35f4 100644 --- a/libc/misc/internals/parse_config.c +++ b/libc/misc/internals/parse_config.c @@ -78,6 +78,7 @@ static off_t bb_get_chunk_with_continuation(parser_t* parsr) parsr->line_len += PAGE_SIZE; parsr->data = realloc(parsr->data, parsr->data_len + parsr->line_len); + parsr->line = parsr->data + parsr->data_len; } } return pos; @@ -186,23 +187,21 @@ again: parser->line_len = 81; if (parser->data_len == 0) parser->data_len += 1 + ntokens * sizeof(char *); - parser->data = realloc(parser->data, - parser->data_len + parser->line_len); + parser->data = malloc(parser->data_len + parser->line_len); if (parser->data == NULL) return 0; parser->allocated |= 1; } /* else { assert(parser->data_len > 0); } */ if (parser->line == NULL) parser->line = parser->data + parser->data_len; - if (*tokens == NULL) - *tokens = (char **) parser->data; - memset(*tokens, 0, sizeof(*tokens[0]) * ntokens); /*config_free_data(parser);*/ /* Read one line (handling continuations with backslash) */ len = bb_get_chunk_with_continuation(parser); if (len == -1) return 0; + *tokens = (char **) parser->data; + memset(*tokens, 0, sizeof(*tokens[0]) * ntokens); line = parser->line; /* Skip multiple token-delimiters in the start of line? */ -- cgit v1.2.3 From c7c7ea92be00a9b5b48d1243bb75d32390263159 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Thu, 14 Oct 2010 06:35:06 +0000 Subject: config parser: always initialize line pointer We must always initialize line pointer since data pointer might have changed due to a realloc (in getserv.c for example). Signed-off-by: Natanael Copa Signed-off-by: Bernhard Reutner-Fischer --- libc/misc/internals/parse_config.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'libc/misc/internals') diff --git a/libc/misc/internals/parse_config.c b/libc/misc/internals/parse_config.c index 6734f35f4..6d3b6f4a0 100644 --- a/libc/misc/internals/parse_config.c +++ b/libc/misc/internals/parse_config.c @@ -192,8 +192,7 @@ again: return 0; parser->allocated |= 1; } /* else { assert(parser->data_len > 0); } */ - if (parser->line == NULL) - parser->line = parser->data + parser->data_len; + parser->line = parser->data + parser->data_len; /*config_free_data(parser);*/ /* Read one line (handling continuations with backslash) */ -- cgit v1.2.3 From 6e74339e7d762857579169068b060a4fd3f345c4 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Thu, 14 Oct 2010 06:35:07 +0000 Subject: parse_config: discard rest of incomplete line If line is longer then size of given buffer and buffer is not allocated by the config parser itself, then discard rest of line. Signed-off-by: Natanael Copa Signed-off-by: Bernhard Reutner-Fischer --- libc/misc/internals/parse_config.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'libc/misc/internals') diff --git a/libc/misc/internals/parse_config.c b/libc/misc/internals/parse_config.c index 6d3b6f4a0..c17d25553 100644 --- a/libc/misc/internals/parse_config.c +++ b/libc/misc/internals/parse_config.c @@ -79,6 +79,13 @@ static off_t bb_get_chunk_with_continuation(parser_t* parsr) parsr->data = realloc(parsr->data, parsr->data_len + parsr->line_len); parsr->line = parsr->data + parsr->data_len; + } else { + /* discard rest of line if not enough space in buffer */ + int c; + do { + c = fgetc(parsr->fp); + } while (c != EOF && c != '\n'); + break; } } return pos; -- cgit v1.2.3