virtio-9p: move G_GNUC_PRINTF to header
cover virtio-9p: move G_GNUC_PRINTF to header
v9fs_string_sprintf() and v9fs_path_sprintf() already have G_GNUC_PRINTF annotations in their own *.c files, but the prototypes in the corresponding headers lack them. When another translation unit includes only the header, -Wformat can no longer validate the argument list.
This series relocates the annotations to fsdev/9p-marshal.h and hw/9pfs/9p.h, then drops the now-redundant annotations in the *.c files. There is no functional change.
I've checked all call sites for these two helper function, all of them already passes the correct number of arguments.
A minimal PoC (sent as the next mail in the thread) demo how G_GNUC_PRINTF behaves differently when the attribute is present only in code.c or code.h file.
--
Sean Wei (3):
fsdev/9p-marshal: move G_GNUC_PRINTF to header
hw/9pfs: move G_GNUC_PRINTF to header
fsdev/9p-marshal.c | 3 +-- fsdev/9p-marshal.h | 2 +- hw/9pfs/9p.c | 3 +-- hw/9pfs/9p.h | 2 +- 4 files changed, 4 insertions(+), 6 deletions(-)
Signature · 1 line
2.49.0
PATCH 1/2 fsdev/9p-marshal: move G_GNUC_PRINTF to header
v9fs_string_sprintf() is annotated with G_GNUC_PRINTF(2, 3) in 9p-marshal.c, but the prototype in fsdev/9p-marshal.h is missing the attribute, so callers that include only the header do not get format checking.
Move the annotation to the header and delete the duplicate in the source file. No behavior change.
--- fsdev/9p-marshal.c | 3 +-- fsdev/9p-marshal.h | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-)
fsdev/9p-marshal.c +1 -2
@@ -27,8 +27,7 @@ void v9fs_string_free(V9fsString *str) str->size =0 ; } -void G_GNUC_PRINTF(2, 3) -v9fs_string_sprintf(V9fsString *str, const char *fmt, ...) +void v9fs_string_sprintf (V9fsString *str,const char *fmt, ...) {va_list ap;
fsdev/9p-marshal.h +1 -1
@@ -76,7 +76,7 @@ static inline void v9fs_string_init(V9fsString *str) str->size =0 ; }void v9fs_string_free (V9fsString *str); -void v9fs_string_sprintf (V9fsString *str,const char *fmt, ...); +void G_GNUC_PRINTF(2, 3) v9fs_string_sprintf(V9fsString *str, const char *fmt, ...); void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs);#endif
Signature · 1 line
2.49.0
Re: [PATCH 1/2] fsdev/9p-marshal: move G_GNUC_PRINTF to header
Quoted 5 lines · Sean Wei
v9fs_string_sprintf()is annotated withG_GNUC_PRINTF(2, 3)in9p-marshal.c, but the prototype infsdev/9p-marshal.his missing the attribute, so callers that include only the header do not get format checking.
Quoted 8 lines
Move the annotation to the header and delete the duplicate in the source file. No behavior change.
Signed-off-by Sean Wei <[email protected]>--- fsdev/9p-marshal.c | 3 +-- fsdev/9p-marshal.h | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-)
[PoC] show header-vs-source G_GNUC_PRINTF behavior
In reply to Sean WeiThis patch is NOT meant for merge.
Just a simple proof-of-concept so reviewers can quickly reproduce the difference between placing a G_GNUC_PRINTF attribute in *.c versus *.h file.
What it tests
-------------
* Two identical printf-style helpers (my_printf) are provided:
- v1: attribute lives in foo-v1.c
- v2: attribute lives in foo-v2.h
* Two callers (bar-v1.c, bar-v2.c) intentionally pass too few
arguments: my_printf("%d %d %d\n", 1).
* A trivial Makefile builds each variant and shows whether GCC emits the missing-argument warning.
Expected result
---------------
$ make -C poc run // Compiling version 1 (Place G_GNUC_PRINTF in 'foo-v1.c') No warning will appear, sliently cause security flaw
// Compiling version 2 (Place G_GNUC_PRINTF in 'foo-v2.h')
bar-v2.c:4:17: warning: more '%' conversions than data arguments [-Wformat-insufficient-args]
--- poc/foo-v1.h | 3 +++ poc/foo-v1.c | 10 ++++++++++ poc/bar-v1.c | 6 ++++++ poc/foo-v2.h | 3 +++ poc/foo-v2.c | 10 ++++++++++ poc/bar-v2.c | 6 ++++++ poc/Makefile | 39 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 77 insertions(+) create mode 100644 poc/foo-v1.h create mode 100644 poc/foo-v1.c create mode 100644 poc/bar-v1.c create mode 100644 poc/foo-v2.h create mode 100644 poc/foo-v2.c create mode 100644 poc/bar-v2.c create mode 100644 poc/Makefile
poc/foo-v1.h new file +3 -0
@@ -0,0 +1,3 @@ +#define G_GNUC_PRINTF (n, m) __attribute__((format(printf, n, m))) + +void my_printf (const char *fmt, ...);
poc/foo-v1.c new file +10 -0
@@ -0,0 +1,10 @@ +#include <stdio.h> +#include <stdarg.h> +#include "foo-v2.h" + +void G_GNUC_PRINTF(1, 2) my_printf(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); +}
poc/bar-v1.c new file +6 -0
@@ -0,0 +1,6 @@ +#include "foo-v1.h" + +int main () { +my_printf ("%d %d %d\n" ,1 );// missing arguments +return 0 ; +}
poc/foo-v2.h new file +3 -0
@@ -0,0 +1,3 @@ +#define G_GNUC_PRINTF (n, m) __attribute__((format(printf, n, m))) + +void G_GNUC_PRINTF(1, 2) my_printf(const char *fmt, ...);
poc/foo-v2.c new file +10 -0
@@ -0,0 +1,10 @@ +#include <stdio.h> +#include <stdarg.h> +#include "foo-v1.h" + +void my_printf (const char *fmt, ...) { +va_list ap; +va_start (ap, fmt); +vprintf (fmt, ap); +va_end (ap); +}
poc/bar-v2.c new file +6 -0
@@ -0,0 +1,6 @@ +#include "foo-v2.h" + +int main () { +my_printf ("%d %d %d\n" ,1 );// missing arguments +return 0 ; +}
poc/Makefile new file +39 -0
@@ -0,0 +1,39 @@ +CC ?= gcc +CFLAGS ?= -Wall + +TARGET = prog-v1 prog-v2 +SRCS = foo-v1.c foo-v2.c bar-v1.c bar-v2.c +OBJS = $(SRCS:.c=.o) + +.PHONY: run + +run: clean prog-v1 prog-v2 + + +prog-v1: + @echo + @echo + @echo "### Compiling version 1 ###" + @echo "Place G_GNUC_PRINTF in 'foo.c' only" + @echo "No warning will appear, sliently cause security flaw" + @echo + $(CC) $(CFLAGS) -c foo-v1.c -o foo-v1.o + $(CC) $(CFLAGS) -c bar-v1.c -o bar-v1.o + @echo + $(CC) foo-v1.o bar-v1.o -o $@ + +prog-v2: + @echo + @echo + @echo "### Compiling version 2###" + @echo "Place G_GNUC_PRINTF in 'foo.h' instead" + @echo "Show warning for missing arguments" + @echo + $(CC) $(CFLAGS) -c foo-v2.c -o foo-v2.o + $(CC) $(CFLAGS) -c bar-v2.c -o bar-v2.o + @echo + $(CC) foo-v2.o bar-v2.o -o $@ + +clean: + @echo "### Clean all artifacts ###" + rm -f $(TARGET) $(OBJS)
Signature · 1 line
2.49.0
PATCH 2/2 hw/9pfs: move G_GNUC_PRINTF to header
In reply to Sean Weiv9fs_path_sprintf() is annotated with G_GNUC_PRINTF(2, 3) in hw/9pfs/9p.c, but the prototype in hw/9pfs/9p.h is missing the attribute, so callers that include only the header do not get format checking.
Move the annotation to the header and delete the duplicate in the source file. No behavior change.
--- hw/9pfs/9p.c | 3 +-- hw/9pfs/9p.h | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-)
hw/9pfs/9p.c +1 -2
@@ -201,8 +201,7 @@ void v9fs_path_free(V9fsPath *path) } -void G_GNUC_PRINTF(2, 3) -v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...) +void v9fs_path_sprintf (V9fsPath *path,const char *fmt, ...) {va_list ap;
hw/9pfs/9p.h +1 -1
@@ -456,7 +456,7 @@ static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu) void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu);void v9fs_path_init (V9fsPath *path);void v9fs_path_free (V9fsPath *path); -void v9fs_path_sprintf (V9fsPath *path,const char *fmt, ...); +void G_GNUC_PRINTF(2, 3) v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...); void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src); size_t v9fs_readdir_response_size(V9fsString *name); int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
Signature · 1 line
2.49.0
Re: [PATCH 2/2] hw/9pfs: move G_GNUC_PRINTF to header
Quoted 5 lines · Sean Wei
v9fs_path_sprintf()is annotated withG_GNUC_PRINTF(2, 3)inhw/9pfs/9p.c, but the prototype inhw/9pfs/9p.his missing the attribute, so callers that include only the header do not get format checking.
Quoted 8 lines
Move the annotation to the header and delete the duplicate in the source file. No behavior change.
Signed-off-by Sean Wei <[email protected]>--- hw/9pfs/9p.c | 3 +-- hw/9pfs/9p.h | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-)
Quoted 23 lines · CEST Sean Wei
v9fs_string_sprintf()andv9fs_path_sprintf()already haveG_GNUC_PRINTFannotations in their own *.c files, but the prototypes in the corresponding headers lack them. When another translation unit includes only the header, -Wformat can no longer validate the argument list.This series relocates the annotations to
fsdev/9p-marshal.handhw/9pfs/9p.h, then drops the now-redundant annotations in the *.c files. There is no functional change.I've checked all call sites for these two helper function, all of them already passes the correct number of arguments.
A minimal PoC (sent as the next mail in the thread) demo how
G_GNUC_PRINTFbehaves differently when the attribute is present only incode.corcode.hfile.--
Sean Wei (3):
fsdev/9p-marshal: moveG_GNUC_PRINTFto header
hw/9pfs: moveG_GNUC_PRINTFto header
Quoted 7 lines
fsdev/9p-marshal.c | 3 +-- fsdev/9p-marshal.h | 2 +- hw/9pfs/9p.c | 3 +-- hw/9pfs/9p.h | 2 +- 4 files changed, 4 insertions(+), 6 deletions(-)
With code style fix queued on 9p.next:
https://github.com/cschoenebeck/qemu/commits/9p.next
Please run scripts/checkpatch.pl next time.
Thanks!
/Christian
Hi Christian,
Quoted 32 lines · Christian Schoenebeck
Quoted 23 lines · CEST Sean Wei
v9fs_string_sprintf()andv9fs_path_sprintf()already haveG_GNUC_PRINTFannotations in their own *.c files, but the prototypes in the corresponding headers lack them. When another translation unit includes only the header, -Wformat can no longer validate the argument list.This series relocates the annotations to
fsdev/9p-marshal.handhw/9pfs/9p.h, then drops the now-redundant annotations in the *.c files. There is no functional change.I've checked all call sites for these two helper function, all of them already passes the correct number of arguments.
A minimal PoC (sent as the next mail in the thread) demo how
G_GNUC_PRINTFbehaves differently when the attribute is present only incode.corcode.hfile.--
Sean Wei (3):
fsdev/9p-marshal: moveG_GNUC_PRINTFto header
hw/9pfs: moveG_GNUC_PRINTFto headerQuoted 7 lines
fsdev/9p-marshal.c | 3 +-- fsdev/9p-marshal.h | 2 +- hw/9pfs/9p.c | 3 +-- hw/9pfs/9p.h | 2 +- 4 files changed, 4 insertions(+), 6 deletions(-)
Quoted 9 lines
With code style fix queued on 9p.next:
https://github.com/cschoenebeck/qemu/commits/9p.nextPlease run
scripts/checkpatch.plnext time.Thanks!
/Christian
Apologies for missing the check-patch step, I’ll make sure to run scripts/checkpatch.pl next time.
Thank you for fixing the code style and queuing the patch!
All the best,
Sean Wei