From b3a8f0048f1f45da9f13638d0545ce9ff144e615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Fri, 13 Oct 2023 08:17:31 +0200 Subject: [PATCH] Refactor dns_{acl,aclenv}_create to return void The dns_{acl,aclenv}_create() can't fail, so change it to return void. --- bin/named/server.c | 5 +-- lib/dns/acl.c | 83 +++++++++------------------------------ lib/dns/include/dns/acl.h | 4 +- lib/dns/view.c | 10 +---- lib/isccfg/aclconf.c | 13 ++---- lib/ns/interfacemgr.c | 22 ++--------- tests/dns/acl_test.c | 12 ++---- 7 files changed, 33 insertions(+), 116 deletions(-) diff --git a/bin/named/server.c b/bin/named/server.c index 3541a79bdf..f23e28547a 100644 --- a/bin/named/server.c +++ b/bin/named/server.c @@ -3878,10 +3878,7 @@ create_mapped_acl(void) { isc_netaddr_fromin6(&addr, &in6); - result = dns_acl_create(named_g_mctx, 1, &acl); - if (result != ISC_R_SUCCESS) { - return (result); - } + dns_acl_create(named_g_mctx, 1, &acl); result = dns_iptable_addprefix(acl->iptable, &addr, 96, true); if (result == ISC_R_SUCCESS) { diff --git a/lib/dns/acl.c b/lib/dns/acl.c index 7f48193143..45d19a563a 100644 --- a/lib/dns/acl.c +++ b/lib/dns/acl.c @@ -32,46 +32,23 @@ * for 'n' ACL elements. The elements are uninitialized and the * length is 0. */ -isc_result_t +void dns_acl_create(isc_mem_t *mctx, int n, dns_acl_t **target) { - dns_acl_t *acl; + REQUIRE(target != NULL && *target == NULL); - /* - * Work around silly limitation of isc_mem_get(). - */ - if (n == 0) { - n = 1; - } + dns_acl_t *acl = isc_mem_get(mctx, sizeof(*acl)); + *acl = (dns_acl_t){ + .refcount = 1, + .nextincache = ISC_LINK_INITIALIZER, + .elements = isc_mem_cget(mctx, n, sizeof(acl->elements[0])), + .alloc = n, + .ports_and_transports = ISC_LIST_INITIALIZER, + .magic = DNS_ACL_MAGIC, + }; - acl = isc_mem_get(mctx, sizeof(*acl)); - - acl->mctx = NULL; isc_mem_attach(mctx, &acl->mctx); - acl->name = NULL; - - isc_refcount_init(&acl->refcount, 1); - - dns_iptable_create(mctx, &acl->iptable); - - acl->elements = NULL; - acl->alloc = 0; - acl->length = 0; - acl->has_negatives = false; - - ISC_LINK_INIT(acl, nextincache); - /* - * Must set magic early because we use dns_acl_detach() to clean up. - */ - acl->magic = DNS_ACL_MAGIC; - - acl->elements = isc_mem_cget(mctx, n, sizeof(acl->elements[0])); - acl->alloc = n; - ISC_LIST_INIT(acl->ports_and_transports); - acl->port_proto_entries = 0; - *target = acl; - return (ISC_R_SUCCESS); } /* @@ -85,10 +62,7 @@ dns_acl_anyornone(isc_mem_t *mctx, bool neg, dns_acl_t **target) { isc_result_t result; dns_acl_t *acl = NULL; - result = dns_acl_create(mctx, 0, &acl); - if (result != ISC_R_SUCCESS) { - return (result); - } + dns_acl_create(mctx, 0, &acl); result = dns_iptable_addprefix(acl->iptable, NULL, 0, !neg); if (result != ISC_R_SUCCESS) { @@ -681,41 +655,22 @@ dns_acl_allowed(isc_netaddr_t *addr, const dns_name_t *signer, dns_acl_t *acl, /* * Initialize ACL environment, setting up localhost and localnets ACLs */ -isc_result_t +void dns_aclenv_create(isc_mem_t *mctx, dns_aclenv_t **envp) { - isc_result_t result; dns_aclenv_t *env = isc_mem_get(mctx, sizeof(*env)); - *env = (dns_aclenv_t){ 0 }; + *env = (dns_aclenv_t){ + .references = 1, + .magic = DNS_ACLENV_MAGIC, + }; isc_mem_attach(mctx, &env->mctx); isc_refcount_init(&env->references, 1); isc_rwlock_init(&env->rwlock); - result = dns_acl_create(mctx, 0, &env->localhost); - if (result != ISC_R_SUCCESS) { - goto cleanup_rwlock; - } - result = dns_acl_create(mctx, 0, &env->localnets); - if (result != ISC_R_SUCCESS) { - goto cleanup_localhost; - } - env->match_mapped = false; -#if defined(HAVE_GEOIP2) - env->geoip = NULL; -#endif /* if defined(HAVE_GEOIP2) */ - - env->magic = DNS_ACLENV_MAGIC; + dns_acl_create(mctx, 0, &env->localhost); + dns_acl_create(mctx, 0, &env->localnets); *envp = env; - - return (ISC_R_SUCCESS); - -cleanup_localhost: - dns_acl_detach(&env->localhost); -cleanup_rwlock: - isc_rwlock_destroy(&env->rwlock); - isc_mem_putanddetach(&env->mctx, env, sizeof(*env)); - return (result); } void diff --git a/lib/dns/include/dns/acl.h b/lib/dns/include/dns/acl.h index 7bff5ce644..580fd415bd 100644 --- a/lib/dns/include/dns/acl.h +++ b/lib/dns/include/dns/acl.h @@ -120,7 +120,7 @@ struct dns_aclenv { ISC_LANG_BEGINDECLS -isc_result_t +void dns_acl_create(isc_mem_t *mctx, int n, dns_acl_t **target); /*%< * Create a new ACL, including an IP table and an array with room @@ -206,7 +206,7 @@ dns_acl_allowed(isc_netaddr_t *addr, const dns_name_t *signer, dns_acl_t *acl, * permitted by 'acl' in environment 'aclenv'. */ -isc_result_t +void dns_aclenv_create(isc_mem_t *mctx, dns_aclenv_t **envp); /*%< * Create ACL environment, setting up localhost and localnets ACLs diff --git a/lib/dns/view.c b/lib/dns/view.c index 51efd70bb5..bbf68a6502 100644 --- a/lib/dns/view.c +++ b/lib/dns/view.c @@ -157,10 +157,7 @@ dns_view_create(isc_mem_t *mctx, dns_dispatchmgr_t *dispatchmgr, goto cleanup_order; } - result = dns_aclenv_create(view->mctx, &view->aclenv); - if (result != ISC_R_SUCCESS) { - goto cleanup_peerlist; - } + dns_aclenv_create(view->mctx, &view->aclenv); dns_nametree_create(view->mctx, DNS_NAMETREE_COUNT, "sfd", &view->sfd); @@ -169,11 +166,6 @@ dns_view_create(isc_mem_t *mctx, dns_dispatchmgr_t *dispatchmgr, return (ISC_R_SUCCESS); -cleanup_peerlist: - if (view->peers != NULL) { - dns_peerlist_detach(&view->peers); - } - cleanup_order: if (view->order != NULL) { dns_order_detach(&view->order); diff --git a/lib/isccfg/aclconf.c b/lib/isccfg/aclconf.c index 6db68b8271..2914106482 100644 --- a/lib/isccfg/aclconf.c +++ b/lib/isccfg/aclconf.c @@ -685,10 +685,7 @@ cfg_acl_fromconfig(const cfg_obj_t *acl_data, const cfg_obj_t *cctx, nelem = cfg_list_length(caml, false); } - result = dns_acl_create(mctx, nelem, &dacl); - if (result != ISC_R_SUCCESS) { - return (result); - } + dns_acl_create(mctx, nelem, &dacl); } if (is_tuple) { @@ -771,12 +768,8 @@ cfg_acl_fromconfig(const cfg_obj_t *acl_data, const cfg_obj_t *cctx, iptab = dacl->iptable; if (nest_level != 0) { - result = dns_acl_create(mctx, - cfg_list_length(ce, false), - &de->nestedacl); - if (result != ISC_R_SUCCESS) { - goto cleanup; - } + dns_acl_create(mctx, cfg_list_length(ce, false), + &de->nestedacl); iptab = de->nestedacl->iptable; } diff --git a/lib/ns/interfacemgr.c b/lib/ns/interfacemgr.c index 08e70f022c..e6dc3579b8 100644 --- a/lib/ns/interfacemgr.c +++ b/lib/ns/interfacemgr.c @@ -308,10 +308,7 @@ ns_interfacemgr_create(isc_mem_t *mctx, ns_server_t *sctx, } ns_listenlist_attach(mgr->listenon4, &mgr->listenon6); - result = dns_aclenv_create(mctx, &mgr->aclenv); - if (result != ISC_R_SUCCESS) { - goto cleanup_listenon; - } + dns_aclenv_create(mctx, &mgr->aclenv); #if defined(HAVE_GEOIP2) mgr->aclenv->geoip = geoip; #else /* if defined(HAVE_GEOIP2) */ @@ -347,9 +344,6 @@ ns_interfacemgr_create(isc_mem_t *mctx, ns_server_t *sctx, return (ISC_R_SUCCESS); -cleanup_listenon: - ns_listenlist_detach(&mgr->listenon4); - ns_listenlist_detach(&mgr->listenon6); cleanup_lock: isc_mutex_destroy(&mgr->lock); ns_server_detach(&mgr->sctx); @@ -1104,14 +1098,8 @@ do_scan(ns_interfacemgr_t *mgr, bool verbose, bool config) { return (result); } - result = dns_acl_create(mgr->mctx, 0, &localhost); - if (result != ISC_R_SUCCESS) { - goto cleanup_iter; - } - result = dns_acl_create(mgr->mctx, 0, &localnets); - if (result != ISC_R_SUCCESS) { - goto cleanup_localhost; - } + dns_acl_create(mgr->mctx, 0, &localhost); + dns_acl_create(mgr->mctx, 0, &localnets); clearlistenon(mgr); @@ -1292,13 +1280,9 @@ do_scan(ns_interfacemgr_t *mgr, bool verbose, bool config) { dns_aclenv_set(mgr->aclenv, localhost, localnets); - /* cleanup_localnets: */ dns_acl_detach(&localnets); - -cleanup_localhost: dns_acl_detach(&localhost); -cleanup_iter: isc_interfaceiter_destroy(&iter); return (result); } diff --git a/tests/dns/acl_test.c b/tests/dns/acl_test.c index 306921e64a..443dd62748 100644 --- a/tests/dns/acl_test.c +++ b/tests/dns/acl_test.c @@ -56,11 +56,9 @@ ISC_RUN_TEST_IMPL(dns_acl_isinsecure) { result = dns_acl_none(mctx, &none); assert_int_equal(result, ISC_R_SUCCESS); - result = dns_acl_create(mctx, 1, ¬none); - assert_int_equal(result, ISC_R_SUCCESS); + dns_acl_create(mctx, 1, ¬none); - result = dns_acl_create(mctx, 1, ¬any); - assert_int_equal(result, ISC_R_SUCCESS); + dns_acl_create(mctx, 1, ¬any); result = dns_acl_merge(notnone, none, false); assert_int_equal(result, ISC_R_SUCCESS); @@ -69,8 +67,7 @@ ISC_RUN_TEST_IMPL(dns_acl_isinsecure) { assert_int_equal(result, ISC_R_SUCCESS); #if defined(HAVE_GEOIP2) - result = dns_acl_create(mctx, 1, &geoip); - assert_int_equal(result, ISC_R_SUCCESS); + dns_acl_create(mctx, 1, &geoip); de = geoip->elements; assert_non_null(de); @@ -84,8 +81,7 @@ ISC_RUN_TEST_IMPL(dns_acl_isinsecure) { de->node_num = dns_acl_node_count(geoip); geoip->length++; - result = dns_acl_create(mctx, 1, ¬geoip); - assert_int_equal(result, ISC_R_SUCCESS); + dns_acl_create(mctx, 1, ¬geoip); result = dns_acl_merge(notgeoip, geoip, false); assert_int_equal(result, ISC_R_SUCCESS);