Skip to content

Commit 96dd9a2

Browse files
JustinStittpmladek
authored andcommitted
lib/test_printf.c: fix clang -Wformat warnings
see warnings: | lib/test_printf.c:157:52: error: format specifies type 'unsigned char' | but the argument has type 'int' [-Werror,-Wformat] test("0|1|1|128|255", | "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1); - | lib/test_printf.c:158:55: error: format specifies type 'char' but the | argument has type 'int' [-Werror,-Wformat] test("0|1|1|-128|-1", | "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1); - | lib/test_printf.c:159:41: error: format specifies type 'unsigned short' | but the argument has type 'int' [-Werror,-Wformat] | test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627); There's an ongoing movement to eventually enable the -Wformat flag for clang. Previous patches have targeted incorrect usage of format specifiers. In this case, however, the "incorrect" format specifiers are intrinsically part of the test cases. Hence, fixing them would be misaligned with their intended purpose. My proposed fix is to simply disable the warnings so that one day a clean build of the kernel with clang (and -Wformat enabled) would be possible. It would also keep us in the green for alot of the CI bots. Link: ClangBuiltLinux#378 Suggested-by: Nathan Chancellor <nathan@kernel.org> Suggested-by: Nick Desaulniers <ndesaulniers@google.com> Signed-off-by: Justin Stitt <justinstitt@google.com> Reviewed-by: Petr Mladek <pmladek@suse.com> Reviewed-by: Nick Desaulniers <ndesaulniers@google.com> Signed-off-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20220718230626.1029318-1-justinstitt@google.com
1 parent e3c8d33 commit 96dd9a2

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/test_printf.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
#define PAD_SIZE 16
3131
#define FILL_CHAR '$'
3232

33+
#define NOWARN(option, comment, block) \
34+
__diag_push(); \
35+
__diag_ignore_all(#option, comment); \
36+
block \
37+
__diag_pop();
38+
3339
KSTM_MODULE_GLOBALS();
3440

3541
static char *test_buffer __initdata;
@@ -159,9 +165,11 @@ test_number(void)
159165
test("0x1234abcd ", "%#-12x", 0x1234abcd);
160166
test(" 0x1234abcd", "%#12x", 0x1234abcd);
161167
test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
162-
test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
163-
test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
164-
test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
168+
NOWARN(-Wformat, "Intentionally test narrowing conversion specifiers.", {
169+
test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
170+
test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
171+
test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
172+
})
165173
/*
166174
* POSIX/C99: »The result of converting zero with an explicit
167175
* precision of zero shall be no characters.« Hence the output

0 commit comments

Comments
 (0)