]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Use isc_mem_regetx() when appropriate
authorOndřej Surý <ondrej@isc.org>
Fri, 26 Aug 2022 09:58:51 +0000 (11:58 +0200)
committerOndřej Surý <ondrej@isc.org>
Wed, 5 Oct 2022 14:44:05 +0000 (16:44 +0200)
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.

bin/named/config.c
lib/dns/acl.c
lib/dns/resolver.c

index 67ef953119d2c3b02e820000c36faa2e4187959a..4837a1e19b0b970dbe07ea3ceb081de2ea2ffcb5 100644 (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
index 2030f75a72cf2e8b39772f5b7abeb47f5cddf4a0..c43713f10e3ab288c0033b29851febe61d082cdd 100644 (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);
index e2da3ae5bba2da12df8270e09abe974b55586699..6cb02c410de86c5d4799484bcea8ec6a82559501 100644 (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 (result != ISC_R_SUCCESS && result != ISC_R_EXISTS) {
+               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 algorithms is set, algorithms[0] contains its
-                * length.
+                * 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.
                 */
-               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;
-               }
+               node->data = algorithms =
+                       isc_mem_regetx(resolver->mctx, algorithms,
+                                      algorithms_len, len, ISC_MEM_ZERO);
+               /* store the new length */
+               algorithms[0] = len;
        }
-       result = ISC_R_SUCCESS;
-cleanup:
-       return (result);
+
+       algorithms[len - 1] |= mask;
+       return (ISC_R_SUCCESS);
 }
 
 bool