From 6afa99362af921c933021db8f9f80dc32a53a46c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Mon, 15 Jul 2019 11:19:03 +0200 Subject: [PATCH] Remove duplicate INSIST checks for isc_refcount API This commits removes superfluous checks when using the isc_refcount API. Examples of superfluous checks: 1. The isc_refcount_decrement function ensures there was not underflow, so this check is superfluous: INSIST(isc_refcount_decrement(&r) > 0); 2 .The isc_refcount_destroy() includes check whether the counter is zero, therefore this is superfluous: INSIST(isc_refcount_decrement(&r) == 1 && isc_refcount_destroy(&r)); --- lib/dns/rbtdb.c | 4 ++-- lib/dns/rpz.c | 6 +++--- lib/dns/tsig.c | 2 +- lib/dns/zone.c | 17 ++++++++--------- lib/isc/mem.c | 4 ++-- lib/isc/unix/socket.c | 2 +- lib/ns/interfacemgr.c | 2 +- 7 files changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/dns/rbtdb.c b/lib/dns/rbtdb.c index e54a000a2a..266e0213dd 100644 --- a/lib/dns/rbtdb.c +++ b/lib/dns/rbtdb.c @@ -8322,7 +8322,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) { - INSIST(isc_refcount_decrement(&rbtdb->references) > 0); + isc_refcount_decrement(&rbtdb->references); free_rbtdb(rbtdb, false, NULL); return (ISC_R_NOMEMORY); } @@ -8343,7 +8343,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; - INSIST(isc_refcount_decrement(&rbtdb->references) > 0); + isc_refcount_decrement(&rbtdb->references); free_rbtdb(rbtdb, false, NULL); return (result); } diff --git a/lib/dns/rpz.c b/lib/dns/rpz.c index 5ee338f7c3..018ab5c16a 100644 --- a/lib/dns/rpz.c +++ b/lib/dns/rpz.c @@ -1484,9 +1484,9 @@ cleanup_task: dns_rbt_destroy(&zones->rbt); cleanup_rbt: - INSIST(isc_refcount_decrement(&zones->irefs) == 1); + isc_refcount_decrement(&zones->irefs); isc_refcount_destroy(&zones->irefs); - INSIST(isc_refcount_decrement(&zones->refs) == 1); + isc_refcount_decrement(&zones->refs); isc_refcount_destroy(&zones->refs); isc_mutex_destroy(&zones->maint_lock); @@ -1567,7 +1567,7 @@ cleanup_ht: isc_timer_detach(&zone->updatetimer); cleanup_timer: - INSIST(isc_refcount_decrement(&zone->refs) > 0); + isc_refcount_decrement(&zone->refs); isc_refcount_destroy(&zone->refs); isc_mem_put(rpzs->mctx, zone, sizeof(*zone)); diff --git a/lib/dns/tsig.c b/lib/dns/tsig.c index 71f8f19998..83f76b97dd 100644 --- a/lib/dns/tsig.c +++ b/lib/dns/tsig.c @@ -349,7 +349,7 @@ dns_tsigkey_createfromkey(const dns_name_t *name, const dns_name_t *algorithm, cleanup_refs: tkey->magic = 0; while (refs-- > 0) { - INSIST(isc_refcount_decrement(&tkey->refs) > 0); + isc_refcount_decrement(&tkey->refs); } isc_refcount_destroy(&tkey->refs); diff --git a/lib/dns/zone.c b/lib/dns/zone.c index db2be499db..1de8b8283e 100644 --- a/lib/dns/zone.c +++ b/lib/dns/zone.c @@ -1086,7 +1086,7 @@ dns_zone_create(dns_zone_t **zonep, isc_mem_t *mctx) { return (ISC_R_SUCCESS); free_refs: - INSIST(isc_refcount_decrement(&zone->erefs) > 0); + isc_refcount_decrement(&zone->erefs); isc_refcount_destroy(&zone->erefs); isc_refcount_destroy(&zone->irefs); @@ -1111,8 +1111,8 @@ zone_free(dns_zone_t *zone) { dns_include_t *include; REQUIRE(DNS_ZONE_VALID(zone)); - REQUIRE(isc_refcount_current(&zone->erefs) == 0); - REQUIRE(isc_refcount_current(&zone->irefs) == 0); + isc_refcount_destroy(&zone->erefs); + isc_refcount_destroy(&zone->irefs); REQUIRE(!LOCKED_ZONE(zone)); REQUIRE(zone->timer == NULL); REQUIRE(zone->zmgr == NULL); @@ -5471,7 +5471,7 @@ dns_zone_detach(dns_zone_t **zonep) { void dns_zone_iattach(dns_zone_t *source, dns_zone_t **target) { REQUIRE(DNS_ZONE_VALID(source)); - REQUIRE(target != NULL && *target == NULL); + LOCK_ZONE(source); zone_iattach(source, target); UNLOCK_ZONE(source); @@ -5479,10 +5479,6 @@ dns_zone_iattach(dns_zone_t *source, dns_zone_t **target) { static void zone_iattach(dns_zone_t *source, dns_zone_t **target) { - - /* - * 'source' locked by caller. - */ REQUIRE(DNS_ZONE_VALID(source)); REQUIRE(LOCKED_ZONE(source)); REQUIRE(target != NULL && *target == NULL); @@ -5491,6 +5487,7 @@ zone_iattach(dns_zone_t *source, dns_zone_t **target) { *target = source; } + static void zone_idetach(dns_zone_t **zonep) { dns_zone_t *zone; @@ -5499,8 +5496,9 @@ zone_idetach(dns_zone_t **zonep) { * 'zone' locked by caller. */ REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep)); - zone = *zonep; REQUIRE(LOCKED_ZONE(*zonep)); + + zone = *zonep; *zonep = NULL; INSIST(isc_refcount_decrement(&zone->irefs) - 1 + @@ -5513,6 +5511,7 @@ dns_zone_idetach(dns_zone_t **zonep) { bool free_needed; REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep)); + zone = *zonep; *zonep = NULL; diff --git a/lib/isc/mem.c b/lib/isc/mem.c index fc7a68e31a..d471891033 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -982,11 +982,11 @@ isc_mem_destroy(isc_mem_t **ctxp) { REQUIRE(VALID_CONTEXT(ctx)); #if ISC_MEM_TRACKLINES - if (isc_refcount_decrement(&ctx->references) != 1) { + if (isc_refcount_decrement(&ctx->references) > 1) { print_active(ctx, stderr); } #else - INSIST(isc_refcount_decrement(&ctx->references) == 1); + isc_refcount_decrement(&ctx->references); #endif isc_refcount_destroy(&ctx->references); destroy(ctx); diff --git a/lib/isc/unix/socket.c b/lib/isc/unix/socket.c index c17965501a..eebce42f89 100644 --- a/lib/isc/unix/socket.c +++ b/lib/isc/unix/socket.c @@ -1933,7 +1933,7 @@ free_socket(isc__socket_t **socketp) { isc__socket_t *sock = *socketp; INSIST(VALID_SOCKET(sock)); - INSIST(isc_refcount_current(&sock->references) == 0); + isc_refcount_destroy(&sock->references); LOCK(&sock->lock); INSIST(!sock->connecting); INSIST(ISC_LIST_EMPTY(sock->recv_list)); diff --git a/lib/ns/interfacemgr.c b/lib/ns/interfacemgr.c index 34a16be604..df1fe63aab 100644 --- a/lib/ns/interfacemgr.c +++ b/lib/ns/interfacemgr.c @@ -340,7 +340,7 @@ ns_interfacemgr_getaclenv(ns_interfacemgr_t *mgr) { void ns_interfacemgr_attach(ns_interfacemgr_t *source, ns_interfacemgr_t **target) { REQUIRE(NS_INTERFACEMGR_VALID(source)); - INSIST(isc_refcount_increment(&source->references) > 0); + isc_refcount_increment(&source->references); *target = source; }