]> git.ipfire.org Git - thirdparty/knot-dns.git/commitdiff
dnssec: when initializing keystore, make memory allocation optional
authorDavid Vašek <david.vasek@nic.cz>
Tue, 2 Dec 2025 15:10:19 +0000 (16:10 +0100)
committerDavid Vašek <david.vasek@nic.cz>
Wed, 20 May 2026 07:10:38 +0000 (09:10 +0200)
src/knot/dnssec/kasp/kasp_zone.c

index d39e1b6d356658813d1627ea90ab992fa392f839..ef89a0b97d73f84d428110ed70d322ba34ed1ce7 100644 (file)
@@ -315,25 +315,34 @@ void free_key_params(key_params_t *parm)
        }
 }
 
-void zone_deinit_keystore(knot_kasp_keystore_t **keystores)
+static void _zone_deinit_keystore(knot_kasp_keystore_t **keystores, bool deallocate)
 {
        if (keystores != NULL && *keystores != NULL) {
                for (size_t i = 0; i < (*keystores)[0].count; i++) {
                        dnssec_keystore_deinit((*keystores)[i].keystore);
                }
-               free(*keystores);
-               *keystores = NULL;
+               if (deallocate) {
+                       free(*keystores);
+                       *keystores = NULL;
+               }
        }
 }
 
+void zone_deinit_keystore(knot_kasp_keystore_t **keystores)
+{
+       _zone_deinit_keystore(keystores, true);
+}
+
 int zone_init_keystore(conf_t *conf, conf_val_t *policy_id, conf_val_t *keystore_id,
                        knot_kasp_keystore_t **keystores)
 {
-       if (keystores == NULL || *keystores != NULL ||
+       if (keystores == NULL ||
            (bool)(policy_id == NULL) == (bool)(keystore_id == NULL)) {
                return KNOT_EINVAL;
        }
 
+       bool allocate = (*keystores == NULL);
+
        char *zone_path = conf_db(conf, C_KASP_DB);
        if (zone_path == NULL) {
                return KNOT_ENOMEM;
@@ -351,10 +360,12 @@ int zone_init_keystore(conf_t *conf, conf_val_t *policy_id, conf_val_t *keystore
        }
 
        size_t ks_count = conf_val_count(keystore_id);
-       *keystores = calloc(ks_count, sizeof(**keystores));
-       if (*keystores == NULL) {
-               free(zone_path);
-               return KNOT_ENOMEM;
+       if (allocate) {
+               *keystores = calloc(ks_count, sizeof(**keystores));
+               if (*keystores == NULL) {
+                       free(zone_path);
+                       return KNOT_ENOMEM;
+               }
        }
 
        int ret = KNOT_EOK;
@@ -380,7 +391,7 @@ int zone_init_keystore(conf_t *conf, conf_val_t *policy_id, conf_val_t *keystore
        }
 
        if (ret != KNOT_EOK) {
-               zone_deinit_keystore(keystores);
+               _zone_deinit_keystore(keystores, allocate);
        }
 
        free(zone_path);