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 <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.
This commit is contained in:
Tony Finch 2023-04-06 11:30:00 +01:00 committed by Ondřej Surý
parent 5a36bebfce
commit 0d6dcd217d
No known key found for this signature in database
GPG key ID: 2820F37E873DEA41
5 changed files with 31 additions and 22 deletions

View file

@ -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);
...
}

View 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

View file

@ -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) \
{ \

View file

@ -27,16 +27,6 @@
#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);
}
/*

View file

@ -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,