Skip to content

Commit e33a02e

Browse files
maciejwieczorretmanshuahkh
authored andcommitted
selftests: Add printf attribute to kselftest prints
Kselftest header defines multiple variadic functions that use printf along with other logic. There is no format checking for the variadic functions that use printing inside kselftest.h. Because of this the compiler won't be able to catch instances of mismatched printf formats and debugging tests might be more difficult. Add the common __printf() attribute macro to kselftest.h. Add __printf() attribute to every function using formatted printing with variadic arguments. Adding the attribute and compiling all selftests exposes a number of -Wformat warnings which were previously unnoticed due to a lack of format specifiers checking by the compiler. Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com> Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 2531f37 commit e33a02e

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

tools/testing/selftests/kselftest.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
#define KSFT_XPASS 3
7979
#define KSFT_SKIP 4
8080

81+
#define __printf(a, b) __attribute__((format(printf, a, b)))
82+
8183
/* counters */
8284
struct ksft_count {
8385
unsigned int ksft_pass;
@@ -144,7 +146,7 @@ static inline void ksft_print_cnts(void)
144146
ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
145147
}
146148

147-
static inline void ksft_print_msg(const char *msg, ...)
149+
static inline __printf(1, 2) void ksft_print_msg(const char *msg, ...)
148150
{
149151
int saved_errno = errno;
150152
va_list args;
@@ -169,7 +171,7 @@ static inline void ksft_perror(const char *msg)
169171
#endif
170172
}
171173

172-
static inline void ksft_test_result_pass(const char *msg, ...)
174+
static inline __printf(1, 2) void ksft_test_result_pass(const char *msg, ...)
173175
{
174176
int saved_errno = errno;
175177
va_list args;
@@ -183,7 +185,7 @@ static inline void ksft_test_result_pass(const char *msg, ...)
183185
va_end(args);
184186
}
185187

186-
static inline void ksft_test_result_fail(const char *msg, ...)
188+
static inline __printf(1, 2) void ksft_test_result_fail(const char *msg, ...)
187189
{
188190
int saved_errno = errno;
189191
va_list args;
@@ -209,7 +211,7 @@ static inline void ksft_test_result_fail(const char *msg, ...)
209211
ksft_test_result_fail(fmt, ##__VA_ARGS__);\
210212
} while (0)
211213

212-
static inline void ksft_test_result_xfail(const char *msg, ...)
214+
static inline __printf(1, 2) void ksft_test_result_xfail(const char *msg, ...)
213215
{
214216
int saved_errno = errno;
215217
va_list args;
@@ -223,7 +225,7 @@ static inline void ksft_test_result_xfail(const char *msg, ...)
223225
va_end(args);
224226
}
225227

226-
static inline void ksft_test_result_skip(const char *msg, ...)
228+
static inline __printf(1, 2) void ksft_test_result_skip(const char *msg, ...)
227229
{
228230
int saved_errno = errno;
229231
va_list args;
@@ -238,7 +240,7 @@ static inline void ksft_test_result_skip(const char *msg, ...)
238240
}
239241

240242
/* TODO: how does "error" differ from "fail" or "skip"? */
241-
static inline void ksft_test_result_error(const char *msg, ...)
243+
static inline __printf(1, 2) void ksft_test_result_error(const char *msg, ...)
242244
{
243245
int saved_errno = errno;
244246
va_list args;
@@ -285,7 +287,7 @@ static inline int ksft_exit_fail(void)
285287
ksft_cnt.ksft_xfail + \
286288
ksft_cnt.ksft_xskip)
287289

288-
static inline int ksft_exit_fail_msg(const char *msg, ...)
290+
static inline __printf(1, 2) int ksft_exit_fail_msg(const char *msg, ...)
289291
{
290292
int saved_errno = errno;
291293
va_list args;
@@ -312,7 +314,7 @@ static inline int ksft_exit_xpass(void)
312314
exit(KSFT_XPASS);
313315
}
314316

315-
static inline int ksft_exit_skip(const char *msg, ...)
317+
static inline __printf(1, 2) int ksft_exit_skip(const char *msg, ...)
316318
{
317319
int saved_errno = errno;
318320
va_list args;

0 commit comments

Comments
 (0)