]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
parser: Always copy the namespace to the stack
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 14:32:06 +0000 (14:32 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 14:33:06 +0000 (14:33 +0000)
This avoids using strdupa() which will result in those functions not
being possible to be inlined.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/parser.c

index 3198202e58c0006e91744b3663e104beb03553c4..0614cd73e973253a8d28d9c82239979fb29482e1 100644 (file)
@@ -231,8 +231,8 @@ static struct pakfire_parser_declaration* pakfire_parser_get_declaration_recursi
        return NULL;
 }
 
-static void pakfire_parser_strip_namespace(char** s) {
-       char* pos = strrchr(*s, '.');
+static void pakfire_parser_strip_namespace(char* s) {
+       char* pos = strrchr(s, '.');
 
        if (pos)
                *pos = '\0';
@@ -265,11 +265,15 @@ static char* pakfire_parser_join(const char* c, const char* val1, const char* va
 static struct pakfire_parser_declaration* pakfire_parser_find_declaration(
                struct pakfire_parser* parser, const char* namespace, const char* name) {
        struct pakfire_parser_declaration* d = NULL;
-       char* n = NULL;
+       char n[NAME_MAX] = "";
+       int r;
 
        // Create a working copy of namespace
-       if (namespace)
-               n = strdupa(namespace);
+       if (namespace) {
+               r = pakfire_string_set(n, namespace);
+               if (r < 0)
+                       return NULL;
+       }
 
        for (;;) {
                d = pakfire_parser_get_declaration_recursive(parser, n, name);
@@ -277,11 +281,11 @@ static struct pakfire_parser_declaration* pakfire_parser_find_declaration(
                        return d;
 
                // End if we have exhausted the namespace
-               if (!n || !*n)
+               if (!*n)
                        break;
 
                // Strip namespace
-               pakfire_parser_strip_namespace(&n);
+               pakfire_parser_strip_namespace(n);
        }
 
        // Nothing found
@@ -592,6 +596,7 @@ ERROR:
 
 static int pakfire_parser_expand_variables(struct pakfire_parser* parser,
                const char* namespace, char** buffer) {
+       char parent_namespace[NAME_MAX];
        pcre2_match_data* match = NULL;
        PCRE2_UCHAR* variable = NULL;
        PCRE2_SIZE variable_length = 0;
@@ -651,8 +656,12 @@ static int pakfire_parser_expand_variables(struct pakfire_parser* parser,
 
                        // Move up one step and lookup there
                        if (namespace && *namespace) {
-                               char* parent_namespace = strdupa(namespace);
-                               pakfire_parser_strip_namespace(&parent_namespace);
+                               r = pakfire_string_set(parent_namespace, namespace);
+                               if (r < 0)
+                                       goto ERROR;
+
+                               // Strip
+                               pakfire_parser_strip_namespace(parent_namespace);
 
                                repl = pakfire_parser_get_raw(parser, parent_namespace, (const char*)variable);