From 52fcc9fc0f14502fa983b62b13c5d377cb35ac43 Mon Sep 17 00:00:00 2001 From: Tony Finch Date: Thu, 8 Jun 2023 12:57:32 +0100 Subject: [PATCH 1/3] Remove some unnecessary token pasting macrology There used to be an extra layer of indirection in the memory functions for certain dynamic linking scenarios. This involved variant spellings like isc__mem and isc___mem. The isc___mem variants were removed in commit 7de846977b2a4fd3 so the token pasting is no longer needed and only serves to obfuscate. --- lib/isc/include/isc/mem.h | 160 ++++++++++++++++++++------------------ 1 file changed, 84 insertions(+), 76 deletions(-) diff --git a/lib/isc/include/isc/mem.h b/lib/isc/include/isc/mem.h index 74083b7b61..8ba4169547 100644 --- a/lib/isc/include/isc/mem.h +++ b/lib/isc/include/isc/mem.h @@ -154,71 +154,66 @@ extern unsigned int isc_mem_defaultflags; #endif #define ISC_MEM_ZERO ((int)0x40) -#define ISCMEMFUNC(sfx) isc__mem_##sfx -#define ISCMEMPOOLFUNC(sfx) isc__mempool_##sfx - -#define isc_mem_get(c, s) ISCMEMFUNC(get)((c), (s), 0 _ISC_MEM_FILELINE) -#define isc_mem_getx(c, s, f) ISCMEMFUNC(get)((c), (s), (f)_ISC_MEM_FILELINE) +#define isc_mem_get(c, s) isc__mem_get((c), (s), 0 _ISC_MEM_FILELINE) +#define isc_mem_getx(c, s, f) isc__mem_get((c), (s), (f)_ISC_MEM_FILELINE) #define isc_mem_reget(c, p, o, n) \ - ISCMEMFUNC(reget)((c), (p), (o), (n), 0 _ISC_MEM_FILELINE) + isc__mem_reget((c), (p), (o), (n), 0 _ISC_MEM_FILELINE) #define isc_mem_regetx(c, p, o, n, f) \ - ISCMEMFUNC(reget)((c), (p), (o), (n), (f)_ISC_MEM_FILELINE) -#define isc_mem_allocate(c, s) \ - ISCMEMFUNC(allocate)((c), (s), 0 _ISC_MEM_FILELINE) + isc__mem_reget((c), (p), (o), (n), (f)_ISC_MEM_FILELINE) +#define isc_mem_allocate(c, s) isc__mem_allocate((c), (s), 0 _ISC_MEM_FILELINE) #define isc_mem_allocatex(c, s, f) \ - ISCMEMFUNC(allocate)((c), (s), (f)_ISC_MEM_FILELINE) + isc__mem_allocate((c), (s), (f)_ISC_MEM_FILELINE) #define isc_mem_callocate(c, n, s) \ - ISCMEMFUNC(callocate)((c), (n), (s), 0 _ISC_MEM_FILELINE) + isc__mem_callocate((c), (n), (s), 0 _ISC_MEM_FILELINE) #define isc_mem_reallocate(c, p, s) \ - ISCMEMFUNC(reallocate)((c), (p), (s), 0 _ISC_MEM_FILELINE) + isc__mem_reallocate((c), (p), (s), 0 _ISC_MEM_FILELINE) #define isc_mem_reallocatex(c, p, s, f) \ - ISCMEMFUNC(reallocate)((c), (p), (s), (f)_ISC_MEM_FILELINE) -#define isc_mem_strdup(c, p) ISCMEMFUNC(strdup)((c), (p)_ISC_MEM_FILELINE) + isc__mem_reallocate((c), (p), (s), (f)_ISC_MEM_FILELINE) +#define isc_mem_strdup(c, p) isc__mem_strdup((c), (p)_ISC_MEM_FILELINE) #define isc_mem_strndup(c, p, l) \ - ISCMEMFUNC(strndup)((c), (p), (l)_ISC_MEM_FILELINE) -#define isc_mempool_get(c) ISCMEMPOOLFUNC(get)((c)_ISC_MEM_FILELINE) + isc__mem_strndup((c), (p), (l)_ISC_MEM_FILELINE) +#define isc_mempool_get(c) isc__mempool_get((c)_ISC_MEM_FILELINE) -#define isc_mem_put(c, p, s) \ - do { \ - ISCMEMFUNC(put)((c), (p), (s), 0 _ISC_MEM_FILELINE); \ - (p) = NULL; \ - } while (0) -#define isc_mem_putx(c, p, s, f) \ - do { \ - ISCMEMFUNC(put) \ - ((c), (p), (s), (f)_ISC_MEM_FILELINE); \ - (p) = NULL; \ - } while (0) -#define isc_mem_putanddetach(c, p, s) \ - do { \ - ISCMEMFUNC(putanddetach)((c), (p), (s), 0 _ISC_MEM_FILELINE); \ - (p) = NULL; \ - } while (0) -#define isc_mem_putanddetachx(c, p, s, f) \ - do { \ - ISCMEMFUNC(putanddetach) \ - ((c), (p), (s), (f)_ISC_MEM_FILELINE); \ - (p) = NULL; \ - } while (0) -#define isc_mem_free(c, p) \ - do { \ - ISCMEMFUNC(free)((c), (p), 0 _ISC_MEM_FILELINE); \ - (p) = NULL; \ - } while (0) -#define isc_mem_freex(c, p, f) \ +#define isc_mem_put(c, p, s) \ do { \ - ISCMEMFUNC(free)((c), (p), (f)_ISC_MEM_FILELINE); \ + isc__mem_put((c), (p), (s), 0 _ISC_MEM_FILELINE); \ (p) = NULL; \ } while (0) -#define isc_mempool_put(c, p) \ - do { \ - ISCMEMPOOLFUNC(put)((c), (p)_ISC_MEM_FILELINE); \ - (p) = NULL; \ +#define isc_mem_putx(c, p, s, f) \ + do { \ + isc__mem_put((c), (p), (s), (f)_ISC_MEM_FILELINE); \ + (p) = NULL; \ + } while (0) +#define isc_mem_putanddetach(c, p, s) \ + do { \ + isc__mem_putanddetach((c), (p), (s), 0 _ISC_MEM_FILELINE); \ + (p) = NULL; \ + } while (0) +#define isc_mem_putanddetachx(c, p, s, f) \ + do { \ + isc__mem_putanddetach((c), (p), (s), (f)_ISC_MEM_FILELINE); \ + (p) = NULL; \ + } while (0) +#define isc_mem_free(c, p) \ + do { \ + isc__mem_free((c), (p), 0 _ISC_MEM_FILELINE); \ + (p) = NULL; \ + } while (0) +#define isc_mem_freex(c, p, f) \ + do { \ + isc__mem_free((c), (p), (f)_ISC_MEM_FILELINE); \ + (p) = NULL; \ + } while (0) +#define isc_mempool_put(c, p) \ + do { \ + isc__mempool_put((c), (p)_ISC_MEM_FILELINE); \ + (p) = NULL; \ } while (0) /*@{*/ -#define isc_mem_create(cp) ISCMEMFUNC(create)((cp)_ISC_MEM_FILELINE) -void ISCMEMFUNC(create)(isc_mem_t **_ISC_MEM_FLARG); +#define isc_mem_create(cp) isc__mem_create((cp)_ISC_MEM_FILELINE) +void +isc__mem_create(isc_mem_t **_ISC_MEM_FLARG); /*!< * \brief Create a memory context. @@ -230,8 +225,9 @@ void ISCMEMFUNC(create)(isc_mem_t **_ISC_MEM_FLARG); /*@{*/ void isc_mem_attach(isc_mem_t *, isc_mem_t **); -#define isc_mem_detach(cp) ISCMEMFUNC(detach)((cp)_ISC_MEM_FILELINE) -void ISCMEMFUNC(detach)(isc_mem_t **_ISC_MEM_FLARG); +#define isc_mem_detach(cp) isc__mem_detach((cp)_ISC_MEM_FILELINE) +void +isc__mem_detach(isc_mem_t **_ISC_MEM_FLARG); /*!< * \brief Attach to / detach from a memory context. * @@ -246,8 +242,9 @@ void ISCMEMFUNC(detach)(isc_mem_t **_ISC_MEM_FLARG); */ /*@}*/ -#define isc_mem_destroy(cp) ISCMEMFUNC(destroy)((cp)_ISC_MEM_FILELINE) -void ISCMEMFUNC(destroy)(isc_mem_t **_ISC_MEM_FLARG); +#define isc_mem_destroy(cp) isc__mem_destroy((cp)_ISC_MEM_FILELINE) +void +isc__mem_destroy(isc_mem_t **_ISC_MEM_FLARG); /*%< * Destroy a memory context. */ @@ -501,38 +498,49 @@ isc_mempool_setfillcount(isc_mempool_t *restrict mpctx, /* * Pseudo-private functions for use via macros. Do not call directly. */ -void ISCMEMFUNC(putanddetach)(isc_mem_t **, void *, size_t, int _ISC_MEM_FLARG); -void ISCMEMFUNC(put)(isc_mem_t *, void *, size_t, int _ISC_MEM_FLARG); -void ISCMEMFUNC(free)(isc_mem_t *, void *, int _ISC_MEM_FLARG); +void +isc__mem_putanddetach(isc_mem_t **, void *, size_t, int _ISC_MEM_FLARG); +void +isc__mem_put(isc_mem_t *, void *, size_t, int _ISC_MEM_FLARG); +void +isc__mem_free(isc_mem_t *, void *, int _ISC_MEM_FLARG); -ISC_ATTR_MALLOC_DEALLOCATOR_IDX(ISCMEMFUNC(put), 2) -void *ISCMEMFUNC(get)(isc_mem_t *, size_t, int _ISC_MEM_FLARG); +ISC_ATTR_MALLOC_DEALLOCATOR_IDX(isc__mem_put, 2) +void * +isc__mem_get(isc_mem_t *, size_t, int _ISC_MEM_FLARG); -ISC_ATTR_DEALLOCATOR_IDX(ISCMEMFUNC(put), 2) -void *ISCMEMFUNC(reget)(isc_mem_t *, void *, size_t, size_t, - int _ISC_MEM_FLARG); +ISC_ATTR_DEALLOCATOR_IDX(isc__mem_put, 2) +void * +isc__mem_reget(isc_mem_t *, void *, size_t, size_t, int _ISC_MEM_FLARG); -ISC_ATTR_MALLOC_DEALLOCATOR_IDX(ISCMEMFUNC(free), 2) -void *ISCMEMFUNC(allocate)(isc_mem_t *, size_t, int _ISC_MEM_FLARG); +ISC_ATTR_MALLOC_DEALLOCATOR_IDX(isc__mem_free, 2) +void * +isc__mem_allocate(isc_mem_t *, size_t, int _ISC_MEM_FLARG); -ISC_ATTR_MALLOC_DEALLOCATOR_IDX(ISCMEMFUNC(free), 2) -void *ISCMEMFUNC(callocate)(isc_mem_t *, size_t, size_t, int _ISC_MEM_FLARG); +ISC_ATTR_MALLOC_DEALLOCATOR_IDX(isc__mem_free, 2) +void * +isc__mem_callocate(isc_mem_t *, size_t, size_t, int _ISC_MEM_FLARG); -ISC_ATTR_DEALLOCATOR_IDX(ISCMEMFUNC(free), 2) -void *ISCMEMFUNC(reallocate)(isc_mem_t *, void *, size_t, int _ISC_MEM_FLARG); +ISC_ATTR_DEALLOCATOR_IDX(isc__mem_free, 2) +void * +isc__mem_reallocate(isc_mem_t *, void *, size_t, int _ISC_MEM_FLARG); ISC_ATTR_RETURNS_NONNULL -ISC_ATTR_MALLOC_DEALLOCATOR_IDX(ISCMEMFUNC(free), 2) -char *ISCMEMFUNC(strdup)(isc_mem_t *, const char *_ISC_MEM_FLARG); +ISC_ATTR_MALLOC_DEALLOCATOR_IDX(isc__mem_free, 2) +char * +isc__mem_strdup(isc_mem_t *, const char *_ISC_MEM_FLARG); ISC_ATTR_RETURNS_NONNULL -ISC_ATTR_MALLOC_DEALLOCATOR_IDX(ISCMEMFUNC(free), 2) -char *ISCMEMFUNC(strndup)(isc_mem_t *, const char *, size_t _ISC_MEM_FLARG); +ISC_ATTR_MALLOC_DEALLOCATOR_IDX(isc__mem_free, 2) +char * +isc__mem_strndup(isc_mem_t *, const char *, size_t _ISC_MEM_FLARG); -ISC_ATTR_MALLOC_DEALLOCATOR_IDX(ISCMEMPOOLFUNC(put), 2) -void *ISCMEMPOOLFUNC(get)(isc_mempool_t *_ISC_MEM_FLARG); +ISC_ATTR_MALLOC_DEALLOCATOR_IDX(isc__mempool_put, 2) +void * +isc__mempool_get(isc_mempool_t *_ISC_MEM_FLARG); -void ISCMEMPOOLFUNC(put)(isc_mempool_t *, void *_ISC_MEM_FLARG); +void +isc__mempool_put(isc_mempool_t *, void *_ISC_MEM_FLARG); #ifdef POP_MALLOC_MACRO /* From 1d341096c1343e0a7df9888fb7c71483f1f14be2 Mon Sep 17 00:00:00 2001 From: Tony Finch Date: Mon, 12 Jun 2023 12:08:07 +0100 Subject: [PATCH 2/3] Get rid of unnecessary macros in the system test dyndb driver CHECKED_MEM_GET and ZERO_PTR are built-in features of isc_mem. --- bin/tests/system/dyndb/driver/db.c | 3 +-- bin/tests/system/dyndb/driver/instance.c | 7 +++--- bin/tests/system/dyndb/driver/util.h | 29 ------------------------ 3 files changed, 4 insertions(+), 35 deletions(-) diff --git a/bin/tests/system/dyndb/driver/db.c b/bin/tests/system/dyndb/driver/db.c index d380511e7e..cadd3d02c2 100644 --- a/bin/tests/system/dyndb/driver/db.c +++ b/bin/tests/system/dyndb/driver/db.c @@ -606,8 +606,7 @@ create_db(isc_mem_t *mctx, const dns_name_t *origin, dns_dbtype_t type, a_addr.s_addr = 0x0100007fU; - CHECKED_MEM_GET_PTR(mctx, sampledb); - ZERO_PTR(sampledb); + sampledb = isc_mem_getx(mctx, sizeof(*sampledb), ISC_MEM_ZERO); isc_mem_attach(mctx, &sampledb->common.mctx); dns_name_init(&sampledb->common.origin, NULL); diff --git a/bin/tests/system/dyndb/driver/instance.c b/bin/tests/system/dyndb/driver/instance.c index 4ce0d1b31b..af09f3551b 100644 --- a/bin/tests/system/dyndb/driver/instance.c +++ b/bin/tests/system/dyndb/driver/instance.c @@ -111,12 +111,11 @@ new_sample_instance(isc_mem_t *mctx, const char *db_name, int argc, char **argv, const dns_dyndbctx_t *dctx, sample_instance_t **sample_instp) { isc_result_t result; - sample_instance_t *inst = NULL; REQUIRE(sample_instp != NULL && *sample_instp == NULL); - CHECKED_MEM_GET_PTR(mctx, inst); - ZERO_PTR(inst); + sample_instance_t *inst = isc_mem_getx(mctx, sizeof(*inst), + ISC_MEM_ZERO); isc_mem_attach(mctx, &inst->mctx); inst->db_name = isc_mem_strdup(mctx, db_name); @@ -225,5 +224,5 @@ destroy_sample_instance(sample_instance_t **instp) { dns_view_detach(&inst->view); dns_zonemgr_detach(&inst->zmgr); - MEM_PUT_AND_DETACH(inst); + isc_mem_putanddetach(&inst->mctx, inst, sizeof(*inst)); } diff --git a/bin/tests/system/dyndb/driver/util.h b/bin/tests/system/dyndb/driver/util.h index d59a7fd6ea..e3ccedfe7e 100644 --- a/bin/tests/system/dyndb/driver/util.h +++ b/bin/tests/system/dyndb/driver/util.h @@ -51,32 +51,3 @@ if (result != ISC_R_SUCCESS) \ goto cleanup; \ } while (0) - -#define CHECKED_MEM_GET(m, target_ptr, s) \ - do { \ - (target_ptr) = isc_mem_get((m), (s)); \ - if ((target_ptr) == NULL) { \ - result = ISC_R_NOMEMORY; \ - log_error("Memory allocation failed"); \ - goto cleanup; \ - } \ - } while (0) - -#define CHECKED_MEM_GET_PTR(m, target_ptr) \ - CHECKED_MEM_GET(m, target_ptr, sizeof(*(target_ptr))) - -#define CHECKED_MEM_STRDUP(m, source, target) \ - do { \ - (target) = isc_mem_strdup((m), (source)); \ - if ((target) == NULL) { \ - result = ISC_R_NOMEMORY; \ - log_error("Memory allocation failed"); \ - goto cleanup; \ - } \ - } while (0) - -#define ZERO_PTR(ptr) memset((ptr), 0, sizeof(*(ptr))) - -#define MEM_PUT_AND_DETACH(target_ptr) \ - isc_mem_putanddetach(&(target_ptr)->mctx, target_ptr, \ - sizeof(*(target_ptr))) From 6271f3c0541f6f93b8f34e3ee7c9f645185a5b59 Mon Sep 17 00:00:00 2001 From: Tony Finch Date: Mon, 12 Jun 2023 12:51:02 +0100 Subject: [PATCH 3/3] Fix the style of an array de-allocation in dnssec-cds Mention the element size explicitly, so that `matching_sigs()`, `signed_loose()`, and `signed_strict()` use the same calculation. --- bin/dnssec/dnssec-cds.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/dnssec/dnssec-cds.c b/bin/dnssec/dnssec-cds.c index 21ecec919a..bdb62bf563 100644 --- a/bin/dnssec/dnssec-cds.c +++ b/bin/dnssec/dnssec-cds.c @@ -739,7 +739,7 @@ signed_strict(dns_rdataset_t *dsset, dns_secalg_t *algo) { } } - isc_mem_put(mctx, algo, nkey); + isc_mem_put(mctx, algo, nkey * sizeof(algo[0])); return (all_ok); }