From: Michael Tremer Date: Thu, 5 Oct 2023 09:56:55 +0000 (+0000) Subject: parser: Refactor join function to avoid format errors X-Git-Tag: 0.9.30~1544 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=33b2a8158c2d40cc8590ae70d45f755acf34ea3a;p=pakfire.git parser: Refactor join function to avoid format errors Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/parser.c b/src/libpakfire/parser.c index b65f16a7b..ff9083d24 100644 --- a/src/libpakfire/parser.c +++ b/src/libpakfire/parser.c @@ -219,27 +219,19 @@ static void pakfire_parser_strip_namespace(char** s) { (*s)[0] = '\0'; } -static char* pakfire_parser_join(const char* fmt, const char* val1, const char* val2) { +static char* pakfire_parser_join(const char* c, const char* val1, const char* val2) { char* result = NULL; int r; - // Merge both values if both are set - if (val1 && *val1 && val2 && *val2) { - r = asprintf(&result, fmt, val1, val2); - if (r < 0) - return NULL; - - return result; - } - - // If only one value is set, return that one - if (val1 && *val1) - return strdup(val1); - else if (val2 && *val2) - return strdup(val2); + // Join both strings + r = asprintf(&result, "%s%s%s", + (val1) ? val1 : "", + (val1 && *val1 && val2 && *val2) ? c : "", + (val2) ? val2 : ""); + if (r < 0) + return NULL; - // Return an empty string if no value is set - return strdup(""); + return result; } static struct pakfire_parser_declaration* pakfire_parser_find_declaration( @@ -282,7 +274,7 @@ int pakfire_parser_set(struct pakfire_parser* parser, struct pakfire_parser_declaration* d = pakfire_parser_get_declaration(parser, namespace, name); if (d) { if (flags & PAKFIRE_PARSER_DECLARATION_APPEND) { - buffer = pakfire_parser_join("%s %s", d->value, value); + buffer = pakfire_parser_join(" ", d->value, value); if (!buffer) return 1; @@ -819,7 +811,7 @@ int pakfire_parser_merge(struct pakfire_parser* parser1, struct pakfire_parser* break; // Make the new namespace - namespace = pakfire_parser_join("%s.%s", parser2->namespace, d->namespace); + namespace = pakfire_parser_join(".", parser2->namespace, d->namespace); if (!namespace) { r = 1; goto OUT; @@ -839,7 +831,7 @@ int pakfire_parser_merge(struct pakfire_parser* parser1, struct pakfire_parser* } // Make the new value - value = pakfire_parser_join("%s %s", old_value, d->value); + value = pakfire_parser_join(" ", old_value, d->value); if (!value) { r = 1; goto OUT;