2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-30 05:57:52 +00:00

Refactor dns_{acl,aclenv}_create to return void

The dns_{acl,aclenv}_create() can't fail, so change it to return void.
This commit is contained in:
Ondřej Surý 2023-10-13 08:17:31 +02:00 committed by Ondřej Surý
parent f5b0bd9b1b
commit b3a8f0048f
7 changed files with 33 additions and 116 deletions

View File

@ -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) {

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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, &notnone);
assert_int_equal(result, ISC_R_SUCCESS);
dns_acl_create(mctx, 1, &notnone);
result = dns_acl_create(mctx, 1, &notany);
assert_int_equal(result, ISC_R_SUCCESS);
dns_acl_create(mctx, 1, &notany);
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, &notgeoip);
assert_int_equal(result, ISC_R_SUCCESS);
dns_acl_create(mctx, 1, &notgeoip);
result = dns_acl_merge(notgeoip, geoip, false);
assert_int_equal(result, ISC_R_SUCCESS);