2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 10:10:06 +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

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

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->elements = isc_mem_getx(mctx, n * sizeof(dns_aclelement_t),
acl->elements = isc_mem_getx(mctx, n * sizeof(acl->elements[0]),
ISC_MEM_ZERO);
acl->alloc = n;
ISC_LIST_INIT(acl->ports_and_transports);
@ -306,30 +306,20 @@ dns_acl_match_port_transport(const isc_netaddr_t *reqaddr,
isc_result_t
dns_acl_merge(dns_acl_t *dest, dns_acl_t *source, bool pos) {
isc_result_t result;
unsigned int newalloc, nelem, i;
unsigned int nelem, i;
int max_node = 0, nodes;
/* Resize the element array if needed. */
if (dest->length + source->length > dest->alloc) {
void *newmem;
newalloc = dest->alloc + source->alloc;
size_t newalloc = dest->alloc + source->alloc;
if (newalloc < 4) {
newalloc = 4;
}
newmem = isc_mem_getx(dest->mctx,
newalloc * sizeof(dns_aclelement_t),
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->elements = isc_mem_regetx(
dest->mctx, dest->elements,
dest->alloc * sizeof(dest->elements[0]),
newalloc * sizeof(dest->elements[0]), ISC_MEM_ZERO);
dest->alloc = newalloc;
}
@ -532,7 +522,7 @@ destroy(dns_acl_t *dacl) {
}
if (dacl->elements != NULL) {
isc_mem_put(dacl->mctx, dacl->elements,
dacl->alloc * sizeof(dns_aclelement_t));
dacl->alloc * sizeof(dacl->elements[0]));
}
if (dacl->name != NULL) {
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,
unsigned int alg) {
unsigned int len, mask;
unsigned char *tmp;
unsigned char *algorithms;
isc_result_t result;
dns_rbtnode_t *node = NULL;
unsigned char *algorithms = NULL;
unsigned int algorithms_len;
/*
* 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,
resolver->mctx, &resolver->algorithms);
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);
if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
algorithms = node->data;
/*
* If algorithms is set, algorithms[0] contains its
* length.
*/
if (algorithms == NULL || len > *algorithms) {
/*
* If no bitfield exists in the node data, or if
* it is not long enough, allocate a new
* bitfield and copy the old (smaller) bitfield
* into it if one exists.
*/
tmp = isc_mem_getx(resolver->mctx, len, ISC_MEM_ZERO);
if (algorithms != NULL) {
memmove(tmp, algorithms, *algorithms);
}
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;
}
if (result != ISC_R_SUCCESS && result != ISC_R_EXISTS) {
return (result);
}
result = ISC_R_SUCCESS;
cleanup:
return (result);
/* If algorithms is set, algorithms[0] contains its length. */
algorithms = node->data;
algorithms_len = (algorithms) ? algorithms[0] : 0;
if (algorithms == NULL || len > algorithms_len) {
INSIST(len > 0);
/*
* If no bitfield exists in the node data, or if
* it is not long enough, allocate a new
* bitfield and copy the old (smaller) bitfield
* into it if one exists.
*/
node->data = algorithms =
isc_mem_regetx(resolver->mctx, algorithms,
algorithms_len, len, ISC_MEM_ZERO);
/* store the new length */
algorithms[0] = len;
}
algorithms[len - 1] |= mask;
return (ISC_R_SUCCESS);
}
bool