From 7a1e7006d06fdbab86ea79a197c316744b09d933 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Fri, 16 Nov 2018 18:17:22 +0100 Subject: [PATCH] core: Protect the trace macros for fun and profit. * src/debug.h: Protect macros using. (_trace_err, _trace_sysres, _trace_syserr): New helper inline functions. Signed-off-by: Werner Koch --- src/data-compat.c | 3 +- src/data-estream.c | 3 +- src/data-fd.c | 3 +- src/data-mem.c | 14 +++-- src/data-stream.c | 3 +- src/data-user.c | 3 +- src/debug.h | 132 +++++++++++++++++++++++++++------------------ src/gpgme.c | 3 +- src/keylist.c | 7 +-- src/posix-io.c | 3 +- src/signers.c | 3 +- 11 files changed, 107 insertions(+), 70 deletions(-) diff --git a/src/data-compat.c b/src/data-compat.c index 9091b968..64ed2d28 100644 --- a/src/data-compat.c +++ b/src/data-compat.c @@ -110,7 +110,8 @@ gpgme_data_new_from_filepart (gpgme_data_t *r_dh, const char *fname, (*r_dh)->data.mem.size = length; (*r_dh)->data.mem.length = length; - return TRACE_SUC ("r_dh=%p", *r_dh); + TRACE_SUC ("r_dh=%p", *r_dh); + return 0; } diff --git a/src/data-estream.c b/src/data-estream.c index 1f1a64eb..1855e74e 100644 --- a/src/data-estream.c +++ b/src/data-estream.c @@ -95,5 +95,6 @@ gpgme_data_new_from_estream (gpgme_data_t *r_dh, gpgrt_stream_t stream) return TRACE_ERR (err); (*r_dh)->data.e_stream = stream; - return TRACE_SUC ("dh=%p", *r_dh); + TRACE_SUC ("dh=%p", *r_dh); + return 0; } diff --git a/src/data-fd.c b/src/data-fd.c index 6a915fc0..5c68130f 100644 --- a/src/data-fd.c +++ b/src/data-fd.c @@ -82,5 +82,6 @@ gpgme_data_new_from_fd (gpgme_data_t *r_dh, int fd) return TRACE_ERR (err); (*r_dh)->data.fd = fd; - return TRACE_SUC ("dh=%p", *r_dh); + TRACE_SUC ("dh=%p", *r_dh); + return 0; } diff --git a/src/data-mem.c b/src/data-mem.c index 915c3e00..f51d2fd7 100644 --- a/src/data-mem.c +++ b/src/data-mem.c @@ -177,7 +177,8 @@ gpgme_data_new (gpgme_data_t *r_dh) if (err) return TRACE_ERR (err); - return TRACE_SUC ("dh=%p", *r_dh); + TRACE_SUC ("dh=%p", *r_dh); + return 0; } @@ -214,7 +215,8 @@ gpgme_data_new_from_mem (gpgme_data_t *r_dh, const char *buffer, (*r_dh)->data.mem.size = size; (*r_dh)->data.mem.length = size; - return TRACE_SUC ("dh=%p", *r_dh); + TRACE_SUC ("dh=%p", *r_dh); + return 0; } @@ -282,13 +284,9 @@ gpgme_data_release_and_get_mem (gpgme_data_t dh, size_t *r_len) gpgme_data_release (dh); if (r_len) - { - TRACE_SUC ("buffer=%p, len=%zu", str, *r_len); - } + TRACE_SUC ("buffer=%p, len=%zu", str, *r_len); else - { - TRACE_SUC ("buffer=%p", str); - } + TRACE_SUC ("buffer=%p", str); return str; } diff --git a/src/data-stream.c b/src/data-stream.c index 19a5a079..1ffb6216 100644 --- a/src/data-stream.c +++ b/src/data-stream.c @@ -104,5 +104,6 @@ gpgme_data_new_from_stream (gpgme_data_t *r_dh, FILE *stream) return TRACE_ERR (err); (*r_dh)->data.stream = stream; - return TRACE_SUC ("dh=%p", *r_dh); + TRACE_SUC ("dh=%p", *r_dh); + return 0; } diff --git a/src/data-user.c b/src/data-user.c index f5bd2efb..0111b2de 100644 --- a/src/data-user.c +++ b/src/data-user.c @@ -100,5 +100,6 @@ gpgme_data_new_from_cbs (gpgme_data_t *r_dh, gpgme_data_cbs_t cbs, void *handle) (*r_dh)->data.user.cbs = cbs; (*r_dh)->data.user.handle = handle; - return TRACE_SUC ("dh=%p", *r_dh); + TRACE_SUC ("dh=%p", *r_dh); + return 0; } diff --git a/src/debug.h b/src/debug.h index 06e6d02c..7ef8cf23 100644 --- a/src/debug.h +++ b/src/debug.h @@ -113,68 +113,97 @@ _gpgme_trace_gpgme_error (gpgme_error_t err, const char *file, int line) void *_gpgme_trace_tag = (void *) (uintptr_t) tag; \ _gpgme_debug_frame_begin () -#define TRACE_BEG(lvl, name, tag, ...) \ +/* Note: We can't protect this with a do-while block. */ +#define TRACE_BEG(lvl, name, tag, ...) \ _TRACE (lvl, name, tag); \ - _gpgme_debug (_gpgme_trace_level, 1, \ + _gpgme_debug (_gpgme_trace_level, 1, \ _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \ __VA_ARGS__) -#define TRACE(lvl, name, tag, ...) \ - _gpgme_debug_frame_begin (), \ - _gpgme_debug (lvl, 0, \ - name, STRINGIFY (tag), (void *) (uintptr_t) tag, \ - __VA_ARGS__), \ - _gpgme_debug_frame_end () - -#define TRACE_ERR(err) \ - err == 0 ? (TRACE_SUC ("")) : \ - (_gpgme_debug (_gpgme_trace_level, -1, NULL, NULL, NULL, \ - "%s:%d: error: %s <%s>\n", \ - _gpgme_trace_func, __LINE__, gpgme_strerror (err), \ - gpgme_strsource (err)), _gpgme_debug_frame_end (), (err)) +#define TRACE(lvl, name, tag, ...) do { \ + _gpgme_debug_frame_begin (); \ + _gpgme_debug (lvl, 0, name, STRINGIFY (tag), (void *)(uintptr_t)tag, \ + __VA_ARGS__); \ + _gpgme_debug_frame_end (); \ + } while (0) -/* The cast to void suppresses GCC warnings. */ -#define TRACE_SYSRES(res) \ - res >= 0 ? ((void) (TRACE_SUC ("result=%i", res)), (res)) : \ - (_gpgme_debug (_gpgme_trace_level, -1, NULL, NULL, NULL, \ - "%s: error: %s\n", \ - _gpgme_trace_func, strerror (errno)), \ - _gpgme_debug_frame_end (), (res)) -#define TRACE_SYSERR(res) \ - res == 0 ? ((void) (TRACE_SUC ("result=%i", res)), (res)) : \ - (_gpgme_debug (_gpgme_trace_level, -1, NULL, NULL, NULL, \ - "%s: error: %s\n", \ - _gpgme_trace_func, strerror (res)), \ - _gpgme_debug_frame_end (), (res)) -#define TRACE_SYSERR_NR(res) \ - do { res == 0 ? ((void) (TRACE_SUC ("result=%i", res)), (res)) : \ - (_gpgme_debug (_gpgme_trace_level, -1, NULL, NULL, NULL, \ - "%s: error: %s\n", \ - _gpgme_trace_func, strerror (res)), \ - _gpgme_debug_frame_end ()); } while (0) +/* Trace a gpg-error and return it. */ +#define TRACE_ERR(err) \ + _trace_err ((err), _gpgme_trace_level, _gpgme_trace_func, __LINE__) +static inline gpg_error_t +_trace_err (gpg_error_t err, int lvl, const char *func, int line) +{ + if (!err) + _gpgme_debug (lvl, 3, func, NULL, NULL, ""); + else + _gpgme_debug (lvl, -1, NULL, NULL, NULL, + "%s:%d: error: %s <%s>\n", + func, line, gpgme_strerror (err), gpgme_strsource (err)); + _gpgme_debug_frame_end (); + return err; +} -#define TRACE_SUC(...) \ - _gpgme_debug (_gpgme_trace_level, 3, _gpgme_trace_func, NULL, NULL, \ - __VA_ARGS__), _gpgme_debug_frame_end () +/* Trace a system call result and return it. */ +#define TRACE_SYSRES(res) \ + _trace_sysres ((res), _gpgme_trace_level, _gpgme_trace_func, __LINE__) +static inline int +_trace_sysres (int res, int lvl, const char *func, int line) +{ + if (res >= 0) + _gpgme_debug (lvl, 3, func, NULL, NULL, "result=%d", res); + else + _gpgme_debug (lvl, -1, NULL, NULL, NULL, + "%s:%d: error: %s (%d)\n", + func, line, strerror (res), res); + _gpgme_debug_frame_end (); + return res; +} -#define TRACE_LOG(...) \ - _gpgme_debug (_gpgme_trace_level, 2, \ - _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \ - __VA_ARGS__) +/* Trace a system call error and return it. */ +#define TRACE_SYSERR(rc) \ + _trace_syserr ((rc), _gpgme_trace_level, _gpgme_trace_func, __LINE__) +static inline int +_trace_syserr (int rc, int lvl, const char *func, int line) +{ + if (!rc) + _gpgme_debug (lvl, 3, func, NULL, NULL, "result=0"); + else + _gpgme_debug (lvl, -1, NULL, NULL, NULL, + "%s:%d: error: %s (%d)\n", + func, line, strerror (rc), rc); + _gpgme_debug_frame_end (); + return rc; +} -#define TRACE_LOGBUF(buf, len) \ - _gpgme_debug_buffer (_gpgme_trace_level, "%s: check: %s", \ - _gpgme_trace_func, buf, len) +#define TRACE_SUC(...) do { \ + _gpgme_debug (_gpgme_trace_level, 3, _gpgme_trace_func, NULL, NULL, \ + __VA_ARGS__); \ + _gpgme_debug_frame_end (); \ + } while (0) -#define TRACE_LOGBUFX(buf, len) \ - _gpgme_debug_buffer (_gpgme_trace_level+1, "%s: check: %s", \ - _gpgme_trace_func, buf, len) +#define TRACE_LOG(...) do { \ + _gpgme_debug (_gpgme_trace_level, 2, \ + _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \ + __VA_ARGS__); \ + } while (0) + +#define TRACE_LOGBUF(buf, len) do { \ + _gpgme_debug_buffer (_gpgme_trace_level, "%s: check: %s", \ + _gpgme_trace_func, buf, len); \ + } while (0) + +#define TRACE_LOGBUFX(buf, len) do { \ + _gpgme_debug_buffer (_gpgme_trace_level+1, "%s: check: %s", \ + _gpgme_trace_func, buf, len); \ + } while (0) + +#define TRACE_SEQ(hlp,fmt) do { \ + _gpgme_debug_begin (&(hlp), _gpgme_trace_level, \ + "%s: check: %s=%p, " fmt, _gpgme_trace_func, \ + _gpgme_trace_tagname, _gpgme_trace_tag); \ + } while (0) -#define TRACE_SEQ(hlp,fmt) \ - _gpgme_debug_begin (&(hlp), _gpgme_trace_level, \ - "%s: check: %s=%p, " fmt, _gpgme_trace_func, \ - _gpgme_trace_tagname, _gpgme_trace_tag) #define TRACE_ADD0(hlp,fmt) \ _gpgme_debug_add (&(hlp), fmt) #define TRACE_ADD1(hlp,fmt,a) \ @@ -186,6 +215,7 @@ _gpgme_trace_gpgme_error (gpgme_error_t err, const char *file, int line) #define TRACE_END(hlp,fmt) \ _gpgme_debug_add (&(hlp), fmt); \ _gpgme_debug_end (&(hlp)) + #define TRACE_ENABLED(hlp) (!!(hlp)) /* And finally a simple macro to trace the location of an error code. diff --git a/src/gpgme.c b/src/gpgme.c index c4a1da11..a0a6c6bc 100644 --- a/src/gpgme.c +++ b/src/gpgme.c @@ -159,7 +159,8 @@ gpgme_new (gpgme_ctx_t *r_ctx) *r_ctx = ctx; - return TRACE_SUC ("ctx=%p", ctx); + TRACE_SUC ("ctx=%p", ctx); + return 0; } diff --git a/src/keylist.c b/src/keylist.c index 7ea7b267..cdb115fd 100644 --- a/src/keylist.c +++ b/src/keylist.c @@ -1235,9 +1235,10 @@ gpgme_op_keylist_next (gpgme_ctx_t ctx, gpgme_key_t *r_key) *r_key = queue_item->key; free (queue_item); - return TRACE_SUC ("key=%p (%s)", *r_key, - ((*r_key)->subkeys && (*r_key)->subkeys->fpr) ? - (*r_key)->subkeys->fpr : "invalid"); + TRACE_SUC ("key=%p (%s)", *r_key, + ((*r_key)->subkeys && (*r_key)->subkeys->fpr) ? + (*r_key)->subkeys->fpr : "invalid"); + return 0; } diff --git a/src/posix-io.c b/src/posix-io.c index 77ecde05..be084312 100644 --- a/src/posix-io.c +++ b/src/posix-io.c @@ -166,7 +166,8 @@ _gpgme_io_pipe (int filedes[2], int inherit_idx) if (err) return TRACE_SYSRES (err); - return TRACE_SUC ("read=0x%x, write=0x%x", filedes[0], filedes[1]); + TRACE_SUC ("read=0x%x, write=0x%x", filedes[0], filedes[1]); + return 0; } diff --git a/src/signers.c b/src/signers.c index 6a5ccd22..ac88aebb 100644 --- a/src/signers.c +++ b/src/signers.c @@ -89,7 +89,8 @@ gpgme_signers_add (gpgme_ctx_t ctx, const gpgme_key_t key) gpgme_key_ref (key); ctx->signers[ctx->signers_len++] = key; - return TRACE_SUC (""); + TRACE_SUC (""); + return 0; }