]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Refactor *_destroy and *_detach functions to unified order of actions.
authorOndřej Surý <ondrej@sury.org>
Tue, 28 Aug 2018 08:18:59 +0000 (10:18 +0200)
committerOndřej Surý <ondrej@sury.org>
Tue, 28 Aug 2018 11:15:59 +0000 (13:15 +0200)
This properly orders clearing the freed pointer and calling isc_refcount_destroy
as early as possible to have ability to put proper memory barrier when cleaning
up reference counting.

20 files changed:
bin/tests/system/dyndb/driver/db.c
lib/dns/acl.c
lib/dns/catz.c
lib/dns/dnstap.c
lib/dns/dst_api.c
lib/dns/iptable.c
lib/dns/keytable.c
lib/dns/nta.c
lib/dns/order.c
lib/dns/portlist.c
lib/dns/rbtdb.c
lib/dns/rpz.c
lib/dns/tsig.c
lib/dns/view.c
lib/dns/zone.c
lib/isc/mem.c
lib/isc/radix.c
lib/isccfg/aclconf.c
lib/isccfg/parser.c
lib/ns/server.c

index 8df5154d650d578e261012b58808ce2c9bd13508..fd0f998b74a5f9f93bb20c485e85cbb286cf9daf 100644 (file)
@@ -87,14 +87,13 @@ free_sampledb(sampledb_t *sampledb) {
 
 static void
 detach(dns_db_t **dbp) {
+       REQUIRE(dbp != NULL && VALID_SAMPLEDB((sampledb_t *)(*dbp)));
        sampledb_t *sampledb = (sampledb_t *)(*dbp);
-
-       REQUIRE(VALID_SAMPLEDB(sampledb));
+       *dbp = NULL;
 
        if (isc_refcount_decrement(&sampledb->refs) == 1) {
                free_sampledb(sampledb);
        }
-       *dbp = NULL;
 }
 
 /*
index 2502a14f3f0f291820f81f4093a986594d20bca5..69d3b524bdc8e3848e2f80fb4f47cb04a828ae67 100644 (file)
@@ -482,15 +482,13 @@ destroy(dns_acl_t *dacl) {
 
 void
 dns_acl_detach(dns_acl_t **aclp) {
+       REQUIRE(aclp != NULL && DNS_ACL_VALID(*aclp));
        dns_acl_t *acl = *aclp;
-
-       REQUIRE(DNS_ACL_VALID(acl));
+       *aclp = NULL;
 
        if (isc_refcount_decrement(&acl->refcount) == 1) {
                destroy(acl);
        }
-
-       *aclp = NULL;
 }
 
 
index 82c6a70646c5664f5769c355fc65a6ccbe92051b..cabaec822eb8fd22fc02ee4bbf1d5a0f6c46038c 100644 (file)
@@ -239,24 +239,18 @@ dns_catz_entry_attach(dns_catz_entry_t *entry, dns_catz_entry_t **entryp) {
 
 void
 dns_catz_entry_detach(dns_catz_zone_t *zone, dns_catz_entry_t **entryp) {
-       dns_catz_entry_t *entry;
-       isc_mem_t *mctx;
-
        REQUIRE(entryp != NULL && *entryp != NULL);
-
-       entry = *entryp;
-
-       mctx = zone->catzs->mctx;
+       dns_catz_entry_t *entry = *entryp;
+       *entryp = NULL;
 
        if (isc_refcount_decrement(&entry->refs) == 1) {
+               isc_refcount_destroy(&entry->refs);
+               isc_mem_t *mctx = zone->catzs->mctx;
                dns_catz_options_free(&entry->opts, mctx);
                if (dns_name_dynamic(&entry->name))
                        dns_name_free(&entry->name, mctx);
-               isc_refcount_destroy(&entry->refs);
                isc_mem_put(mctx, entry, sizeof(dns_catz_entry_t));
        }
-
-       *entryp = NULL;
 }
 
 bool
@@ -741,18 +735,15 @@ dns_catz_zone_attach(dns_catz_zone_t *zone, dns_catz_zone_t **zonep) {
 
 void
 dns_catz_zone_detach(dns_catz_zone_t **zonep) {
-       isc_result_t result;
-       dns_catz_zone_t *zone;
-       isc_ht_iter_t *iter = NULL;
-       isc_mem_t *mctx;
-
        REQUIRE(zonep != NULL && *zonep != NULL);
-
-       zone = *zonep;
+       dns_catz_zone_t *zone = *zonep;
+       *zonep = NULL;
 
        if (isc_refcount_decrement(&zone->refs) == 1) {
                isc_refcount_destroy(&zone->refs);
                if (zone->entries != NULL) {
+                       isc_ht_iter_t *iter = NULL;
+                       isc_result_t result;
                        result = isc_ht_iter_create(zone->entries, &iter);
                        INSIST(result == ISC_R_SUCCESS);
                        for (result = isc_ht_iter_first(iter);
@@ -771,13 +762,14 @@ dns_catz_zone_detach(dns_catz_zone_t **zonep) {
                        INSIST(isc_ht_count(zone->entries) == 0);
                        isc_ht_destroy(&zone->entries);
                }
-               mctx = zone->catzs->mctx;
+               isc_mem_t *mctx = zone->catzs->mctx;
                isc_timer_detach(&zone->updatetimer);
                if (zone->db_registered == true) {
-                       result = dns_db_updatenotify_unregister(zone->db,
-                                                   dns_catz_dbupdate_callback,
-                                                   zone->catzs);
-                       INSIST(result == ISC_R_SUCCESS);
+                       INSIST(dns_db_updatenotify_unregister(
+                                      zone->db,
+                                      dns_catz_dbupdate_callback,
+                                      zone->catzs)
+                              == ISC_R_SUCCESS);
                }
                if (zone->dbversion)
                        dns_db_closeversion(zone->db, &zone->dbversion,
@@ -792,32 +784,28 @@ dns_catz_zone_detach(dns_catz_zone_t **zonep) {
                zone->catzs = NULL;
                isc_mem_put(mctx, zone, sizeof(dns_catz_zone_t));
        }
-
-       *zonep = NULL;
 }
 
 void
-dns_catz_catzs_detach(dns_catz_zones_t ** catzsp) {
-       dns_catz_zones_t *catzs;
-       isc_ht_iter_t *iter = NULL;
-       isc_result_t result;
-       dns_catz_zone_t *zone;
-
+dns_catz_catzs_detach(dns_catz_zones_t **catzsp) {
+       REQUIRE(catzsp != NULL && *catzsp != NULL);
+       dns_catz_zones_t *catzs = *catzsp;
 
-       REQUIRE(catzsp != NULL);
        catzs = *catzsp;
-       REQUIRE(catzs != NULL);
-
        *catzsp = NULL;
+
        if (isc_refcount_decrement(&catzs->refs) == 1) {
                isc_refcount_destroy(&catzs->refs);
                DESTROYLOCK(&catzs->lock);
                if (catzs->zones != NULL) {
+                       isc_ht_iter_t *iter = NULL;
+                       isc_result_t result;
                        result = isc_ht_iter_create(catzs->zones, &iter);
                        INSIST(result == ISC_R_SUCCESS);
                        for (result = isc_ht_iter_first(iter);
                             result == ISC_R_SUCCESS;)
                        {
+                               dns_catz_zone_t *zone;
                                isc_ht_iter_current(iter, (void **) &zone);
                                result = isc_ht_iter_delcurrent_next(iter);
                                dns_catz_zone_detach(&zone);
index e50ad13435406b1303207216fc74ab3231c6e9d3..077445d005f85c03b7a28cfbbde83e5e590cc3e9 100644 (file)
@@ -579,17 +579,14 @@ destroy(dns_dtenv_t *env) {
 
 void
 dns_dt_detach(dns_dtenv_t **envp) {
-       dns_dtenv_t *env;
-
        REQUIRE(envp != NULL && VALID_DTENV(*envp));
-
-       env = *envp;
+       dns_dtenv_t *env = *envp;
+       *envp = NULL;
 
        if (isc_refcount_decrement(&env->refcount) == 1) {
+               isc_refcount_destroy(&env->refcount);
                destroy(env);
        }
-
-       *envp = NULL;
 }
 
 static isc_result_t
index 2407c49504f829ed0f45d32aa4b3c8550e4ef0f8..7633f1c54f99f0cf906fcca02c07feb6d4e4cbfb 100644 (file)
@@ -1130,17 +1130,14 @@ dst_key_attach(dst_key_t *source, dst_key_t **target) {
 
 void
 dst_key_free(dst_key_t **keyp) {
-       isc_mem_t *mctx;
-       dst_key_t *key;
-
        REQUIRE(dst_initialized == true);
        REQUIRE(keyp != NULL && VALID_KEY(*keyp));
-
-       key = *keyp;
-       mctx = key->mctx;
+       dst_key_t *key = *keyp;
+       *keyp = NULL;
 
        if (isc_refcount_decrement(&key->refs) == 1) {
                isc_refcount_destroy(&key->refs);
+               isc_mem_t *mctx = key->mctx;
                if (key->keydata.generic != NULL) {
                        INSIST(key->func->destroy != NULL);
                        key->func->destroy(key);
@@ -1157,7 +1154,6 @@ dst_key_free(dst_key_t **keyp) {
                isc_safe_memwipe(key, sizeof(*key));
                isc_mem_putanddetach(&mctx, key, sizeof(*key));
        }
-       *keyp = NULL;
 }
 
 bool
index daaf3f85a35e09fd8d692f0e9587a0bd04538061..36bad9bf587e30adedf16b760921a64bf8773691 100644 (file)
@@ -148,14 +148,14 @@ dns_iptable_attach(dns_iptable_t *source, dns_iptable_t **target) {
 
 void
 dns_iptable_detach(dns_iptable_t **tabp) {
+       REQUIRE(tabp != NULL && DNS_IPTABLE_VALID(*tabp));
        dns_iptable_t *tab = *tabp;
-       REQUIRE(DNS_IPTABLE_VALID(tab));
+       *tabp = NULL;
 
        if (isc_refcount_decrement(&tab->refcount) == 1) {
+               isc_refcount_destroy(&tab->refcount);
                destroy_iptable(tab);
        }
-
-       *tabp = NULL;
 }
 
 static void
@@ -168,7 +168,6 @@ destroy_iptable(dns_iptable_t *dtab) {
                dtab->radix = NULL;
        }
 
-       isc_refcount_destroy(&dtab->refcount);
        dtab->magic = 0;
        isc_mem_putanddetach(&dtab->mctx, dtab, sizeof(*dtab));
 }
index dfe7d35c2b76f23f797e8fa51a512ab332cb995c..9f61f3dc90997402ea222c180b2e3e53b2532c3e 100644 (file)
@@ -125,27 +125,19 @@ dns_keytable_attach(dns_keytable_t *source, dns_keytable_t **targetp) {
 
 void
 dns_keytable_detach(dns_keytable_t **keytablep) {
-       dns_keytable_t *keytable;
-
-       /*
-        * Detach *keytablep from its keytable.
-        */
-
        REQUIRE(keytablep != NULL && VALID_KEYTABLE(*keytablep));
-
-       keytable = *keytablep;
+       dns_keytable_t *keytable = *keytablep;
+       *keytablep = NULL;
 
        if (isc_refcount_decrement(&keytable->references) == 1) {
-               INSIST(isc_refcount_current(&keytable->active_nodes) == 0);
-               isc_refcount_destroy(&keytable->active_nodes);
                isc_refcount_destroy(&keytable->references);
+               isc_refcount_destroy(&keytable->active_nodes);
                dns_rbt_destroy(&keytable->table);
                isc_rwlock_destroy(&keytable->rwlock);
                keytable->magic = 0;
                isc_mem_putanddetach(&keytable->mctx,
                                     keytable, sizeof(*keytable));
        }
-       *keytablep = NULL;
 }
 
 /*%
@@ -587,7 +579,7 @@ dns_keytable_detachkeynode(dns_keytable_t *keytable, dns_keynode_t **keynodep)
        REQUIRE(VALID_KEYTABLE(keytable));
        REQUIRE(keynodep != NULL && VALID_KEYNODE(*keynodep));
 
-       (void)isc_refcount_decrement(&keytable->active_nodes);
+       INSIST(isc_refcount_decrement(&keytable->active_nodes) > 0);
        dns_keynode_detach(keytable->mctx, keynodep);
 }
 
@@ -745,7 +737,7 @@ dns_keytable_forall(dns_keytable_t *keytable,
                        break;
                }
        }
-       (void)isc_refcount_decrement(&keytable->active_nodes);
+       INSIST(isc_refcount_decrement(&keytable->active_nodes) > 0);
 
    cleanup:
        dns_rbtnodechain_invalidate(&chain);
@@ -812,15 +804,17 @@ dns_keynode_attach(dns_keynode_t *source, dns_keynode_t **target) {
 
 void
 dns_keynode_detach(isc_mem_t *mctx, dns_keynode_t **keynode) {
+       REQUIRE(keynode != NULL && VALID_KEYNODE(*keynode));
        dns_keynode_t *node = *keynode;
-       REQUIRE(VALID_KEYNODE(node));
+       *keynode = NULL;
+
        if (isc_refcount_decrement(&node->refcount) == 1) {
-               if (node->key != NULL)
-                       dst_key_free(&node->key);
                isc_refcount_destroy(&node->refcount);
+               if (node->key != NULL) {
+                       dst_key_free(&node->key);
+               }
                isc_mem_put(mctx, node, sizeof(dns_keynode_t));
        }
-       *keynode = NULL;
 }
 
 void
index 8f32ae622994bdd6d9048d8c3f2f468e0079429b..194688f86526ef25ae70222753ae6f58d98a73a2 100644 (file)
@@ -66,30 +66,31 @@ nta_ref(dns_nta_t *nta) {
 
 static void
 nta_detach(isc_mem_t *mctx, dns_nta_t **ntap) {
+       REQUIRE(ntap != NULL && VALID_NTA(*ntap));
        dns_nta_t *nta = *ntap;
-
-       REQUIRE(VALID_NTA(nta));
+       *ntap = NULL;
 
        if (isc_refcount_decrement(&nta->refcount) == 1) {
+               isc_refcount_destroy(&nta->refcount);
                nta->magic = 0;
                if (nta->timer != NULL) {
-                       (void) isc_timer_reset(nta->timer,
-                                              isc_timertype_inactive,
-                                              NULL, NULL, true);
+                       (void)isc_timer_reset(nta->timer,
+                                             isc_timertype_inactive,
+                                             NULL, NULL, true);
                        isc_timer_detach(&nta->timer);
                }
-               isc_refcount_destroy(&nta->refcount);
-               if (dns_rdataset_isassociated(&nta->rdataset))
+               if (dns_rdataset_isassociated(&nta->rdataset)) {
                        dns_rdataset_disassociate(&nta->rdataset);
-               if (dns_rdataset_isassociated(&nta->sigrdataset))
+               }
+               if (dns_rdataset_isassociated(&nta->sigrdataset)) {
                        dns_rdataset_disassociate(&nta->sigrdataset);
+               }
                if (nta->fetch != NULL) {
                        dns_resolver_cancelfetch(nta->fetch);
                        dns_resolver_destroyfetch(&nta->fetch);
                }
                isc_mem_put(mctx, nta, sizeof(dns_nta_t));
        }
-       *ntap = NULL;
 }
 
 static void
index 26563af8b3d94fc6459294d8e3090a6ab8029206..f8918f9dc26f9fc6214d2736114652559a329ada 100644 (file)
@@ -134,22 +134,19 @@ dns_order_attach(dns_order_t *source, dns_order_t **target) {
 
 void
 dns_order_detach(dns_order_t **orderp) {
+       REQUIRE(orderp != NULL && DNS_ORDER_VALID(*orderp));
        dns_order_t *order;
-       dns_order_ent_t *ent;
-
-       REQUIRE(orderp != NULL);
        order = *orderp;
-       REQUIRE(DNS_ORDER_VALID(order));
+       *orderp = NULL;
 
        if (isc_refcount_decrement(&order->references) == 1) {
+               isc_refcount_destroy(&order->references);
                order->magic = 0;
+               dns_order_ent_t *ent;
                while ((ent = ISC_LIST_HEAD(order->ents)) != NULL) {
                        ISC_LIST_UNLINK(order->ents, ent, link);
                        isc_mem_put(order->mctx, ent, sizeof(*ent));
                }
-               isc_refcount_destroy(&order->references);
                isc_mem_putanddetach(&order->mctx, order, sizeof(*order));
        }
-
-       *orderp = NULL;
 }
index 92315b7a3e9e9f1f3acb5003f6920683aaa7c0c5..9fb983f6c5a6275a17de4b1bea5f696140161e4e 100644 (file)
@@ -234,11 +234,9 @@ dns_portlist_attach(dns_portlist_t *portlist, dns_portlist_t **portlistp) {
 
 void
 dns_portlist_detach(dns_portlist_t **portlistp) {
-       dns_portlist_t *portlist;
-
-       REQUIRE(portlistp != NULL);
-       portlist = *portlistp;
-       REQUIRE(DNS_VALID_PORTLIST(portlist));
+       REQUIRE(portlistp != NULL && DNS_VALID_PORTLIST(*portlistp));
+       dns_portlist_t *portlist = *portlistp;
+       *portlistp = NULL;
 
        if (isc_refcount_decrement(&portlist->refcount) == 1) {
                portlist->magic = 0;
@@ -251,5 +249,4 @@ dns_portlist_detach(dns_portlist_t **portlistp) {
                isc_mem_putanddetach(&portlist->mctx, portlist,
                                     sizeof(*portlist));
        }
-       *portlistp = NULL;
 }
index e30f1f0ce60e592c921341b3574f22596a3a9671..92d8d0ec727b69c220bf58126de6406436367fa6 100644 (file)
@@ -968,7 +968,9 @@ free_rbtdb(dns_rbtdb_t *rbtdb, bool log, isc_event_t *event) {
 
        if (rbtdb->current_version != NULL) {
 
-               INSIST(isc_refcount_decrement(&rbtdb->current_version->references) == 1);
+               INSIST(isc_refcount_decrement(
+                              &rbtdb->current_version->references)
+                      == 1);
 
                UNLINK(rbtdb->open_versions, rbtdb->current_version, link);
                isc_rwlock_destroy(&rbtdb->current_version->glue_rwlock);
@@ -1179,15 +1181,14 @@ maybe_free_rbtdb(dns_rbtdb_t *rbtdb) {
 
 static void
 detach(dns_db_t **dbp) {
+       REQUIRE(dbp != NULL && VALID_RBTDB((dns_rbtdb_t *)(*dbp)));
        dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(*dbp);
-
-       REQUIRE(VALID_RBTDB(rbtdb));
+       *dbp = NULL;
 
        if (isc_refcount_decrement(&rbtdb->references) == 1) {
+               (void)isc_refcount_current(&rbtdb->references);
                maybe_free_rbtdb(rbtdb);
        }
-
-       *dbp = NULL;
 }
 
 static void
@@ -1991,7 +1992,7 @@ decrement_reference(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
        } else
                write_locked = true;
 
-       isc_refcount_decrement(&nodelock->references);
+       INSIST(isc_refcount_decrement(&nodelock->references) > 0);
 
        if (KEEP_NODE(node, rbtdb))
                goto restore_locks;
@@ -2363,6 +2364,7 @@ cleanup_dead_nodes_callback(isc_task_t *task, isc_event_t *event) {
        else {
                isc_event_free(&event);
                if (isc_refcount_decrement(&rbtdb->references) == 1) {
+                       (void)isc_refcount_current(&rbtdb->references);
                        maybe_free_rbtdb(rbtdb);
                }
        }
@@ -2422,6 +2424,7 @@ closeversion(dns_db_t *db, dns_dbversion_t **versionp, bool commit) {
                        cur_version = rbtdb->current_version;
                        cur_ref = isc_refcount_decrement(&cur_version->references);
                        if (cur_ref == 1) {
+                               (void)isc_refcount_current(&cur_version->references);
                                if (cur_version->serial == rbtdb->least_serial)
                                        INSIST(EMPTY(cur_version->changed_list));
                                UNLINK(rbtdb->open_versions,
@@ -8318,7 +8321,7 @@ dns_rbtdb_create(isc_mem_t *mctx, const dns_name_t *origin, dns_dbtype_t type,
        rbtdb->next_serial = 2;
        rbtdb->current_version = allocate_version(mctx, 1, 1, false);
        if (rbtdb->current_version == NULL) {
-               isc_refcount_decrement(&rbtdb->references);
+               INSIST(isc_refcount_decrement(&rbtdb->references) > 0);
                free_rbtdb(rbtdb, false, NULL);
                return (ISC_R_NOMEMORY);
        }
@@ -8339,7 +8342,7 @@ dns_rbtdb_create(isc_mem_t *mctx, const dns_name_t *origin, dns_dbtype_t type,
                isc_mem_put(mctx, rbtdb->current_version,
                            sizeof(*rbtdb->current_version));
                rbtdb->current_version = NULL;
-               isc_refcount_decrement(&rbtdb->references);
+               INSIST(isc_refcount_decrement(&rbtdb->references) > 0);
                free_rbtdb(rbtdb, false, NULL);
                return (result);
        }
index 030365cd0ede2dd49ec5892704888343aaf9ffa7..5ca068077634d9338c53d2885df4f352d3752046 100644 (file)
@@ -1484,7 +1484,7 @@ cleanup_task:
        dns_rbt_destroy(&zones->rbt);
 
 cleanup_rbt:
-       (void)isc_refcount_decrement(&zones->refs);
+       INSIST(isc_refcount_decrement(&zones->refs) > 0);
        isc_refcount_destroy(&zones->refs);
 
        DESTROYLOCK(&zones->maint_lock);
@@ -1567,7 +1567,7 @@ cleanup_ht:
        isc_timer_detach(&zone->updatetimer);
 
 cleanup_timer:
-       isc_refcount_decrement(&zone->refs);
+       INSIST(isc_refcount_decrement(&zone->refs) > 0);
        isc_refcount_destroy(&zone->refs);
 
        isc_mem_put(zone->rpzs->mctx, zone, sizeof(*zone));
@@ -2048,9 +2048,9 @@ cidr_free(dns_rpz_zones_t *rpzs) {
  */
 static void
 rpz_detach(dns_rpz_zone_t **rpzp, dns_rpz_zones_t *rpzs) {
-       dns_rpz_zone_t *rpz;
-
-       rpz = *rpzp;
+       REQUIRE(rpzp != NULL && *rpzp != NULL);
+       dns_rpz_zone_t *rpz = *rpzp;
+       *rpzp = NULL;
 
        if (isc_refcount_decrement(&rpz->refs) == 1) {
                isc_refcount_destroy(&rpz->refs);
@@ -2086,8 +2086,6 @@ rpz_detach(dns_rpz_zone_t **rpzp, dns_rpz_zones_t *rpzs) {
 
                isc_mem_put(rpzs->mctx, rpz, sizeof(*rpz));
        }
-
-       *rpzp = NULL;
 }
 
 void
@@ -2102,23 +2100,20 @@ dns_rpz_attach_rpzs(dns_rpz_zones_t *rpzs, dns_rpz_zones_t **rpzsp) {
  */
 void
 dns_rpz_detach_rpzs(dns_rpz_zones_t **rpzsp) {
-       dns_rpz_zones_t *rpzs;
-       dns_rpz_zone_t *rpz;
-       dns_rpz_num_t rpz_num;
-
-       REQUIRE(rpzsp != NULL);
-       rpzs = *rpzsp;
-       REQUIRE(rpzs != NULL);
-
+       REQUIRE(rpzsp != NULL && *rpzsp != NULL);
+       dns_rpz_zones_t *rpzs = *rpzsp;
        *rpzsp = NULL;
+
        if (isc_refcount_decrement(&rpzs->refs) == 1) {
                isc_refcount_destroy(&rpzs->refs);
-
                /*
                 * Forget the last of view's rpz machinery after the last reference.
                 */
-               for (rpz_num = 0; rpz_num < DNS_RPZ_MAX_ZONES; ++rpz_num) {
-                       rpz = rpzs->zones[rpz_num];
+               for (dns_rpz_num_t rpz_num = 0;
+                    rpz_num < DNS_RPZ_MAX_ZONES;
+                    ++rpz_num)
+               {
+                       dns_rpz_zone_t *rpz = rpzs->zones[rpz_num];
                        rpzs->zones[rpz_num] = NULL;
                        if (rpz != NULL) {
                                rpz_detach(&rpz, rpzs);
index 529efdfe844cb979db0e021f9d58fa132c3a61d5..71580418ac49c4a1141e711e7426c9e753d5cf8d 100644 (file)
@@ -370,7 +370,7 @@ dns_tsigkey_createfromkey(const dns_name_t *name, const dns_name_t *algorithm,
  cleanup_refs:
        tkey->magic = 0;
        while (refs-- > 0) {
-               (void)isc_refcount_decrement(&tkey->refs);
+               INSIST(isc_refcount_decrement(&tkey->refs) > 0);
        }
        isc_refcount_destroy(&tkey->refs);
 
@@ -729,24 +729,19 @@ tsigkey_free(dns_tsigkey_t *key) {
                dns_name_free(key->creator, key->mctx);
                isc_mem_put(key->mctx, key->creator, sizeof(dns_name_t));
        }
-       isc_refcount_destroy(&key->refs);
        isc_mem_putanddetach(&key->mctx, key, sizeof(dns_tsigkey_t));
 }
 
 void
 dns_tsigkey_detach(dns_tsigkey_t **keyp) {
-       dns_tsigkey_t *key;
-
-       REQUIRE(keyp != NULL);
-       REQUIRE(VALID_TSIG_KEY(*keyp));
-
-       key = *keyp;
+       REQUIRE(keyp != NULL && VALID_TSIG_KEY(*keyp));
+       dns_tsigkey_t *key = *keyp;
+       *keyp = NULL;
 
        if (isc_refcount_decrement(&key->refs) == 1) {
+               isc_refcount_destroy(&key->refs);
                tsigkey_free(key);
        }
-
-       *keyp = NULL;
 }
 
 void
index 751b0d18afbc6466ff32e636745c85ebcf78a7ab..09630e6a8a255d43d9a9865642d082a7f8f411b2 100644 (file)
@@ -311,7 +311,7 @@ dns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
                dns_tsigkeyring_detach(&view->dynamickeys);
 
  cleanup_references:
-       (void)isc_refcount_decrement(&view->references);
+       INSIST(isc_refcount_decrement(&view->references) > 0);
        isc_refcount_destroy(&view->references);
 
        if (view->fwdtable != NULL)
@@ -542,7 +542,6 @@ destroy(dns_view_t *view) {
                dns_badcache_destroy(&view->failcache);
        DESTROYLOCK(&view->new_zone_lock);
        DESTROYLOCK(&view->lock);
-       isc_refcount_destroy(&view->references);
        isc_mem_free(view->mctx, view->nta_file);
        isc_mem_free(view->mctx, view->name);
        isc_mem_putanddetach(&view->mctx, view, sizeof(*view));
@@ -576,36 +575,42 @@ dns_view_attach(dns_view_t *source, dns_view_t **targetp) {
 
 static void
 view_flushanddetach(dns_view_t **viewp, bool flush) {
-       dns_view_t *view;
-       bool done = false;
+       REQUIRE(viewp != NULL && DNS_VIEW_VALID(*viewp));
+       dns_view_t *view = *viewp;
+       *viewp = NULL;
 
-       REQUIRE(viewp != NULL);
-       view = *viewp;
-       REQUIRE(DNS_VIEW_VALID(view));
+       if (flush) {
+               view->flush = flush;
+       }
 
-       if (flush)
-               view->flush = true;
+       bool done = false;
        if (isc_refcount_decrement(&view->references) == 1) {
                dns_zone_t *mkzone = NULL, *rdzone = NULL;
 
+               isc_refcount_destroy(&view->references);
                LOCK(&view->lock);
-               if (!RESSHUTDOWN(view))
+               if (!RESSHUTDOWN(view)) {
                        dns_resolver_shutdown(view->resolver);
-               if (!ADBSHUTDOWN(view))
+               }
+               if (!ADBSHUTDOWN(view)) {
                        dns_adb_shutdown(view->adb);
-               if (!REQSHUTDOWN(view))
+               }
+               if (!REQSHUTDOWN(view)) {
                        dns_requestmgr_shutdown(view->requestmgr);
+               }
                if (view->zonetable != NULL) {
-                       if (view->flush)
+                       if (view->flush) {
                                dns_zt_flushanddetach(&view->zonetable);
-                       else
+                       } else {
                                dns_zt_detach(&view->zonetable);
+                       }
                }
                if (view->managed_keys != NULL) {
                        mkzone = view->managed_keys;
                        view->managed_keys = NULL;
-                       if (view->flush)
+                       if (view->flush) {
                                dns_zone_flush(mkzone);
+                       }
                }
                if (view->redirect != NULL) {
                        rdzone = view->redirect;
@@ -620,17 +625,20 @@ view_flushanddetach(dns_view_t **viewp, bool flush) {
                UNLOCK(&view->lock);
 
                /* Need to detach zones outside view lock */
-               if (mkzone != NULL)
+               if (mkzone != NULL) {
                        dns_zone_detach(&mkzone);
+               }
 
-               if (rdzone != NULL)
+               if (rdzone != NULL) {
                        dns_zone_detach(&rdzone);
+               }
        }
 
        *viewp = NULL;
 
-       if (done)
+       if (done) {
                destroy(view);
+       }
 }
 
 void
index b9e2b83e3e242398b3432fea05148839e9639c52..c350c0747398d0ba1f5509be03069da55c42e616 100644 (file)
@@ -1083,7 +1083,7 @@ dns_zone_create(dns_zone_t **zonep, isc_mem_t *mctx) {
                isc_stats_detach(&zone->gluecachestats);
 
  free_erefs:
-       (void)isc_refcount_decrement(&zone->erefs);
+       INSIST(isc_refcount_decrement(&zone->erefs) > 0);
        isc_refcount_destroy(&zone->erefs);
 
        ZONEDB_DESTROYLOCK(&zone->dblock);
@@ -1256,7 +1256,6 @@ zone_free(dns_zone_t *zone) {
        /* last stuff */
        ZONEDB_DESTROYLOCK(&zone->dblock);
        DESTROYLOCK(&zone->lock);
-       isc_refcount_destroy(&zone->erefs);
        zone->magic = 0;
        mctx = zone->mctx;
        isc_mem_put(mctx, zone, sizeof(*zone));
@@ -5258,16 +5257,16 @@ dns_zone_attach(dns_zone_t *source, dns_zone_t **target) {
 
 void
 dns_zone_detach(dns_zone_t **zonep) {
-       dns_zone_t *zone;
-       dns_zone_t *raw = NULL;
-       dns_zone_t *secure = NULL;
-       bool free_now = false;
-
        REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep));
+       dns_zone_t *zone = *zonep;
+       *zonep = NULL;
 
-       zone = *zonep;
-
+       bool free_now = false;
+       dns_zone_t *raw = NULL;
+       dns_zone_t *secure = NULL;
        if (isc_refcount_decrement(&zone->erefs) == 1) {
+               isc_refcount_destroy(&zone->erefs);
+
                LOCK_ZONE(zone);
                INSIST(zone != zone->raw);
                /*
@@ -5303,12 +5302,13 @@ dns_zone_detach(dns_zone_t **zonep) {
                }
                UNLOCK_ZONE(zone);
        }
-       *zonep = NULL;
        if (free_now) {
-               if (raw != NULL)
+               if (raw != NULL) {
                        dns_zone_detach(&raw);
-               if (secure != NULL)
+               }
+               if (secure != NULL) {
                        dns_zone_idetach(&secure);
+               }
                zone_free(zone);
        }
 }
index 371b2e3f087200dff666c9f2f2bc2364f6a0a320..16609cb75230803af911601e948ca527b3a783cc 100644 (file)
@@ -1049,7 +1049,6 @@ destroy(isc__mem_t *ctx) {
                ctx->malloced -= DEBUG_TABLE_COUNT * sizeof(debuglist_t);
        }
 #endif
-       isc_refcount_destroy(&ctx->references);
 
        if (ctx->checkfree) {
                for (i = 0; i <= ctx->max_size; i++) {
@@ -1106,17 +1105,14 @@ isc__mem_attach(isc_mem_t *source0, isc_mem_t **targetp) {
 
 void
 isc__mem_detach(isc_mem_t **ctxp) {
-       isc__mem_t *ctx;
-
-       REQUIRE(ctxp != NULL);
-       ctx = (isc__mem_t *)*ctxp;
-       REQUIRE(VALID_CONTEXT(ctx));
+       REQUIRE(ctxp != NULL && VALID_CONTEXT(*ctxp));
+       isc__mem_t *ctx = (isc__mem_t *)*ctxp;
+       *ctxp = NULL;
 
        if (isc_refcount_decrement(&ctx->references) == 1) {
+               isc_refcount_destroy(&ctx->references);
                destroy(ctx);
        }
-
-       *ctxp = NULL;
 }
 
 /*
@@ -1131,38 +1127,24 @@ isc__mem_detach(isc_mem_t **ctxp) {
 
 void
 isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) {
-       isc__mem_t *ctx;
-       size_info *si;
-       size_t oldsize;
-
-       REQUIRE(ctxp != NULL);
-       ctx = (isc__mem_t *)*ctxp;
-       REQUIRE(VALID_CONTEXT(ctx));
+       REQUIRE(ctxp != NULL && VALID_CONTEXT(*ctxp));
        REQUIRE(ptr != NULL);
-
-       /*
-        * Must be before mem_putunlocked() as ctxp is usually within
-        * [ptr..ptr+size).
-        */
+       isc__mem_t *ctx = (isc__mem_t *)*ctxp;
        *ctxp = NULL;
 
        if (ISC_UNLIKELY((isc_mem_debugging &
                          (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0))
        {
                if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
-                       si = &(((size_info *)ptr)[-1]);
-                       oldsize = si->u.size - ALIGNMENT_SIZE;
+                       size_info *si = &(((size_info *)ptr)[-1]);
+                       size_t oldsize = si->u.size - ALIGNMENT_SIZE;
                        if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
                                oldsize -= ALIGNMENT_SIZE;
                        INSIST(oldsize == size);
                }
                isc__mem_free((isc_mem_t *)ctx, ptr FLARG_PASS);
 
-               if (isc_refcount_decrement(&ctx->references) == 1) {
-                       destroy(ctx);
-               }
-
-               return;
+               goto destroy;
        }
 
        MCTXLOCK(ctx, &ctx->lock);
@@ -1177,7 +1159,9 @@ isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) {
        }
        MCTXUNLOCK(ctx, &ctx->lock);
 
+destroy:
        if (isc_refcount_decrement(&ctx->references) == 1) {
+               isc_refcount_destroy(&ctx->references);
                destroy(ctx);
        }
 }
@@ -1200,7 +1184,7 @@ isc__mem_destroy(isc_mem_t **ctxp) {
                print_active(ctx, stderr);
        }
 #else
-       (void)isc_refcount_decrement(&ctx->references);
+       INSIST(isc_refcount_decrement(&ctx->references) == 1);
 #endif
        isc_refcount_destroy(&ctx->references);
        destroy(ctx);
index 040a386f6961ae67dbdcbdfd89f99835d5d5b2f1..8f5de8217fa9e491fac79fc791a9b151dec54d97 100644 (file)
@@ -76,13 +76,12 @@ _new_prefix(isc_mem_t *mctx, isc_prefix_t **target, int family, void *dest,
 
 static void
 _deref_prefix(isc_prefix_t *prefix) {
-       if (prefix == NULL)
-               return;
-
-       if (isc_refcount_decrement(&prefix->refcount) == 1) {
-               isc_refcount_destroy(&prefix->refcount);
-               isc_mem_putanddetach(&prefix->mctx, prefix,
-                                    sizeof(isc_prefix_t));
+       if (prefix != NULL) {
+               if (isc_refcount_decrement(&prefix->refcount) == 1) {
+                       isc_refcount_destroy(&prefix->refcount);
+                       isc_mem_putanddetach(&prefix->mctx, prefix,
+                                            sizeof(isc_prefix_t));
+               }
        }
 }
 
@@ -706,10 +705,3 @@ isc_radix_remove(isc_radix_tree_t *radix, isc_radix_node_t *node) {
                parent->l = child;
        }
 }
-
-/*
-Local Variables:
-c-basic-offset: 4
-indent-tabs-mode: t
-End:
-*/
index 4d43fabce171e8f8714e7c8168294dd17301c575..02c18209849aee0ba6d8821de80e3fe00c514443 100644 (file)
@@ -70,14 +70,13 @@ cfg_aclconfctx_attach(cfg_aclconfctx_t *src, cfg_aclconfctx_t **dest) {
 
 void
 cfg_aclconfctx_detach(cfg_aclconfctx_t **actxp) {
-       cfg_aclconfctx_t *actx;
-       dns_acl_t *dacl, *next;
-
        REQUIRE(actxp != NULL && *actxp != NULL);
-
-       actx = *actxp;
+       cfg_aclconfctx_t *actx = *actxp;
+       *actxp = NULL;
 
        if (isc_refcount_decrement(&actx->references) == 1) {
+               dns_acl_t *dacl, *next;
+               isc_refcount_destroy(&actx->references);
                for (dacl = ISC_LIST_HEAD(actx->named_acl_cache);
                     dacl != NULL;
                     dacl = next)
@@ -89,8 +88,6 @@ cfg_aclconfctx_detach(cfg_aclconfctx_t **actxp) {
                }
                isc_mem_putanddetach(&actx->mctx, actx, sizeof(*actx));
        }
-
-       *actxp = NULL;
 }
 
 /*
index 656a0f683ba4764d302cabdd23a2964dba60688e..c370b1980eedcfcf1b4a5c565a0c1f4c06a3d6ed 100644 (file)
@@ -675,7 +675,6 @@ cfg_parser_destroy(cfg_parser_t **pctxp) {
        cfg_parser_t *pctx;
 
        REQUIRE(pctxp != NULL && *pctxp != NULL);
-
        pctx = *pctxp;
        *pctxp = NULL;
 
@@ -3132,19 +3131,17 @@ cfg_obj_istype(const cfg_obj_t *obj, const cfg_type_t *type) {
  */
 void
 cfg_obj_destroy(cfg_parser_t *pctx, cfg_obj_t **objp) {
-       cfg_obj_t *obj;
-
        REQUIRE(objp != NULL && *objp != NULL);
        REQUIRE(pctx != NULL);
 
-       obj = *objp;
+       cfg_obj_t *obj = *objp;
+       *objp = NULL;
 
        if (isc_refcount_decrement(&obj->references) == 1) {
                obj->type->rep->free(pctx, obj);
                isc_refcount_destroy(&obj->references);
                isc_mem_put(pctx->mctx, obj, sizeof(cfg_obj_t));
        }
-       *objp = NULL;
 }
 
 void
index 4a133f3c51c0b59d9f62ccbf9624ad2ce67f7bac..91ae32d69236103ef9721ca1b2424074408681bd 100644 (file)
@@ -127,9 +127,9 @@ void
 ns_server_detach(ns_server_t **sctxp) {
        ns_server_t *sctx;
 
-       REQUIRE(sctxp != NULL);
+       REQUIRE(sctxp != NULL && SCTX_VALID(*sctxp));
        sctx = *sctxp;
-       REQUIRE(SCTX_VALID(sctx));
+       *sctxp = NULL;
 
        if (isc_refcount_decrement(&sctx->references) == 1) {
                ns_altsecret_t *altsecret;
@@ -185,8 +185,6 @@ ns_server_detach(ns_server_t **sctxp) {
 
                isc_mem_putanddetach(&sctx->mctx, sctx, sizeof(*sctx));
        }
-
-       *sctxp = NULL;
 }
 
 isc_result_t