From 4d0c2ca90f56a4748a3fa78fca1fd672882fa367 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 28 Apr 2019 09:59:28 +0100 Subject: [PATCH] testcrypt: refactor return_opt_foo functions. There are already three tediously similar functions that wrap a NULL check around some existing function to return one or another kind of pointer, and I'm about to want to add another one. Make a macro so that it's easy to make more functions identical to these three. --- testcrypt.c | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/testcrypt.c b/testcrypt.c index 8071c013..0d028e0e 100644 --- a/testcrypt.c +++ b/testcrypt.c @@ -503,29 +503,18 @@ static void return_val_string_asciz(strbuf *out, char *s) return_val_string(out, sb); } -static void return_opt_val_string_asciz(strbuf *out, char *s) -{ - if (!s) - strbuf_catf(out, "NULL\n"); - else - return_val_string_asciz(out, s); -} +#define NULLABLE_RETURN_WRAPPER(type_name, c_type) \ + static void return_opt_##type_name(strbuf *out, c_type ptr) \ + { \ + if (!ptr) \ + strbuf_catf(out, "NULL\n"); \ + else \ + return_##type_name(out, ptr); \ + } -static void return_opt_val_cipher(strbuf *out, ssh_cipher *c) -{ - if (!c) - strbuf_catf(out, "NULL\n"); - else - return_val_cipher(out, c); -} - -static void return_opt_val_hash(strbuf *out, ssh_hash *h) -{ - if (!h) - strbuf_catf(out, "NULL\n"); - else - return_val_hash(out, h); -} +NULLABLE_RETURN_WRAPPER(val_string_asciz, char *) +NULLABLE_RETURN_WRAPPER(val_cipher, ssh_cipher *) +NULLABLE_RETURN_WRAPPER(val_hash, ssh_hash *) static void handle_hello(BinarySource *in, strbuf *out) {