From d72f19fb1ebc0571cc31801e2725976e79a051e7 Mon Sep 17 00:00:00 2001 From: Hauke Mehrtens Date: Tue, 22 Sep 2015 16:40:58 +0200 Subject: [PATCH] logging: add format checks for printf input This patch makes gcc aware that this is a function formating a string, this way gcc is able to check if the format string is correct and warn the user otherwise. This should make it possible to find some more problems in the code. Change-Id: I27058eb1d5c3352e6f872e4013eedb94d42d757a Signed-off-by: Hauke Mehrtens Reviewed-on: https://gerrit.iotivity.org/gerrit/2935 Tested-by: jenkins-iotivity Reviewed-by: Patrick Lankswert --- resource/csdk/connectivity/common/inc/logger.h | 14 +++++++++++--- resource/csdk/logger/include/logger.h | 12 ++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/resource/csdk/connectivity/common/inc/logger.h b/resource/csdk/connectivity/common/inc/logger.h index e48ee7c..5e7ff5c 100644 --- a/resource/csdk/connectivity/common/inc/logger.h +++ b/resource/csdk/connectivity/common/inc/logger.h @@ -121,7 +121,11 @@ void OICLog(LogLevel level, const char *tag, const char *logStr); * @param tag - Module name * @param format - variadic log string */ -void OICLogv(LogLevel level, const char *tag, const char *format, ...); +void OICLogv(LogLevel level, const char *tag, const char *format, ...) +#if defined(__GNUC__) + __attribute__ ((format(printf, 3, 4))) +#endif +; /** * Output the contents of the specified buffer (in hex) with the specified priority level. @@ -168,7 +172,11 @@ void OICLogBuffer(LogLevel level, const char *tag, const uint8_t *buffer, uint16 * @param format - variadic log string */ void OICLogv(LogLevel level, PROGMEM const char *tag, const int16_t lineNum, - PROGMEM const char *format, ...); + PROGMEM const char *format, ...) +#if defined(__GNUC__) + __attribute__ ((format(printf, 4, 5))) +#endif +; #endif #ifdef TB_LOG @@ -211,4 +219,4 @@ void OICLogv(LogLevel level, PROGMEM const char *tag, const int16_t lineNum, #ifdef __cplusplus } #endif // __cplusplus -#endif /* U_LOGGER_H_ */ \ No newline at end of file +#endif /* U_LOGGER_H_ */ diff --git a/resource/csdk/logger/include/logger.h b/resource/csdk/logger/include/logger.h index 3c69eae..2c85161 100644 --- a/resource/csdk/logger/include/logger.h +++ b/resource/csdk/logger/include/logger.h @@ -105,7 +105,11 @@ typedef enum { * @param tag - Module name * @param format - variadic log string */ - void OCLogv(LogLevel level, const char * tag, const char * format, ...); + void OCLogv(LogLevel level, const char * tag, const char * format, ...) +#if defined(__GNUC__) + __attribute__ ((format(printf, 3, 4))) +#endif + ; /** * Output a log string with the specified priority level. @@ -160,7 +164,11 @@ typedef enum { * @param tag - Module name * @param format - variadic log string */ - void OCLogv(LogLevel level, const char * tag, const char * format, ...); + void OCLogv(LogLevel level, const char * tag, const char * format, ...) +#if defined(__GNUC__) + __attribute__ ((format(printf, 3, 4))) +#endif + ; #endif #ifdef TB_LOG -- 2.7.4