From 36943c7c1cf1f381984e33c0a9ff783e99ad31c8 Mon Sep 17 00:00:00 2001 From: Tony Finch Date: Thu, 6 Apr 2023 11:30:00 +0100 Subject: [PATCH] An OUTARG() macro for optional return values The OUTARG() macro avoids a fair amount of tedious boilerplate. I have included a Coccinelle semantic patch to use OUTARG() where appropriate. The patch needs a #include in `openssl_shim.c` in order to work. --- cocci/outarg.spatch | 14 ++++++++++++++ lib/dns/openssl_shim.c | 2 ++ lib/isc/histo.c | 10 ---------- lib/isc/include/isc/util.h | 10 ++++++++++ 4 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 cocci/outarg.spatch diff --git a/cocci/outarg.spatch b/cocci/outarg.spatch new file mode 100644 index 0000000000..e4d5ac16b7 --- /dev/null +++ b/cocci/outarg.spatch @@ -0,0 +1,14 @@ +@@ +type T; +identifier fun; +identifier arg; +expression val; +@@ + fun(..., T *arg, ...) { + ... +- if (arg != NULL) { +- *arg = val; +- } ++ OUTARG(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/isc/histo.c b/lib/isc/histo.c index 395b27b6b6..048e1215ca 100644 --- a/lib/isc/histo.c +++ b/lib/isc/histo.c @@ -34,16 +34,6 @@ #define STRUCT_FLEX_SIZE(pointer, member, count) \ (sizeof(*(pointer)) + sizeof(*(pointer)->member) * (count)) -/* - * 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') diff --git a/lib/isc/include/isc/util.h b/lib/isc/include/isc/util.h index eb99583e20..712c8c727a 100644 --- a/lib/isc/include/isc/util.h +++ b/lib/isc/include/isc/util.h @@ -96,6 +96,16 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) +/* + * Optional return values, or out-arguments + */ +#define OUTARG(ptr, val) \ + ({ \ + if ((ptr) != NULL) { \ + *(ptr) = (val); \ + } \ + }) + /*% * Use this in translation units that would otherwise be empty, to * suppress compiler warnings.