Enable this warning, which suggests adding format attributes to some
functions, to help with format string validation. Fix the warnings it
generates by using the new ATTR_FORMAT_PRINTF macro.
There is only one spot we can't fix, that is in modprobe.c. The
compiler suggests we add an attribute to the kmod_set_log_fn
declaration, which we can't do, since it's not our code:
/home/simark/src/lttng-tools/src/bin/lttng-sessiond/modprobe.c: In function ‘setup_kmod_ctx’:
/home/simark/src/lttng-tools/src/bin/lttng-sessiond/modprobe.c:286:31: error: argument 2 of ‘kmod_set_log_fn’ might be a candidate for a format attribute [-Werror=suggest-attribute=format]
286 | kmod_set_log_fn(*ctx, log_kmod, NULL);
| ^~~~~~~~
I don't see any other choice but to explicitly ignore that spot.
Introduce some macros to abstract how to ignore specific diagnostics.
This is useful since not all compilers support the same diagnostic
flags. For example, telling clang to ignore -Wsuggest-attribute=format
would give an error.
Change-Id: I71278d7e2cdc66d4bbc59bd966469d0b427e963d
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
-Wmissing-parameter-type dnl
-Wshadow dnl
-Wno-gnu-folding-constant dnl
-Wmissing-parameter-type dnl
-Wshadow dnl
-Wno-gnu-folding-constant dnl
+ -Wsuggest-attribute=format dnl
dnl GCC enables this with -Wall in C++, and that generates a
dnl lot of warnings that have on average a low value to fix.
-Wno-sign-compare dnl
dnl GCC enables this with -Wall in C++, and that generates a
dnl lot of warnings that have on average a low value to fix.
-Wno-sign-compare dnl
/**
* @brief Logging function for libkmod integration.
*/
/**
* @brief Logging function for libkmod integration.
*/
-static void log_kmod(void *data, int priority, const char *file, int line,
+static ATTR_FORMAT_PRINTF(6, 0)
+void log_kmod(void *data, int priority, const char *file, int line,
const char *fn, const char *format, va_list args)
{
char *str;
const char *fn, const char *format, va_list args)
{
char *str;
+ /*
+ * Parameter 2 of kmod_set_log_fn generates a
+ * -Wsuggest-attribute=formatkmod_set_log_fn warning that we can't fix,
+ * ignore it.
+ */
+ DIAGNOSTIC_PUSH
+ DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT
kmod_set_log_fn(*ctx, log_kmod, NULL);
kmod_set_log_fn(*ctx, log_kmod, NULL);
ret = kmod_load_resources(*ctx);
if (ret < 0) {
ERR("Failed to load kmod library resources");
ret = kmod_load_resources(*ctx);
if (ret < 0) {
ERR("Failed to load kmod library resources");
* remaining space left in packet and write, since mutual exclusion
* protects us from concurrent writes.
*/
* remaining space left in packet and write, since mutual exclusion
* protects us from concurrent writes.
*/
+static ATTR_FORMAT_PRINTF(2, 3)
int lttng_metadata_printf(struct ust_registry_session *session,
const char *fmt, ...)
{
int lttng_metadata_printf(struct ust_registry_session *session,
const char *fmt, ...)
{
if (entry->start.signedness) {
ret = lttng_metadata_printf(session,
if (entry->start.signedness) {
ret = lttng_metadata_printf(session,
- "%lld", (long long) entry->start.value);
+ "%" PRId64, entry->start.value);
} else {
ret = lttng_metadata_printf(session,
} else {
ret = lttng_metadata_printf(session,
- "%llu", entry->start.value);
+ "%" PRIu64, entry->start.value);
} else {
if (entry->end.signedness) {
ret = lttng_metadata_printf(session,
} else {
if (entry->end.signedness) {
ret = lttng_metadata_printf(session,
- " ... %lld,\n",
- (long long) entry->end.value);
+ " ... %" PRId64 ",\n",
+ entry->end.value);
} else {
ret = lttng_metadata_printf(session,
} else {
ret = lttng_metadata_printf(session,
return ret >= 0 ? 0 : ret;
}
return ret >= 0 ? 0 : ret;
}
+static ATTR_FORMAT_PRINTF(2, 3)
void xml_error_handler(void *ctx, const char *format, ...)
{
char *errMsg;
void xml_error_handler(void *ctx, const char *format, ...)
{
char *errMsg;
#define ASSERT_LOCKED(lock) LTTNG_ASSERT(pthread_mutex_trylock(&lock))
#define ASSERT_LOCKED(lock) LTTNG_ASSERT(pthread_mutex_trylock(&lock))
+/* Attribute suitable to tag functions as having printf()-like arguments. */
+#define ATTR_FORMAT_PRINTF(_string_index, _first_to_check) \
+ __attribute__((format(printf, _string_index, _first_to_check)))
+
+/* Macros used to ignore specific compiler diagnostics. */
+
+#define DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
+#define DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
+
+#if defined(__clang__)
+ /* Clang */
+# define DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT
+#else
+ /* GCC */
+# define DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT \
+ _Pragma("GCC diagnostic ignored \"-Wsuggest-attribute=format\"")
+#endif
+
/*
* lttng_strncpy returns 0 on success, or nonzero on failure.
* It checks that the @src string fits into @dst_len before performing
/*
* lttng_strncpy returns 0 on success, or nonzero on failure.
* It checks that the @src string fits into @dst_len before performing
#include <unistd.h>
#include <common/compat/tid.h>
#include <unistd.h>
#include <common/compat/tid.h>
+#include <common/macros.h>
#include "signal-helper.h"
#include "utils.h"
#include "signal-helper.h"
#include "utils.h"
{ NULL, 0, 0, NULL, 0 }
};
{ NULL, 0, 0, NULL, 0 }
};
-static void debug_printf(const char *format, ...)
+static ATTR_FORMAT_PRINTF(1, 2)
+void debug_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
{
va_list args;
va_start(args, format);
#include <unistd.h>
#include <common/compat/tid.h>
#include <unistd.h>
#include <common/compat/tid.h>
+#include <common/macros.h>
#include "signal-helper.h"
#include "utils.h"
#include "signal-helper.h"
#include "utils.h"
{ NULL, 0, 0, NULL, 0 }
};
{ NULL, 0, 0, NULL, 0 }
};
-static void debug_printf(const char *format, ...)
+static ATTR_FORMAT_PRINTF(1, 2)
+void debug_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
{
va_list args;
va_start(args, format);
#include <lttng/lttng-error.h>
#include <common/macros.h>
#include <lttng/lttng-error.h>
#include <common/macros.h>
+#include <common/macros.h>
+
struct validation_ctx {
xmlSchemaParserCtxtPtr parser_ctx;
xmlSchemaPtr schema;
struct validation_ctx {
xmlSchemaParserCtxtPtr parser_ctx;
xmlSchemaPtr schema;
+static ATTR_FORMAT_PRINTF(2, 3)
void xml_error_handler(void *ctx, const char *format, ...)
{
char *err_msg;
void xml_error_handler(void *ctx, const char *format, ...)
{
char *err_msg;