kunit: Add gnu_printf specifiers
PATCH 1/1 kunit: Add gnu_printf specifiers
Some KUnit functions use variable arguments to implement a printf-like format string. Use the __printf() attribute to let the compiler warn if invalid format strings are passed in.
If the kernel is build with W=1, it complained about the lack of these
specifiers, e.g.:../lib/kunit/test.c:72:2: warning: function ‘kunit_log_append’ might be a candidate for ‘gnu_printf’ format attribute [-Wsuggest-attribute=format]
--- include/kunit/test.h | 2 +- lib/kunit/string-stream.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-)
include/kunit/test.h +1 -1
@@ -610,7 +610,7 @@ static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)void kunit_cleanup (struct kunit *test); -void kunit_log_append (char *log,const char *fmt, ...); +void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...); /* * printk and log to per-test or per-suite log buffer. Logging only done
lib/kunit/string-stream.h +3 -3
@@ -35,9 +35,9 @@ struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp); int __printf(2, 3) string_stream_add(struct string_stream *stream, const char *fmt, ...); -int string_stream_vadd(struct string_stream *stream, - const char *fmt, - va_list args); +int __printf(2, 0) string_stream_vadd(struct string_stream *stream, + const char *fmt, + va_list args); char *string_stream_get_string(struct string_stream *stream);
Signature · 1 line
2.31.1.751.gd2f1c929bd-goog
Quoted 10 lines · David Gow' via KUnit Development
Some KUnit functions use variable arguments to implement a printf-like format string. Use the
__printf()attribute to let the compiler warn if invalid format strings are passed in.If the kernel is build with W=1, it complained about the lack of these
specifiers, e.g.:../lib/kunit/test.c:72:2: warning: function ‘kunit_log_append’ might be a candidate for ‘gnu_printf’ format attribute [-Wsuggest-attribute=format]Signed-off-by David Gow <[email protected]>
As noted below, these additions don't really do anything. Unfortunately, they just make compiler warnings noisier in the case of kunit_log_append().
But if this silences a W=1 warning, then we should probably add them in. I guess it also serves as documentation that we're using the same standard format specifiers and not something custom, which is nice.
Quoted 10 lines
--- include/kunit/test.h | 2 +- lib/kunit/string-stream.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-)include/kunit/test.h +0
-0
include/kunit/test.h static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp) · line 613
Quoted 5 lines
include/kunit/test.h +1
-1@@ -610,7 +610,7 @@ static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)void kunit_cleanup (struct kunit *test); -void kunit_log_append (char *log,const char *fmt, ...); +void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
Before this patch:../lib/kunit/kunit-example-test.c: In function ‘example_simple_test’:../include/linux/kern_levels.h:5:18: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
5 | #define KERN_SOH "\001" /* ASCII Start Of Header */
| ^~~~~~../include/kunit/test.h:622:10: note: in definition of macro ‘kunit_log’
622 | printk(lvl fmt, ##__VA_ARGS__); \
| ^~~
../include/kunit/test.h:641:2: note: in expansion of macro ‘kunit_printk’
641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
| ^~~~~~~~~~~~
../include/linux/kern_levels.h:14:19: note: in expansion of macro ‘KERN_SOH’
14 | #define KERN_INFO KERN_SOH "6" /* informational */
| ^~~~~~~~
../include/kunit/test.h:641:15: note: in expansion of macro ‘KERN_INFO’
641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
| ^~~~~~~~~../lib/kunit/kunit-example-test.c:23:2: note: in expansion of macro ‘kunit_info’
23 | kunit_info(test, "invalid: %s", 42);
After this patch, it gets noisier:
In file included from ../lib/kunit/kunit-example-test.c:9:../lib/kunit/kunit-example-test.c: In function ‘example_simple_test’:../include/linux/kern_levels.h:5:18: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
5 | #define KERN_SOH "\001" /* ASCII Start Of Header */
| ^~~~~~../include/kunit/test.h:622:10: note: in definition of macro ‘kunit_log’
622 | printk(lvl fmt, ##__VA_ARGS__); \
| ^~~
../include/kunit/test.h:641:2: note: in expansion of macro ‘kunit_printk’
641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
| ^~~~~~~~~~~~
../include/linux/kern_levels.h:14:19: note: in expansion of macro ‘KERN_SOH’
14 | #define KERN_INFO KERN_SOH "6" /* informational */
| ^~~~~~~~
../include/kunit/test.h:641:15: note: in expansion of macro ‘KERN_INFO’
641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
| ^~~~~~~~~
../lib/kunit/kunit-example-test.c:23:2: note: in expansion of macro ‘kunit_info’
23 | kunit_info(test, "invalid: %s", 42);
| ^~~~~~~~~~
../include/kunit/test.h:105:31: warning: format ‘%s’ expects argument
of type ‘char *’, but argument 4 has type ‘int’ [-Wformat=]
105 | #define KUNIT_SUBTEST_INDENT " "
| ^~~~~~../include/kunit/test.h:623:42: note: in definition of macro ‘kunit_log’
623 | kunit_log_append((test_or_suite)->log, fmt "\n", \
| ^~~../include/kunit/test.h:628:23: note: in expansion of macro ‘KUNIT_SUBTEST_INDENT’
628 | kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
| ^~~~~~~~~~~~~~~~~~~~
../include/kunit/test.h:641:2: note: in expansion of macro ‘kunit_printk’
641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
| ^~~~~~~~~~~~
../lib/kunit/kunit-example-test.c:23:2: note: in expansion of macro ‘kunit_info’
23 | kunit_info(test, "invalid: %s", 42);
| ^~~~~~~~~~Quoted 8 lines
include/kunit/test.h +0
-0/* * printk and log to per-test or per-suite log buffer. Logging only done lib/kunit/string-stream.h +0
-0
lib/kunit/string-stream.h struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp); · line 40
Quoted 9 lines
lib/kunit/string-stream.h +3
-3@@ -35,9 +35,9 @@ struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp); int __printf(2, 3) string_stream_add(struct string_stream *stream, const char *fmt, ...); -int string_stream_vadd(struct string_stream *stream, - const char *fmt, - va_list args); +int __printf(2, 0) string_stream_vadd(struct string_stream *stream, + const char *fmt, + va_list args);
This is never called with a literal fmt string.
It's currently only ever called through the _add variant, which does have __printf(2,3).
So this can't catch any mistakes currently.
And I think it's hard to imagine we'd ever pass in a literal format string w/ a va_list.
Quoted 10 lines
lib/kunit/string-stream.h +0
-1char *string_stream_get_string (struct string_stream *stream); --2.31.1.751.gd2f1c929bd-goog
--
You received this message because you are subscribed to the Google Groups "KUnit Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20210513200350.854429-1-davidgow%40google.com.
Quoted 16 lines · Daniel Latypov
Quoted 10 lines · David Gow' via KUnit Development
Some KUnit functions use variable arguments to implement a printf-like format string. Use the
__printf()attribute to let the compiler warn if invalid format strings are passed in.If the kernel is build with W=1, it complained about the lack of these
specifiers, e.g.:../lib/kunit/test.c:72:2: warning: function ‘kunit_log_append’ might be a candidate for ‘gnu_printf’ format attribute [-Wsuggest-attribute=format]Signed-off-by David Gow <[email protected]>Reviewed-by Daniel Latypov <[email protected]>
Quoted 8 lines
As noted below, these additions don't really do anything. Unfortunately, they just make compiler warnings noisier in the case of
kunit_log_append().But if this silences a W=1 warning, then we should probably add them in. I guess it also serves as documentation that we're using the same standard format specifiers and not something custom, which is nice.
Yeah: I did this to get rid of the W=1 warnings. I don't know if there's a way of doing this which would be less verbose: I do think that the format checking is worthwhile in general, even if we're hitting a few nasty cases where things are nested in macros.
Quoted 68 lines
Quoted 10 lines
--- include/kunit/test.h | 2 +- lib/kunit/string-stream.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-)include/kunit/test.h +0
-0include/kunit/test.h static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp) · line 613
Quoted 5 lines
include/kunit/test.h +1
-1@@ -610,7 +610,7 @@ static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)void kunit_cleanup (struct kunit *test); -void kunit_log_append (char *log,const char *fmt, ...); +void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);Before this patch:
../lib/kunit/kunit-example-test.c: In function ‘example_simple_test’:../include/linux/kern_levels.h:5:18: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
5 | #defineKERN_SOH"\001" /* ASCII Start Of Header */
| ^~~~~~../include/kunit/test.h:622:10: note: in definition of macro ‘kunit_log’622 | printk(lvl fmt, ##__VA_ARGS__); \ | ^~~ ../include/kunit/test.h:641:2: note: in expansion of macro ‘kunit_printk’ 641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) | ^~~~~~~~~~~~ ../include/linux/kern_levels.h:14:19: note: in expansion of macro ‘KERN_SOH’ 14 | #define KERN_INFO KERN_SOH "6" /* informational */ | ^~~~~~~~ ../include/kunit/test.h:641:15: note: in expansion of macro ‘KERN_INFO’ 641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) | ^~~~~~~~~
../lib/kunit/kunit-example-test.c:23:2: note: in expansion of macro ‘kunit_info’
23 |kunit_info(test, "invalid: %s", 42);After this patch, it gets noisier:
In file included from../lib/kunit/kunit-example-test.c:9:../lib/kunit/kunit-example-test.c: In function ‘example_simple_test’:../include/linux/kern_levels.h:5:18: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
5 | #defineKERN_SOH"\001" /* ASCII Start Of Header */
| ^~~~~~../include/kunit/test.h:622:10: note: in definition of macro ‘kunit_log’622 | printk(lvl fmt, ##__VA_ARGS__); \ | ^~~ ../include/kunit/test.h:641:2: note: in expansion of macro ‘kunit_printk’ 641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) | ^~~~~~~~~~~~ ../include/linux/kern_levels.h:14:19: note: in expansion of macro ‘KERN_SOH’ 14 | #define KERN_INFO KERN_SOH "6" /* informational */ | ^~~~~~~~ ../include/kunit/test.h:641:15: note: in expansion of macro ‘KERN_INFO’ 641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) | ^~~~~~~~~ ../lib/kunit/kunit-example-test.c:23:2: note: in expansion of macro ‘kunit_info’ 23 | kunit_info(test, "invalid: %s", 42); | ^~~~~~~~~~ ../include/kunit/test.h:105:31: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘int’ [-Wformat=] 105 | #define KUNIT_SUBTEST_INDENT " " | ^~~~~~
../include/kunit/test.h:623:42: note: in definition of macro ‘kunit_log’
623 | kunit_log_append((test_or_suite)->log, fmt "\n", \
| ^~~../include/kunit/test.h:628:23: note: in expansion of macro ‘KUNIT_SUBTEST_INDENT’
Quoted 10 lines
628 | kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
| ^~~~~~~~~~~~~~~~~~~~
../include/kunit/test.h:641:2: note: in expansion of macro ‘kunit_printk’
641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
| ^~~~~~~~~~~~
../lib/kunit/kunit-example-test.c:23:2: note: in expansion of macro ‘kunit_info’
23 | kunit_info(test, "invalid: %s", 42);
| ^~~~~~~~~~Yeah: that is pretty ugly. TBH, it was pretty ugly beforehand, and this does make it worse. I guess that's the price we pay for having so many nested macros, as well.
Personally, I suspect this is still worth it to get rid of the compiler warnings, but only just.
Quoted 22 lines
Quoted 8 lines
include/kunit/test.h +0
-0/* * printk and log to per-test or per-suite log buffer. Logging only done lib/kunit/string-stream.h +0
-0lib/kunit/string-stream.h struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp); · line 40
Quoted 9 lines
lib/kunit/string-stream.h +3
-3@@ -35,9 +35,9 @@ struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp); int __printf(2, 3) string_stream_add(struct string_stream *stream, const char *fmt, ...); -int string_stream_vadd(struct string_stream *stream, - const char *fmt, - va_list args); +int __printf(2, 0) string_stream_vadd(struct string_stream *stream, + const char *fmt, + va_list args);This is never called with a literal
fmtstring.
It's currently only ever called through the_addvariant, which does have__printf(2,3).
Quoted 4 lines
So this can't catch any mistakes currently.
And I think it's hard to imagine we'd ever pass in a literal format string w/ ava_list.
Yeah: I was tempted to leave this one out, but it was triggering warnings with the "you should use __printf()" heuristic. In fact, it had two warnings.
The __printf() specifier documentation does specifically call out cases where a va_list is passed in as a case to use '0' for the positional argument, but only the format string is checked for
validity: there's no typechecking.
Quoted 10 lines
Quoted 10 lines
lib/kunit/string-stream.h +0
-1char *string_stream_get_string (struct string_stream *stream); --2.31.1.751.gd2f1c929bd-goog
--
You received this message because you are subscribed to the Google Groups "KUnit Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20210513200350.854429-1-davidgow%40google.com.
Quoted 27 lines · David Gow
Quoted 16 lines · Daniel Latypov
Quoted 10 lines · David Gow' via KUnit Development
Some KUnit functions use variable arguments to implement a printf-like format string. Use the
__printf()attribute to let the compiler warn if invalid format strings are passed in.If the kernel is build with W=1, it complained about the lack of these
specifiers, e.g.:../lib/kunit/test.c:72:2: warning: function ‘kunit_log_append’ might be a candidate for ‘gnu_printf’ format attribute [-Wsuggest-attribute=format]Signed-off-by David Gow <[email protected]>Reviewed-by Daniel Latypov <[email protected]>Quoted 8 lines
As noted below, these additions don't really do anything. Unfortunately, they just make compiler warnings noisier in the case of
kunit_log_append().But if this silences a W=1 warning, then we should probably add them in. I guess it also serves as documentation that we're using the same standard format specifiers and not something custom, which is nice.
Quoted 4 lines
Yeah: I did this to get rid of the W=1 warnings. I don't know if there's a way of doing this which would be less verbose: I do think that the format checking is worthwhile in general, even if we're hitting a few nasty cases where things are nested in macros.
Yeah. In case it wasn't clear, I think both annotations are clearly worth having if they silence W=1 warnings.
We're more likely to produce more noise w/ those warnings than the extra noise of someone making a typo or forgetting in a kunit_info() somewhere while writing a test.
It's just a bit sad that doing what the compiler suggests doesn't really make life better :(
Quoted 132 lines
Quoted 70 lines
Quoted 12 lines
--- include/kunit/test.h | 2 +- lib/kunit/string-stream.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-)include/kunit/test.h +0
-0include/kunit/test.h static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp) · line 613
Quoted 5 lines
include/kunit/test.h +1
-1@@ -610,7 +610,7 @@ static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)void kunit_cleanup (struct kunit *test); -void kunit_log_append (char *log,const char *fmt, ...); +void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);Before this patch:
../lib/kunit/kunit-example-test.c: In function ‘example_simple_test’:../include/linux/kern_levels.h:5:18: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
5 | #defineKERN_SOH"\001" /* ASCII Start Of Header */
| ^~~~~~../include/kunit/test.h:622:10: note: in definition of macro ‘kunit_log’622 | printk(lvl fmt, ##__VA_ARGS__); \ | ^~~ ../include/kunit/test.h:641:2: note: in expansion of macro ‘kunit_printk’ 641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) | ^~~~~~~~~~~~ ../include/linux/kern_levels.h:14:19: note: in expansion of macro ‘KERN_SOH’ 14 | #define KERN_INFO KERN_SOH "6" /* informational */ | ^~~~~~~~ ../include/kunit/test.h:641:15: note: in expansion of macro ‘KERN_INFO’ 641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) | ^~~~~~~~~
../lib/kunit/kunit-example-test.c:23:2: note: in expansion of macro ‘kunit_info’
23 |kunit_info(test, "invalid: %s", 42);After this patch, it gets noisier:
In file included from../lib/kunit/kunit-example-test.c:9:../lib/kunit/kunit-example-test.c: In function ‘example_simple_test’:../include/linux/kern_levels.h:5:18: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
5 | #defineKERN_SOH"\001" /* ASCII Start Of Header */
| ^~~~~~../include/kunit/test.h:622:10: note: in definition of macro ‘kunit_log’622 | printk(lvl fmt, ##__VA_ARGS__); \ | ^~~ ../include/kunit/test.h:641:2: note: in expansion of macro ‘kunit_printk’ 641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) | ^~~~~~~~~~~~ ../include/linux/kern_levels.h:14:19: note: in expansion of macro ‘KERN_SOH’ 14 | #define KERN_INFO KERN_SOH "6" /* informational */ | ^~~~~~~~ ../include/kunit/test.h:641:15: note: in expansion of macro ‘KERN_INFO’ 641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) | ^~~~~~~~~ ../lib/kunit/kunit-example-test.c:23:2: note: in expansion of macro ‘kunit_info’ 23 | kunit_info(test, "invalid: %s", 42); | ^~~~~~~~~~ ../include/kunit/test.h:105:31: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘int’ [-Wformat=] 105 | #define KUNIT_SUBTEST_INDENT " " | ^~~~~~
../include/kunit/test.h:623:42: note: in definition of macro ‘kunit_log’
623 | kunit_log_append((test_or_suite)->log, fmt "\n", \
| ^~~../include/kunit/test.h:628:23: note: in expansion of macro ‘KUNIT_SUBTEST_INDENT’Quoted 10 lines
628 | kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \ | ^~~~~~~~~~~~~~~~~~~~ ../include/kunit/test.h:641:2: note: in expansion of macro ‘kunit_printk’ 641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) | ^~~~~~~~~~~~ ../lib/kunit/kunit-example-test.c:23:2: note: in expansion of macro ‘kunit_info’ 23 | kunit_info(test, "invalid: %s", 42); | ^~~~~~~~~~Yeah: that is pretty ugly. TBH, it was pretty ugly beforehand, and this does make it worse. I guess that's the price we pay for having so many nested macros, as well.
Personally, I suspect this is still worth it to get rid of the compiler warnings, but only just.Quoted 22 lines
Quoted 8 lines
include/kunit/test.h +0
-0/* * printk and log to per-test or per-suite log buffer. Logging only done lib/kunit/string-stream.h +0
-0lib/kunit/string-stream.h struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp); · line 40
Quoted 9 lines
lib/kunit/string-stream.h +3
-3@@ -35,9 +35,9 @@ struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp); int __printf(2, 3) string_stream_add(struct string_stream *stream, const char *fmt, ...); -int string_stream_vadd(struct string_stream *stream, - const char *fmt, - va_list args); +int __printf(2, 0) string_stream_vadd(struct string_stream *stream, + const char *fmt, + va_list args);This is never called with a literal
fmtstring.
It's currently only ever called through the_addvariant, which does have__printf(2,3).Quoted 4 lines
So this can't catch any mistakes currently.
And I think it's hard to imagine we'd ever pass in a literal format string w/ ava_list.Yeah: I was tempted to leave this one out, but it was triggering warnings with the "you should use
__printf()" heuristic. In fact, it had two warnings.
The__printf()specifier documentation does specifically call out cases where ava_listis passed in as a case to use '0' for the positional argument, but only the format string is checked for
validity: there's no typechecking.Quoted 10 lines
Quoted 10 lines
lib/kunit/string-stream.h +0
-1char *string_stream_get_string (struct string_stream *stream); --2.31.1.751.gd2f1c929bd-goog
--
You received this message because you are subscribed to the Google Groups "KUnit Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20210513200350.854429-1-davidgow%40google.com.
Quoted 10 lines · David Gow
Some KUnit functions use variable arguments to implement a printf-like format string. Use the
__printf()attribute to let the compiler warn if invalid format strings are passed in.If the kernel is build with W=1, it complained about the lack of these
specifiers, e.g.:../lib/kunit/test.c:72:2: warning: function ‘kunit_log_append’ might be a candidate for ‘gnu_printf’ format attribute [-Wsuggest-attribute=format]Signed-off-by David Gow <[email protected]>