Skip to contentSkip to conversation

Add argument checking for a number of functions

Qemu-devel In review Stefan Weil 4 participants 9 messages Jul 1, 2010

lore
Share & tools
lore
#0 lore raw

gcc can check printf like variable arguments. These patches tell gcc to do so for several functions.

[PATCH 1/4] blockdev.h: Add GCC attribute (check format arguments) [PATCH 2/4] darwin-user: Add GCC attribute (check format arguments) [PATCH 3/4] qemu-char.h: Add GCC attribute (check format arguments) [PATCH 4/4] slirp.h: Add GCC attribute (check format arguments)

#1 lore raw

PATCH 1/4 blockdev.h: Add GCC attribute (check format arguments)

---
 blockdev.h |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
blockdev.h +2 -1
index 23ea576..3c5c85d 100644
@@ -42,7 +42,8 @@ extern int drive_get_max_bus(BlockInterfaceType type);
 extern void drive_uninit(DriveInfo *dinfo);
 extern const char *drive_get_serial(BlockDriverState *bdrv);
 
-extern QemuOpts *drive_add(const char *file, const char *fmt, ...);
+extern QemuOpts *drive_add(const char *file, const char *fmt, ...)
+    __attribute__ ((__format__ (__printf__, 2, 3)));
 extern DriveInfo *drive_init(QemuOpts *arg, int default_to_scsi,
                              int *fatal_error);
 
Signature · 1 line
1.7.1
#2 lore raw

Re: [Qemu-devel] [PATCH 1/4] blockdev.h: Add GCC attribute (check format arguments)

Quoted 10 lines · Stefan Weil

---
 blockdev.h |    3 ++-

 1 files changed, 2 insertions(+), 1 deletions(-)
blockdev.h +0 -0
index 23ea576..3c5c85d 100644

blockdev.h extern int drive_get_max_bus(BlockInterfaceType type); · line 48

Quoted 8 lines
blockdev.h +2 -1
@@ -42,7 +42,8 @@ extern int drive_get_max_bus(BlockInterfaceType type);
  extern void drive_uninit(DriveInfo *dinfo);
  extern const char *drive_get_serial(BlockDriverState *bdrv);
 
-extern QemuOpts *drive_add(const char *file, const char *fmt, ...);
+extern QemuOpts *drive_add(const char *file, const char *fmt, ...)
+    __attribute__ ((__format__ (__printf__, 2, 3)));
  extern DriveInfo *drive_init(QemuOpts *arg, int default_to_scsi,
                              int *fatal_error);

I lost the cover letter, so this applies to all patches: Wouldn't it make sense to make GCC_FMT_ATTR(n, m) from audio/audio_int.h available universally and then use that?

#3 lore raw

Re: [Qemu-devel] [PATCH 1/4] blockdev.h: Add GCC attribute (check format arguments)

Am 01.07.2010 22:10, schrieb Blue Swirl:

Quoted 14 lines · Stefan Weil
Quoted 13 lines
---
 blockdev.h |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
blockdev.h +0 -0
index 23ea576..3c5c85d 100644
@@ -42,7 +42,8 @@ extern int drive_get_max_bus(BlockInterfaceType type);
 extern void drive_uninit(DriveInfo *dinfo);
 extern const char *drive_get_serial(BlockDriverState *bdrv);
 
Quoted 9 lines

blockdev.h extern int drive_get_max_bus(BlockInterfaceType type); · line 48

Quoted 5 lines
blockdev.h +2 -1
-extern QemuOpts *drive_add(const char *file, const char *fmt, ...);
+extern QemuOpts *drive_add(const char *file, const char *fmt, ...)
+    __attribute__ ((__format__ (__printf__, 2, 3)));
 extern DriveInfo *drive_init(QemuOpts *arg, int default_to_scsi,
                             int *fatal_error);

I lost the cover letter, so this applies to all patches: Wouldn't it make sense to make GCC_FMT_ATTR(n, m) from audio/audio_int.h available universally and then use that?

That's a matter of personal taste:

GCC_FMT_ATTR(n, m) is shorter, but a human reader has to look it up once to see what it does (ok, some readers might guess it right). The compiler has to look it up, too, so a common header file is needed.

When I prepared the patches, I did not notice that some functions used GCC_FMT_ATTR. I added the __attribute__ macro to these functions and got a compiler error... This shows that at least for me GCC_FMT_ATTR was confusing (of course it no longer is).

Some people prefer GCC_FMT_ATTR because they want to be able to redefine it for non-gcc compilers.

__attribute__ can also be redefined for that case, so that is not a very strong argument.

I prefer using __attribute__ without intermediate macro, but don't mind if a different style is preferred for qemu.

