From: Michael Tremer Date: Mon, 14 Jun 2021 10:37:21 +0000 (+0000) Subject: config: Fix reference counting X-Git-Tag: 0.9.28~1261 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1824fd6f672e9bd4368ddd59c98cecb1eeb0bdfb;p=pakfire.git config: Fix reference counting The reference counter wasn't correctly initialized which cause that the configuration struct was freed too soon but still accessed. Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/config.c b/src/libpakfire/config.c index a231b4894..92091971c 100644 --- a/src/libpakfire/config.c +++ b/src/libpakfire/config.c @@ -54,6 +54,9 @@ int pakfire_config_create(struct pakfire_config** config) { if (!c) return ENOMEM; + // Initialize reference counter + c->nrefs = 1; + // Initialise entries STAILQ_INIT(&c->entries); @@ -77,7 +80,7 @@ static void pakfire_config_free(struct pakfire_config* config) { } struct pakfire_config* pakfire_config_ref(struct pakfire_config* config) { - config->nrefs++; + ++config->nrefs; return config; } diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 17750879a..561084363 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -973,10 +973,10 @@ int pakfire_has_flag(Pakfire pakfire, int flag) { } struct pakfire_config* pakfire_get_config(Pakfire pakfire) { - if (pakfire->config) - return pakfire_config_ref(pakfire->config); + if (!pakfire->config) + return NULL; - return NULL; + return pakfire_config_ref(pakfire->config); } PAKFIRE_EXPORT const char* pakfire_get_path(Pakfire pakfire) {