The -Wformat=2 diagnostic flag on GCC enables the -Wformat-nonliteral
-Wformat-security diagnostics, which are useful to catch some format
string mistakes. -Wformat-security is also enabled by default with
Clang, meaning that there were some warnings only appearing with
Clang.
Try to enabled the -Wformat=2 flag to make things more consistent across
compilers and catch more mistakes.
The only issues are these, in tests/regression/ust/linking:
CC demo_builtin-demo.o
In file included from /usr/include/stdio.h:866,
from /home/simark/src/lttng-tools/tests/regression/ust/linking/demo.c:9:
/usr/include/bits/stdio2.h: In function ‘sprintf’:
/usr/include/bits/stdio2.h:40:35: warning: format not a string literal, argument types not checked [-Wformat-nonliteral]
40 | __va_arg_pack ());
| ^~~~~~~~~~~~~
The reason this appears is that this directory uses -Wsystem-headers,
making the compiler show diagnostics in headers considered "system
headers". Manually silence those warnings by disabling
-Wformat-nonliteral in that specific directory.
Change-Id: I4c7991e76b2f5405f3b3397348adb9134de37d41
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
-Wshadow dnl
-Wno-gnu-folding-constant dnl
-Wsuggest-attribute=format 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
if (ret) {
filter_msg = strdup(" [failed to retrieve filter]");
} else if (filter_str) {
if (ret) {
filter_msg = strdup(" [failed to retrieve filter]");
} else if (filter_str) {
- const char * const filter_fmt = " [filter: '%s']";
-
- filter_msg = (char *) malloc(strlen(filter_str) +
- strlen(filter_fmt) + 1);
- if (filter_msg) {
- sprintf(filter_msg, filter_fmt,
- filter_str);
+ if (asprintf(&filter_msg, " [filter: '%s']", filter_str) == -1) {
+ filter_msg = NULL;
if (ret) {
filter_msg = strdup(" [failed to retrieve filter]");
} else if (filter_str) {
if (ret) {
filter_msg = strdup(" [failed to retrieve filter]");
} else if (filter_str) {
- const char * const filter_fmt =
- " [filter: '%s']";
-
- filter_msg = (char *) malloc(strlen(filter_str) +
- strlen(filter_fmt) + 1);
- if (filter_msg) {
- sprintf(filter_msg, filter_fmt,
- filter_str);
+ if (asprintf(&filter_msg, " [filter: '%s']", filter_str) == -1) {
+ filter_msg = NULL;
#define ATTR_FORMAT_PRINTF(_string_index, _first_to_check) \
__attribute__((format(printf, _string_index, _first_to_check)))
#define ATTR_FORMAT_PRINTF(_string_index, _first_to_check) \
__attribute__((format(printf, _string_index, _first_to_check)))
+/* Attribute suitable to tag functions as having strftime()-like arguments. */
+#define ATTR_FORMAT_STRFTIME(_string_index) \
+ __attribute__((format(strftime, _string_index, 0)))
+
/* Macros used to ignore specific compiler diagnostics. */
#define DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
/* Macros used to ignore specific compiler diagnostics. */
#define DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
#if defined(__clang__)
/* Clang */
# define DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT
#if defined(__clang__)
/* Clang */
# define DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT
+# define DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL \
+ _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
#else
/* GCC */
# define DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT \
_Pragma("GCC diagnostic ignored \"-Wsuggest-attribute=format\"")
#else
/* GCC */
# define DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT \
_Pragma("GCC diagnostic ignored \"-Wsuggest-attribute=format\"")
+# define DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL \
+ _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
/* Get date and time for session path */
time(&rawtime);
timeinfo = localtime(&rawtime);
/* Get date and time for session path */
time(&rawtime);
timeinfo = localtime(&rawtime);
+ DIAGNOSTIC_PUSH
+ DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
ret = strftime(dst, len, format, timeinfo);
ret = strftime(dst, len, format, timeinfo);
if (ret == 0) {
ERR("Unable to strftime with format %s at dst %p of len %zu", format,
dst, len);
if (ret == 0) {
ERR("Unable to strftime with format %s at dst %p of len %zu", format,
dst, len);
int utils_get_count_order_u64(uint64_t x);
const char *utils_get_home_dir(void);
char *utils_get_user_home_dir(uid_t uid);
int utils_get_count_order_u64(uint64_t x);
const char *utils_get_home_dir(void);
char *utils_get_user_home_dir(uid_t uid);
-size_t utils_get_current_time_str(const char *format, char *dst, size_t len);
+
+size_t utils_get_current_time_str(const char *format, char *dst, size_t len)
+ ATTR_FORMAT_STRFTIME(1);
+
int utils_get_group_id(const char *name, bool warn, gid_t *gid);
char *utils_generate_optstring(const struct option *long_options,
size_t opt_count);
int utils_get_group_id(const char *name, bool warn, gid_t *gid);
char *utils_generate_optstring(const struct option *long_options,
size_t opt_count);
+ DIAGNOSTIC_PUSH
+ DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
ret = snprintf(lh->health_sock_path, sizeof(lh->health_sock_path),
home_str, home);
ret = snprintf(lh->health_sock_path, sizeof(lh->health_sock_path),
home_str, home);
if ((ret < 0) || (ret >= sizeof(lh->health_sock_path))) {
return -ENOMEM;
}
if ((ret < 0) || (ret >= sizeof(lh->health_sock_path))) {
return -ENOMEM;
}
# -Wsystem-headers is needed to print warnings in the tracepoint
# description file.
# -Wsystem-headers is needed to print warnings in the tracepoint
# description file.
-AM_CPPFLAGS += -I$(srcdir) -Wsystem-headers
+#
+# However, we see some -Wformat-nonliteral warnings in some system headers,
+# so disable that.
+AM_CPPFLAGS += -I$(srcdir) -Wsystem-headers -Wno-format-nonliteral
# Set LIBS to nothing so the application does not link on useless
# libraries.
# Set LIBS to nothing so the application does not link on useless
# libraries.
}
handle = lttng_payload_view_pop_fd_handle(&view);
}
handle = lttng_payload_view_pop_fd_handle(&view);
- ok(!handle, test_description);
+ ok(!handle, "%s", test_description);
fd_handle_put(handle);
}
lttng_payload_reset(&payload);
return;
fail:
fd_handle_put(handle);
}
lttng_payload_reset(&payload);
return;
fail:
- fail(test_description);
+ fail("%s", test_description);
lttng_payload_reset(&payload);
}
lttng_payload_reset(&payload);
}
}
lttng_payload_reset(&payload);
}
lttng_payload_reset(&payload);
- pass(test_description);
+ pass("%s", test_description);
fd_handle_put(handle);
return;
fail:
lttng_payload_reset(&payload);
fd_handle_put(handle);
return;
fail:
lttng_payload_reset(&payload);
- fail(test_description);
+ fail("%s", test_description);
}
lttng_payload_reset(&payload);
}
lttng_payload_reset(&payload);
- pass(test_description);
+ pass("%s", test_description);
fd_handle_put(handle1);
fd_handle_put(handle2);
fd_handle_put(view_handle1);
fd_handle_put(handle1);
fd_handle_put(handle2);
fd_handle_put(view_handle1);
return;
fail:
lttng_payload_reset(&payload);
return;
fail:
lttng_payload_reset(&payload);
- fail(test_description);
+ fail("%s", test_description);
fd_handle_put(handle1);
fd_handle_put(handle2);
fd_handle_put(view_handle1);
fd_handle_put(handle1);
fd_handle_put(handle2);
fd_handle_put(view_handle1);