From fb5f7ad971ea9b7cc92cc43c03973c318b716f6d Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Wed, 4 Nov 2015 19:42:17 +0100 Subject: [PATCH] Use the GCC printf format attribute. This enables the compiler to check the arguments against a printf style format string in all calls to the logging functions. --- examples/utils.h | 8 +++++++- src/context-private.h | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/examples/utils.h b/examples/utils.h index e43c06c..7562beb 100644 --- a/examples/utils.h +++ b/examples/utils.h @@ -26,9 +26,15 @@ extern "C" { #endif /* __cplusplus */ +#if defined(__GNUC__) +#define ATTR_FORMAT_PRINTF(a,b) __attribute__((format(printf, a, b))) +#else +#define ATTR_FORMAT_PRINTF(a,b) +#endif + #define WARNING(expr) message ("%s:%d: %s\n", __FILE__, __LINE__, expr) -int message (const char* fmt, ...); +int message (const char* fmt, ...) ATTR_FORMAT_PRINTF(1, 2); void message_set_logfile (const char* filename); diff --git a/src/context-private.h b/src/context-private.h index e7509bf..7f50a68 100644 --- a/src/context-private.h +++ b/src/context-private.h @@ -40,6 +40,12 @@ extern "C" { #define FUNCTION __FUNCTION__ #endif +#if defined(__GNUC__) +#define ATTR_FORMAT_PRINTF(a,b) __attribute__((format(printf, a, b))) +#else +#define ATTR_FORMAT_PRINTF(a,b) +#endif + #ifdef ENABLE_LOGGING #define HEXDUMP(context, loglevel, prefix, data, size) dc_context_hexdump (context, loglevel, __FILE__, __LINE__, FUNCTION, prefix, data, size) #define SYSERROR(context, errcode) dc_context_syserror (context, DC_LOGLEVEL_ERROR, __FILE__, __LINE__, FUNCTION, errcode) @@ -57,7 +63,7 @@ extern "C" { #endif dc_status_t -dc_context_log (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *format, ...); +dc_context_log (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *format, ...) ATTR_FORMAT_PRINTF(6, 7); dc_status_t dc_context_syserror (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, int errcode);