]> git.ipfire.org Git - pakfire.git/commitdiff
parser: Remove declaration limit and allocate memory dynamically
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 13 Feb 2021 15:40:09 +0000 (15:40 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 13 Feb 2021 15:42:19 +0000 (15:42 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/parser.c

index 60ae14079c23b3262b211403551c2eb6e83cd5b7..4d9eecffd745cd3ddea2082a99d2bca28e0a86c8 100644 (file)
@@ -30,7 +30,6 @@
 #include <pakfire/private.h>
 #include <pakfire/util.h>
 
-#define NUM_DECLARATIONS 1024
 #define VARIABLE_PATTERN "%\\{([A-Za-z0-9_\\-]+)\\}"
 
 struct _PakfireParser {
@@ -40,8 +39,7 @@ struct _PakfireParser {
 
        char* namespace;
 
-       struct pakfire_parser_declaration* declarations[NUM_DECLARATIONS];
-       size_t next_declaration;
+       struct pakfire_parser_declaration** declarations;
        size_t num_declarations;
 };
 
@@ -88,10 +86,6 @@ PAKFIRE_EXPORT PakfireParser pakfire_parser_create(Pakfire pakfire, PakfireParse
                // Make namespace
                parser->namespace = pakfire_parser_make_namespace(parent, namespace);
 
-               parser->num_declarations =
-                       sizeof(parser->declarations) / sizeof(*parser->declarations);
-               parser->next_declaration = 0;
-
                DEBUG(pakfire, "Allocated new parser at %p (%s, %p)\n",
                        parser, parser->namespace, parser->parent);
        }
@@ -114,13 +108,12 @@ Pakfire pakfire_parser_get_pakfire(PakfireParser parser) {
 }
 
 static void pakfire_parser_free_declarations(PakfireParser parser) {
+       if (!parser->declarations)
+               return;
+
        for (unsigned int i = 0; i < parser->num_declarations; i++) {
                struct pakfire_parser_declaration* d = parser->declarations[i];
 
-               // If we hit NULL, this is the end
-               if (!d)
-                       break;
-
                // Free everything
                if (d->name)
                        free(d->name);
@@ -128,6 +121,8 @@ static void pakfire_parser_free_declarations(PakfireParser parser) {
                        free(d->value);
                free(d);
        }
+
+       free(parser->declarations);
 }
 
 static void pakfire_parser_free(PakfireParser parser) {
@@ -199,12 +194,6 @@ static int pakfire_parser_set_declaration(PakfireParser parser,
                return 0;
        }
 
-       // Check if we have any space left
-       if (parser->next_declaration >= parser->num_declarations) {
-               ERROR(parser->pakfire, "No free declarations left\n");
-               return -1;
-       }
-
        // Allocate a new declaration
        d = calloc(1, sizeof(*d));
        if (!d)
@@ -218,7 +207,9 @@ static int pakfire_parser_set_declaration(PakfireParser parser,
        DEBUG(parser->pakfire, "New declaration: %s = %s\n", d->name, d->value);
 
        // Assign new declaration to array
-       parser->declarations[parser->next_declaration++] = d;
+       parser->declarations = reallocarray(parser->declarations,
+               sizeof(*parser->declarations), parser->num_declarations + 1);
+       parser->declarations[parser->num_declarations++] = d;
 
        return 0;
 }