]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
parser: Refactor creating declarations
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 12:37:21 +0000 (12:37 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 12:37:21 +0000 (12:37 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/parser.c

index 3a1a40c8e83ea8f21c0be5108e74b3486b1195a6..8d0cd8200b0899b8168e6dc1f56312f316c8b4ff 100644 (file)
@@ -83,20 +83,20 @@ static int pakfire_parser_compile_regexes(struct pakfire_parser* parser) {
        return 0;
 }
 
-static void pakfire_parser_free_declarations(struct pakfire_parser* parser) {
-       if (!parser->declarations)
-               return;
+static void pakfire_parser_free_declaration(struct pakfire_parser_declaration* d) {
+       if (d->value)
+               free(d->value);
+       free(d);
+}
 
-       for (unsigned int i = 0; i < parser->num_declarations; i++) {
-               struct pakfire_parser_declaration* d = parser->declarations[i];
+static void pakfire_parser_free_declarations(struct pakfire_parser* parser) {
+       if (parser->declarations) {
+               for (unsigned int i = 0; i < parser->num_declarations; i++)
+                       pakfire_parser_free_declaration(parser->declarations[i]);
 
-               // Free everything
-               if (d->value)
-                       free(d->value);
-               free(d);
+               free(parser->declarations);
+               parser->declarations = NULL;
        }
-
-       free(parser->declarations);
 }
 
 static void pakfire_parser_free(struct pakfire_parser* parser) {
@@ -311,8 +311,11 @@ static struct pakfire_parser_declaration* pakfire_parser_find_declaration(
 
 int pakfire_parser_set(struct pakfire_parser* parser,
                const char* namespace, const char* name, const char* value, int flags) {
+       struct pakfire_parser_declaration* d = NULL;
        char* buffer = NULL;
+       int r;
 
+       // Check inputs
        if (!name)
                return -EINVAL;
 
@@ -320,31 +323,37 @@ int pakfire_parser_set(struct pakfire_parser* parser,
                namespace = "";
 
        // Handle when name already exists
-       struct pakfire_parser_declaration* d = pakfire_parser_get_declaration(parser, namespace, name);
+       d = pakfire_parser_get_declaration(parser, namespace, name);
        if (d) {
+               // Append?
                if (flags & PAKFIRE_PARSER_DECLARATION_APPEND) {
                        buffer = pakfire_parser_join(" ", d->value, value);
                        if (!buffer)
-                               return 1;
+                               return -errno;
 
                        // Reset the append flag
                        flags &= ~PAKFIRE_PARSER_DECLARATION_APPEND;
+
+               // Copy?
                } else {
                        buffer = strdup(value);
+                       if (!buffer)
+                               return -errno;
                }
 
                // Replace value
                if (d->value)
                        free(d->value);
-
                d->value = buffer;
 
                // Update flags
                if (flags)
                        d->flags = flags;
 
+#ifdef PAKFIRE_DEBUG_PARSER
                DEBUG(parser->ctx, "%p: Updated declaration: %s.%s = %s\n",
                        parser, d->namespace, d->name, d->value);
+#endif /* PAKFIRE_DEBUG_PARSER */
 
                // All done
                return 0;
@@ -353,19 +362,31 @@ int pakfire_parser_set(struct pakfire_parser* parser,
        // Allocate a new declaration
        d = calloc(1, sizeof(*d));
        if (!d)
-               return -1;
+               return -errno;
 
        // Store namespace
-       pakfire_string_set(d->namespace, namespace);
+       r = pakfire_string_set(d->namespace, namespace);
+       if (r < 0)
+               goto ERROR;
 
-       // Import name & value
-       pakfire_string_set(d->name, name);
-       if (value)
+       // Import name
+       r = pakfire_string_set(d->name, name);
+       if (r < 0)
+               goto ERROR;
+
+       // Import value
+       if (value) {
                d->value = strdup(value);
+               if (!d->value) {
+                       r = -errno;
+                       goto ERROR;
+               }
+       }
 
        // Import flags
        d->flags = flags;
 
+#ifdef PAKFIRE_DEBUG_PARSER
        DEBUG(parser->ctx, "%p: New declaration (%u): %s.%s %s= %s\n",
                parser,
                d->flags,
@@ -373,13 +394,26 @@ int pakfire_parser_set(struct pakfire_parser* parser,
                d->name,
                (d->flags & PAKFIRE_PARSER_DECLARATION_APPEND) ? "+" : "",
                d->value);
+#endif /* PAKFIRE_DEBUG_PARSER */
 
-       // Assign new declaration to array
+       // Grow the array
        parser->declarations = reallocarray(parser->declarations,
-               parser->num_declarations + 1, sizeof(*parser->declarations));
+                       parser->num_declarations + 1, sizeof(*parser->declarations));
+       if (!parser->declarations) {
+               r = -errno;
+               goto ERROR;
+       }
+
+       // Store the declaration
        parser->declarations[parser->num_declarations++] = d;
 
        return 0;
+
+ERROR:
+       if (d)
+               pakfire_parser_free_declaration(d);
+
+       return r;
 }
 
 int pakfire_parser_apply_declaration(struct pakfire_parser* parser,