From 4bd8601ebd95c130e504cc724b0c5eb163050982 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Fri, 28 Jul 2017 13:02:08 +0200 Subject: [PATCH] pkg_parse: fix segfault when parsing descriptions with leading newlines During parsing of multi-line descriptions that begin with a newline character, the description pointer will be NULL when the first continuation line is encountered, causing the strlen() invocation on the description pointer to segfault. This is caused by the fact that the parse_simple() helper used for parsing the initial description line returns NULL instead of an empty string when encountering only whitespace after the field name. Rework the continuation line parsing code to not assume an initialized pointer by checking before calling strlen() and avoiding realloc() on a NULL pointer. Fixes FS#933. Signed-off-by: Jo-Philipp Wich --- libopkg/pkg_parse.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/libopkg/pkg_parse.c b/libopkg/pkg_parse.c index d35770c..0baa4db 100644 --- a/libopkg/pkg_parse.c +++ b/libopkg/pkg_parse.c @@ -333,18 +333,16 @@ int pkg_parse_line(void *ptr, char *line, uint mask) case ' ': if ((mask & PFM_DESCRIPTION) && reading_description) { - if (isatty(1)) { - description = xrealloc(description, - strlen(description) - + 1 + strlen(line) + - 1); + size_t len = (description ? strlen(description) : 0) + + (isatty(1) ? 1 : 0) + strlen(line) + 1; + + description = description ? xrealloc(description, len) + : xcalloc(len, 1); + + if (isatty(1)) strcat(description, "\n"); - } else { - description = xrealloc(description, - strlen(description) - + 1 + strlen(line)); - } - strcat(description, (line)); + + strcat(description, line); goto dont_reset_flags; } else if ((mask & PFM_CONFFILES) && reading_conffiles) { parse_conffiles(pkg, line); -- 2.25.1