]> git.ipfire.org Git - pakfire.git/commitdiff
repo: Check pointers before trying to free them
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Jan 2025 22:47:25 +0000 (22:47 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Jan 2025 22:47:25 +0000 (22:47 +0000)
This is again for the static analyzer

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

index ee78fcf859bfde40980a1dec9c8cff95188dd9c6..e1d877557774e18501bf117c35cf839ec20eb6dd 100644 (file)
@@ -790,28 +790,31 @@ static int pakfire_repo_refresh_metadata(struct pakfire_repo* repo, const int fo
        return pakfire_repo_read_metadata(repo, path);
 }
 
-static void free_repo_appdata(struct pakfire_repo_appdata* appdata) {
+static void pakfire_repo_free_appdata(struct pakfire_repo_appdata* appdata) {
        if (appdata)
                free(appdata);
 }
 
-static void pakfire_repo_free(struct pakfire_repo* repo, const int free_repo) {
+static void pakfire_repo_free(struct pakfire_repo* self, const int free_repo) {
+       // Optionally free appdata
        if (free_repo) {
-               free_repo_appdata(repo->repo->appdata);
-               repo_free(repo->repo, 0);
-       }
+               if (self->repo) {
+                       if (self->repo->appdata)
+                               pakfire_repo_free_appdata(self->repo->appdata);
 
-       // Free the key
-       if (repo->key)
-               pakfire_key_unref(repo->key);
+                       repo_free(self->repo, 0);
+               }
+       }
 
-       if (repo->mirrorlist)
-               pakfire_mirrorlist_unref(repo->mirrorlist);
-       if (repo->pakfire)
-               pakfire_unref(repo->pakfire);
-       if (repo->ctx)
-               pakfire_ctx_unref(repo->ctx);
-       free(repo);
+       if (self->mirrorlist)
+               pakfire_mirrorlist_unref(self->mirrorlist);
+       if (self->pakfire)
+               pakfire_unref(self->pakfire);
+       if (self->key)
+               pakfire_key_unref(self->key);
+       if (self->ctx)
+               pakfire_ctx_unref(self->ctx);
+       free(self);
 }
 
 void pakfire_repo_free_all(struct pakfire* pakfire) {
@@ -823,7 +826,9 @@ void pakfire_repo_free_all(struct pakfire* pakfire) {
        int i;
 
        FOR_REPOS(i, repo) {
-               free_repo_appdata(repo->appdata);
+               if (repo->appdata)
+                       pakfire_repo_free_appdata(repo->appdata);
+
                repo_free(repo, 0);
        }
 }