Regards
Stefan

#4 lore raw

Re: [Qemu-devel] [PATCH 1/4] blockdev.h: Add GCC attribute (check format arguments)

Quoted 39 lines · Stefan Weil

Am 01.07.2010 22:10, schrieb Blue Swirl:

Quoted 14 lines · Stefan Weil
Quoted 13 lines
---
 blockdev.h |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
blockdev.h +0 -0
index 23ea576..3c5c85d 100644
@@ -42,7 +42,8 @@ extern int drive_get_max_bus(BlockInterfaceType type);
 extern void drive_uninit(DriveInfo *dinfo);
 extern const char *drive_get_serial(BlockDriverState *bdrv);
 
Quoted 9 lines

blockdev.h extern int drive_get_max_bus(BlockInterfaceType type); · line 48

Quoted 5 lines
blockdev.h +2 -1
-extern QemuOpts *drive_add(const char *file, const char *fmt, ...);
+extern QemuOpts *drive_add(const char *file, const char *fmt, ...)
+    __attribute__ ((__format__ (__printf__, 2, 3)));
 extern DriveInfo *drive_init(QemuOpts *arg, int default_to_scsi,
                             int *fatal_error);

I lost the cover letter, so this applies to all patches: Wouldn't it make sense to make GCC_FMT_ATTR(n, m) from audio/audio_int.h available universally and then use that?

That's a matter of personal taste:

GCC_FMT_ATTR(n, m) is shorter, but a human reader has to look it up once to see what it does (ok, some readers might guess it right). The compiler has to look it up, too, so a common header file is needed.

When I prepared the patches, I did not notice that some functions used GCC_FMT_ATTR. I added the __attribute__ macro to these functions and got a compiler error... This shows that at least for me GCC_FMT_ATTR was confusing (of course it no longer is).

Quoted 5 lines

Some people prefer GCC_FMT_ATTR because they want to be able to redefine it for non-gcc compilers.

__attribute__ can also be redefined for that case, so that is not a very strong argument.

Just to be pedantic, you can _not_ redfined __attribute the name is reserved..

Quoted 8 lines

I prefer using __attribute__ without intermediate macro, but don't mind if a different style is preferred for qemu.

Regards
Stefan

Signature · 1 line
mailto:[email protected]
#5 lore raw

PATCH 2/4 darwin-user: Add GCC attribute (check format arguments)

In reply to Stefan Weil
---
 darwin-user/qemu.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
darwin-user/qemu.h +1 -1
index 462bbda..c8adce7 100644
@@ -100,7 +100,7 @@ int do_sigaction(int sig, const struct sigaction *act,
 int do_sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss);
 
 void gemu_log(const char *fmt, ...) __attribute__((format(printf,1,2)));
-void qerror(const char *fmt, ...);
+void qerror(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
 
 void write_dt(void *ptr, unsigned long addr, unsigned long limit, int flags);
 
Signature · 1 line
1.7.1
#6 lore raw

PATCH 3/4 qemu-char.h: Add GCC attribute (check format arguments)

In reply to Stefan Weil
---
 qemu-char.h |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
qemu-char.h +2 -1
index e3a0783..1bbac6e 100644
@@ -76,7 +76,8 @@ CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
                                     void (*init)(struct CharDriverState *s));
 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s));
 void qemu_chr_close(CharDriverState *chr);
-void qemu_chr_printf(CharDriverState *s, const char *fmt, ...);
+void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
+    __attribute__ ((__format__ (__printf__, 2, 3)));
 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len);
 void qemu_chr_send_event(CharDriverState *s, int event);
 void qemu_chr_add_handlers(CharDriverState *s,
Signature · 1 line
1.7.1
#7 lore raw

PATCH 4/4 slirp.h: Add GCC attribute (check format arguments)

In reply to Stefan Weil
---
 slirp/slirp.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
slirp/slirp.h +1 -1
index 98a2644..c30c19e 100644
@@ -294,7 +294,7 @@ void if_start(struct ttys *);
  long gethostid(void);
 #endif
 
-void lprint(const char *, ...);
+void lprint(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
 
 #ifndef _WIN32
 #include <netdb.h>
Signature · 1 line
1.7.1
#8 lore raw
In reply to Stefan Weil
Quoted 7 lines · Stefan Weil

gcc can check printf like variable arguments. These patches tell gcc to do so for several functions.

[PATCH 1/4] blockdev.h: Add GCC attribute (check format arguments) [PATCH 2/4] darwin-user: Add GCC attribute (check format arguments) [PATCH 3/4] qemu-char.h: Add GCC attribute (check format arguments) [PATCH 4/4] slirp.h: Add GCC attribute (check format arguments)

r~