2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-24 11:08:45 +00:00

Use isc_mem_regetx() when appropriate

While refactoring the isc_mem_getx(...) usage, couple places were
identified where the memory was resized manually.  Use the
isc_mem_reget(...) that was introduced in [GL !5440] to resize the
arrays via function rather than a custom code.
This commit is contained in:
Ondřej Surý 2022-08-26 11:58:51 +02:00
parent c0598d404c
commit e18b6fb6a6
No known key found for this signature in database
GPG Key ID: 2820F37E873DEA41
3 changed files with 49 additions and 79 deletions

View File

@ -685,29 +685,17 @@ named_config_getname(isc_mem_t *mctx, const cfg_obj_t *obj,
if (newlen >= oldlen) { \ if (newlen >= oldlen) { \
size_t newsize = (newlen + 16) * sizeof(array[0]); \ size_t newsize = (newlen + 16) * sizeof(array[0]); \
size_t oldsize = oldlen * sizeof(array[0]); \ size_t oldsize = oldlen * sizeof(array[0]); \
void *tmp = isc_mem_get(mctx, newsize); \ array = isc_mem_regetx(mctx, array, oldsize, newsize, \
memset(tmp, 0, newsize); \ ISC_MEM_ZERO); \
if (oldlen != 0) { \
memmove(tmp, array, oldsize); \
isc_mem_put(mctx, array, oldsize); \
} \
array = tmp; \
oldlen = newlen + 16; \ oldlen = newlen + 16; \
} }
#define shrink_array(mctx, array, newlen, oldlen) \ #define shrink_array(mctx, array, newlen, oldlen) \
if (newlen < oldlen) { \ if (newlen < oldlen) { \
void *tmp = NULL; \
size_t newsize = newlen * sizeof(array[0]); \ size_t newsize = newlen * sizeof(array[0]); \
size_t oldsize = oldlen * sizeof(array[0]); \ size_t oldsize = oldlen * sizeof(array[0]); \
if (newlen != 0) { \ array = isc_mem_regetx(mctx, array, oldsize, newsize, \
tmp = isc_mem_getx(mctx, newsize, ISC_MEM_ZERO); \ ISC_MEM_ZERO); \
memmove(tmp, array, newsize); \
} else { \
tmp = NULL; \
} \
isc_mem_put(mctx, array, oldsize); \
array = tmp; \
oldlen = newlen; \ oldlen = newlen; \
} }

View File

@ -70,7 +70,7 @@ dns_acl_create(isc_mem_t *mctx, int n, dns_acl_t **target) {
*/ */
acl->magic = DNS_ACL_MAGIC; acl->magic = DNS_ACL_MAGIC;
acl->elements = isc_mem_getx(mctx, n * sizeof(dns_aclelement_t), acl->elements = isc_mem_getx(mctx, n * sizeof(acl->elements[0]),
ISC_MEM_ZERO); ISC_MEM_ZERO);
acl->alloc = n; acl->alloc = n;
ISC_LIST_INIT(acl->ports_and_transports); ISC_LIST_INIT(acl->ports_and_transports);
@ -306,30 +306,20 @@ dns_acl_match_port_transport(const isc_netaddr_t *reqaddr,
isc_result_t isc_result_t
dns_acl_merge(dns_acl_t *dest, dns_acl_t *source, bool pos) { dns_acl_merge(dns_acl_t *dest, dns_acl_t *source, bool pos) {
isc_result_t result; isc_result_t result;
unsigned int newalloc, nelem, i; unsigned int nelem, i;
int max_node = 0, nodes; int max_node = 0, nodes;
/* Resize the element array if needed. */ /* Resize the element array if needed. */
if (dest->length + source->length > dest->alloc) { if (dest->length + source->length > dest->alloc) {
void *newmem; size_t newalloc = dest->alloc + source->alloc;
newalloc = dest->alloc + source->alloc;
if (newalloc < 4) { if (newalloc < 4) {
newalloc = 4; newalloc = 4;
} }
newmem = isc_mem_getx(dest->mctx, dest->elements = isc_mem_regetx(
newalloc * sizeof(dns_aclelement_t), dest->mctx, dest->elements,
ISC_MEM_ZERO); dest->alloc * sizeof(dest->elements[0]),
newalloc * sizeof(dest->elements[0]), ISC_MEM_ZERO);
/* Copy in the original elements */
memmove(newmem, dest->elements,
dest->length * sizeof(dns_aclelement_t));
/* Release the memory for the old elements array */
isc_mem_put(dest->mctx, dest->elements,
dest->alloc * sizeof(dns_aclelement_t));
dest->elements = newmem;
dest->alloc = newalloc; dest->alloc = newalloc;
} }
@ -532,7 +522,7 @@ destroy(dns_acl_t *dacl) {
} }
if (dacl->elements != NULL) { if (dacl->elements != NULL) {
isc_mem_put(dacl->mctx, dacl->elements, isc_mem_put(dacl->mctx, dacl->elements,
dacl->alloc * sizeof(dns_aclelement_t)); dacl->alloc * sizeof(dacl->elements[0]));
} }
if (dacl->name != NULL) { if (dacl->name != NULL) {
isc_mem_free(dacl->mctx, dacl->name); isc_mem_free(dacl->mctx, dacl->name);

View File

@ -11110,10 +11110,10 @@ isc_result_t
dns_resolver_disable_algorithm(dns_resolver_t *resolver, const dns_name_t *name, dns_resolver_disable_algorithm(dns_resolver_t *resolver, const dns_name_t *name,
unsigned int alg) { unsigned int alg) {
unsigned int len, mask; unsigned int len, mask;
unsigned char *tmp;
unsigned char *algorithms;
isc_result_t result; isc_result_t result;
dns_rbtnode_t *node = NULL; dns_rbtnode_t *node = NULL;
unsigned char *algorithms = NULL;
unsigned int algorithms_len;
/* /*
* Whether an algorithm is disabled (or not) is stored in a * Whether an algorithm is disabled (or not) is stored in a
@ -11130,7 +11130,7 @@ dns_resolver_disable_algorithm(dns_resolver_t *resolver, const dns_name_t *name,
result = dns_rbt_create(resolver->mctx, free_algorithm, result = dns_rbt_create(resolver->mctx, free_algorithm,
resolver->mctx, &resolver->algorithms); resolver->mctx, &resolver->algorithms);
if (result != ISC_R_SUCCESS) { if (result != ISC_R_SUCCESS) {
goto cleanup; return (result);
} }
} }
@ -11139,39 +11139,31 @@ dns_resolver_disable_algorithm(dns_resolver_t *resolver, const dns_name_t *name,
result = dns_rbt_addnode(resolver->algorithms, name, &node); result = dns_rbt_addnode(resolver->algorithms, name, &node);
if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) { if (result != ISC_R_SUCCESS && result != ISC_R_EXISTS) {
return (result);
}
/* If algorithms is set, algorithms[0] contains its length. */
algorithms = node->data; algorithms = node->data;
/* algorithms_len = (algorithms) ? algorithms[0] : 0;
* If algorithms is set, algorithms[0] contains its
* length. if (algorithms == NULL || len > algorithms_len) {
*/ INSIST(len > 0);
if (algorithms == NULL || len > *algorithms) {
/* /*
* If no bitfield exists in the node data, or if * If no bitfield exists in the node data, or if
* it is not long enough, allocate a new * it is not long enough, allocate a new
* bitfield and copy the old (smaller) bitfield * bitfield and copy the old (smaller) bitfield
* into it if one exists. * into it if one exists.
*/ */
tmp = isc_mem_getx(resolver->mctx, len, ISC_MEM_ZERO); node->data = algorithms =
if (algorithms != NULL) { isc_mem_regetx(resolver->mctx, algorithms,
memmove(tmp, algorithms, *algorithms); algorithms_len, len, ISC_MEM_ZERO);
/* store the new length */
algorithms[0] = len;
} }
tmp[len - 1] |= mask;
/* tmp[0] should contain the length of 'tmp'. */
*tmp = len;
node->data = tmp;
/* Free the older bitfield. */
if (algorithms != NULL) {
isc_mem_put(resolver->mctx, algorithms,
*algorithms);
}
} else {
algorithms[len - 1] |= mask; algorithms[len - 1] |= mask;
} return (ISC_R_SUCCESS);
}
result = ISC_R_SUCCESS;
cleanup:
return (result);
} }
bool bool