]> git.ipfire.org Git - pakfire.git/commitdiff
transaction: Refactor reading userinstalled packages
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 20 Sep 2023 18:15:18 +0000 (18:15 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 20 Sep 2023 18:15:18 +0000 (18:15 +0000)
This could potentially leak memory and was less robust.

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

index ab64307a7e8ae4675475ec452a99acc1f0f65f13..91470699f74169ef9d3891cc9b17b90604556504 100644 (file)
@@ -193,36 +193,59 @@ static int pakfire_transaction_import_transaction(
 
 static int pakfire_transaction_import_userinstalled(
                struct pakfire_transaction* t, Solver* solver) {
+       Queue userinstalled;
+       const char* package = NULL;
+       int r = 0;
+
+       queue_init(&userinstalled);
+
+       // Cleanup previous content
        if (t->userinstalled) {
-               free(t->userinstalled);
+               pakfire_strings_free(t->userinstalled);
                t->userinstalled = NULL;
        }
 
-       Queue userinstalled;
-       queue_init(&userinstalled);
-
        // Fetch a list of all packages that are installed by the user
        solver_get_userinstalled(solver, &userinstalled, GET_USERINSTALLED_NAMES);
 
        // Skip everything if the queue is empty
        if (!userinstalled.count)
-               return 0;
+               goto OUT;
 
        t->userinstalled = calloc(userinstalled.count + 1, sizeof(*t->userinstalled));
        if (!t->userinstalled) {
-               ERROR(t->pakfire, "Could not allocate userinstalled\n");
-               return 1;
+               ERROR(t->pakfire, "Could not allocate userinstalled: %m\n");
+               r = -errno;
+               goto ERROR;
        }
 
        Pool* pool = pakfire_get_solv_pool(t->pakfire);
 
        // Store the names of all userinstalled packages
        for (int i = 0; i < userinstalled.count; i++) {
-               const char* package = pool_id2str(pool, userinstalled.elements[i]);
+               package = pool_id2str(pool, userinstalled.elements[i]);
+
                t->userinstalled[i] = strdup(package);
+
+               if (!t->userinstalled[i]) {
+                       r = -errno;
+                       goto ERROR;
+               }
        }
 
-       return 0;
+       // Success
+       r = 0;
+
+ERROR:
+       if (t->userinstalled) {
+               pakfire_strings_free(t->userinstalled);
+               t->userinstalled = NULL;
+       }
+
+OUT:
+       queue_free(&userinstalled);
+
+       return r;
 }
 
 int pakfire_transaction_create(struct pakfire_transaction** transaction,