]> git.ipfire.org Git - pakfire.git/commitdiff
parser: Refactor join function to avoid format errors
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 5 Oct 2023 09:56:55 +0000 (09:56 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 5 Oct 2023 09:56:55 +0000 (09:56 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/parser.c

index b65f16a7bdfad939e8ce410ac86fcc6f2c9127c9..ff9083d248705ae691c35ee41e3773edc43323c7 100644 (file)
@@ -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;