From 0d6dcd217de63e4d137e6c4461694514be21d676 Mon Sep 17 00:00:00 2001 From: Tony Finch Date: Thu, 6 Apr 2023 11:30:00 +0100 Subject: [PATCH] A SET_IF_NOT_NULL() macro for optional return values The SET_IF_NOT_NULL() macro avoids a fair amount of tedious boilerplate, checking pointer parameters to see if they're non-NULL and updating them if they are. The macro was already in the dns_zone unit, and this commit moves it to the header. I have included a Coccinelle semantic patch to use SET_IF_NOT_NULL() where appropriate. The patch needs an #include in `openssl_shim.c` in order to work. --- cocci/set_if_not_null.spatch | 14 ++++++++++++++ lib/dns/openssl_shim.c | 2 ++ lib/dns/zone.c | 7 +------ lib/isc/histo.c | 22 ++++++---------------- lib/isc/include/isc/util.h | 8 ++++++++ 5 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 cocci/set_if_not_null.spatch diff --git a/cocci/set_if_not_null.spatch b/cocci/set_if_not_null.spatch new file mode 100644 index 0000000000..ce44d67250 --- /dev/null +++ b/cocci/set_if_not_null.spatch @@ -0,0 +1,14 @@ +@@ +type T; +identifier fun; +identifier arg; +expression val; +@@ + fun(..., T *arg, ...) { + ... +- if (arg != NULL) { +- *arg = val; +- } ++ SET_IF_NOT_NULL(arg, val); + ... + } diff --git a/lib/dns/openssl_shim.c b/lib/dns/openssl_shim.c index 9d0e397e2d..15e93b61ba 100644 --- a/lib/dns/openssl_shim.c +++ b/lib/dns/openssl_shim.c @@ -13,6 +13,8 @@ #include "openssl_shim.h" +#include + #if !HAVE_RSA_SET0_KEY && OPENSSL_VERSION_NUMBER < 0x30000000L /* From OpenSSL 1.1.0 */ int diff --git a/lib/dns/zone.c b/lib/dns/zone.c index 6fe31061b8..21a906813b 100644 --- a/lib/dns/zone.c +++ b/lib/dns/zone.c @@ -125,7 +125,7 @@ */ #define RANGE(a, min, max) (((a) < (min)) ? (min) : ((a) < (max) ? (a) : (max))) -#define NSEC3REMOVE(x) (((x)&DNS_NSEC3FLAG_REMOVE) != 0) +#define NSEC3REMOVE(x) (((x) & DNS_NSEC3FLAG_REMOVE) != 0) /*% * Key flags @@ -5474,11 +5474,6 @@ invalidate_rdataset: return (result); } -#define SET_IF_NOT_NULL(obj, val) \ - if (obj != NULL) { \ - *obj = val; \ - } - #define SET_SOA_VALUES(soattl_v, serial_v, refresh_v, retry_v, expire_v, \ minimum_v) \ { \ diff --git a/lib/isc/histo.c b/lib/isc/histo.c index 870c3c0152..b44dab0cb4 100644 --- a/lib/isc/histo.c +++ b/lib/isc/histo.c @@ -27,16 +27,6 @@ #include #include -/* - * XXXFANF this should probably be in too - */ -#define OUTARG(ptr, val) \ - ({ \ - if ((ptr) != NULL) { \ - *(ptr) = (val); \ - } \ - }) - #define HISTO_MAGIC ISC_MAGIC('H', 's', 't', 'o') #define HISTO_VALID(p) ISC_MAGIC_VALID(p, HISTO_MAGIC) #define HISTOMULTI_MAGIC ISC_MAGIC('H', 'g', 'M', 't') @@ -327,9 +317,9 @@ isc_histo_get(const isc_histo_t *hg, uint key, uint64_t *minp, uint64_t *maxp, REQUIRE(HISTO_VALID(hg)); if (key < BUCKETS(hg)) { - OUTARG(minp, key_to_minval(hg, key)); - OUTARG(maxp, key_to_maxval(hg, key)); - OUTARG(countp, get_key_count(hg, key)); + SET_IF_NOT_NULL(minp, key_to_minval(hg, key)); + SET_IF_NOT_NULL(maxp, key_to_maxval(hg, key)); + SET_IF_NOT_NULL(countp, get_key_count(hg, key)); return (ISC_R_SUCCESS); } else { return (ISC_R_RANGE); @@ -465,9 +455,9 @@ isc_histo_moments(const isc_histo_t *hg, double *pm0, double *pm1, sigma += count * delta * (value - mean); } - OUTARG(pm0, pop); - OUTARG(pm1, mean); - OUTARG(pm2, (pop > 0) ? sqrt(sigma / pop) : 0.0); + SET_IF_NOT_NULL(pm0, pop); + SET_IF_NOT_NULL(pm1, mean); + SET_IF_NOT_NULL(pm2, (pop > 0) ? sqrt(sigma / pop) : 0.0); } /* diff --git a/lib/isc/include/isc/util.h b/lib/isc/include/isc/util.h index 25df4dbc34..a7bc9908ba 100644 --- a/lib/isc/include/isc/util.h +++ b/lib/isc/include/isc/util.h @@ -96,6 +96,14 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) +/* + * Optional return values, or out-arguments + */ +#define SET_IF_NOT_NULL(obj, val) \ + if ((obj) != NULL) { \ + *(obj) = (val); \ + } + /*% * Get the allocation size for a struct with a flexible array member * containing `count` elements. The struct is identified by a pointer,