]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
A SET_IF_NOT_NULL() macro for optional return values
authorTony Finch <fanf@isc.org>
Thu, 6 Apr 2023 10:30:00 +0000 (11:30 +0100)
committerOndřej Surý <ondrej@isc.org>
Tue, 15 Aug 2023 10:04:29 +0000 (12:04 +0200)
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 <isc/util.h> 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 [new file with mode: 0644]
lib/dns/openssl_shim.c
lib/dns/zone.c
lib/isc/histo.c
lib/isc/include/isc/util.h

diff --git a/cocci/set_if_not_null.spatch b/cocci/set_if_not_null.spatch
new file mode 100644 (file)
index 0000000..ce44d67
--- /dev/null
@@ -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);
+               ...
+       }
index 9d0e397e2d808bbc72348e9eafd962df8cae43d8..15e93b61ba7cffe53e42b44b063c074c23d59a38 100644 (file)
@@ -13,6 +13,8 @@
 
 #include "openssl_shim.h"
 
+#include <isc/util.h>
+
 #if !HAVE_RSA_SET0_KEY && OPENSSL_VERSION_NUMBER < 0x30000000L
 /* From OpenSSL 1.1.0 */
 int
index 6fe31061b874401b07482a5d5bbf81858c5a7527..21a906813b1cccbc2be00e65dc20dce65dac83af 100644 (file)
  */
 #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)                                        \
        {                                                                \
index 870c3c0152f1eead2c3e3f4bb4c7a047a10d6b04..b44dab0cb42cde05405361ad512dc1db680d3549 100644 (file)
 #include <isc/mem.h>
 #include <isc/tid.h>
 
-/*
- * XXXFANF this should probably be in <isc/util.h> 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);
 }
 
 /*
index 25df4dbc34fd0da62b93d2c31ba47efa7153660f..a7bc9908badf13a1a4ceba1c0aa88f5191ed90f2 100644 (file)
 
 #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,