Skip to contentSkip to conversation

mips_fulong2e: Detect format errors for function prom_set

PATCH In review Stefan Weil 2 participants 19 messages Sep 11, 2010 – Sep 22, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mips_fulong2e: Detect format errors for function prom_set

Add the necessary gcc attribute and fix the detected errors.

Cc Aurelien Jarno <[email protected]>
---
 hw/mips_fulong2e.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
hw/mips_fulong2e.c +5 -3
index cbe7156..2ef5070 100644
@@ -77,6 +77,8 @@ static struct _loaderparams {
 } loaderparams;
 
 static void prom_set(uint32_t* prom_buf, int index, const char *string, ...)
+    __attribute__ ((format (gnu_printf, 3, 4)));
+static void prom_set(uint32_t* prom_buf, int index, const char *string, ...)
 {
     va_list ap;
     int32_t table_addr;
@@ -141,13 +143,13 @@ static int64_t load_kernel (CPUState *env)
     prom_size = ENVP_NB_ENTRIES * (sizeof(int32_t) + ENVP_ENTRY_SIZE);
     prom_buf = qemu_malloc(prom_size);
 
-    prom_set(prom_buf, index++, loaderparams.kernel_filename);
+    prom_set(prom_buf, index++, "%s", loaderparams.kernel_filename);
     if (initrd_size > 0) {
-        prom_set(prom_buf, index++, "rd_start=0x" PRIx64 " rd_size=%li %s",
+        prom_set(prom_buf, index++, "rd_start=0x%" PRIx64 " rd_size=%li %s",
                  cpu_mips_phys_to_kseg0(NULL, initrd_offset), initrd_size,
                  loaderparams.kernel_cmdline);
     } else {
-        prom_set(prom_buf, index++, loaderparams.kernel_cmdline);
+        prom_set(prom_buf, index++, "%s", loaderparams.kernel_cmdline);
     }
 
     /* Setup minimum environment variables */
Signature · 1 line
1.7.0.4
#1 lore raw

PATCH 1/1 mips_malta: Detect format errors for function prom_set

Add the necessary gcc attribute and fix the detected errors.

Cc Aurelien Jarno <[email protected]>
---
 hw/mips_malta.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
hw/mips_malta.c +4 -2
index a458c97..deee273 100644
@@ -684,6 +684,8 @@ static void write_bootloader (CPUState *env, uint8_t *base,
 }
 
 static void prom_set(uint32_t* prom_buf, int index, const char *string, ...)
+    __attribute__ ((format (gnu_printf, 3, 4)));
+static void prom_set(uint32_t* prom_buf, int index, const char *string, ...)
 {
     va_list ap;
     int32_t table_addr;
@@ -750,13 +752,13 @@ static int64_t load_kernel(int big_endian)
     prom_size = ENVP_NB_ENTRIES * (sizeof(int32_t) + ENVP_ENTRY_SIZE);
     prom_buf = qemu_malloc(prom_size);
 
-    prom_set(prom_buf, prom_index++, loaderparams.kernel_filename);
+    prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_filename);
     if (initrd_size > 0) {
         prom_set(prom_buf, prom_index++, "rd_start=0x%" PRIx64 " rd_size=%li %s",
                  cpu_mips_phys_to_kseg0(NULL, initrd_offset), initrd_size,
                  loaderparams.kernel_cmdline);
     } else {
-        prom_set(prom_buf, prom_index++, loaderparams.kernel_cmdline);
+        prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_cmdline);
     }
 
     prom_set(prom_buf, prom_index++, "memsize");
Signature · 1 line
1.7.0.4
#2 lore raw

PATCH 1/2 Add support for gcc format attribute gnu_printf

Since version 4.4.x, gcc supports additional format attributes.

    __attribute__ ((format (gnu_printf, 1, 2)))
should be used instead of
    __attribute__ ((format (printf, 1, 2))

because QEMU always uses standard format strings (even with mingw32).

For older compilers, we simply define gnu_printf = printf, so they work with the new format attribute, too.

---
 configure |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
configure +11 -0
index 4061cb7..1300879 100755
@@ -148,6 +148,17 @@ for flag in $gcc_flags; do
     fi
 done
 
+# Check gnu_printf (supported by gcc >= 4.4.x).
+cat > $TMPC << EOF
+static void test(const char *format, ...)
+    __attribute__ ((format (gnu_printf, 1, 2)));
+static void test(const char *format, ...) {}
+int main(void) { test("\n"); return 0; }
+EOF
+if ! compile_prog "-Werror" ""; then
+    QEMU_CFLAGS="-Dgnu_printf=printf $QEMU_CFLAGS"
+fi
+
 # check that the C compiler works.
 cat > $TMPC <<EOF
 int main(void) {}
Signature · 1 line
1.7.1
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] Add support for gcc format attribute gnu_printf

Quoted 22 lines · Stefan Weil

Since version 4.4.x, gcc supports additional format attributes. __attribute__ ((format (gnu_printf, 1, 2)))
should be used instead of
   __attribute__ ((format (printf, 1, 2))
because QEMU always uses standard format strings (even with mingw32).

For older compilers, we simply define gnu_printf = printf, so they work with the new format attribute, too.

---
 configure |   11 +++++++++++

 1 files changed, 11 insertions(+), 0 deletions(-)
configure +0 -0
index 4061cb7..1300879 100755
@@ -148,6 +148,17 @@ for flag in $gcc_flags; do

    fi
 done

configure for flag in $gcc_flags; do · line 159

Quoted 9 lines
configure +9 -0
+# Check gnu_printf (supported by gcc >= 4.4.x).
+cat > $TMPC << EOF
+static void test(const char *format, ...)
+    __attribute__ ((format (gnu_printf, 1, 2)));
+static void test(const char *format, ...) {}
+int main(void) { test("\n"); return 0; }
+EOF
+if ! compile_prog "-Werror" ""; then
+    QEMU_CFLAGS="-Dgnu_printf=printf $QEMU_CFLAGS"

This could cause problems, for example if system headers declared a function called gnu_printf.

I'd introduce CONFIG_GCC_FMT_ATTR_GNU_PRINTF and adjust GCC_FMT_ATTR definition based on that.

#4 lore raw

PATCH 1/1 Move macros GCC_ATTR and GCC_FMT_ATTR to common header file

By moving the definition of GCC_ATTR and GCC_FMT_ATTR from audio_int.h to qemu-common.h these macros are now generally available for further patches which add the gcc format attribute.

Newer gcc versions support format gnu_printf which is better suited for use in QEMU than format printf (QEMU always uses standard format strings (even with mingw32)).

---
 audio/audio_int.h |    8 --------
 qemu-common.h     |   16 ++++++++++++++++
 2 files changed, 16 insertions(+), 8 deletions(-)
audio/audio_int.h +0 -8
index f6a77ad..d8560b6 100644
@@ -236,14 +236,6 @@ static inline int audio_ring_dist (int dst, int src, int len)
     return (dst >= src) ? (dst - src) : (len - src + dst);
 }
 
-#if defined __GNUC__
-#define GCC_ATTR __attribute__ ((__unused__, format (gnu_printf, 1, 2)))
-#define GCC_FMT_ATTR(n, m) __attribute__ ((format (gnu_printf, n, m)))
-#else
-#define GCC_ATTR /**/
-#define GCC_FMT_ATTR(n, m)
-#endif
-
 static void GCC_ATTR dolog (const char *fmt, ...)
 {
     va_list ap;
qemu-common.h +16 -0
index 956b545..8a2872a 100644
@@ -70,6 +70,22 @@ struct iovec {
 #include <sys/uio.h>
 #endif
 
+#if defined __GNUC__
+# if (__GNUC__ < 4) || \
+     defined(__GNUC_MINOR__) && (__GNUC__ = 4) && (__GNUC_MINOR__ < 4)
+   /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */
+#  define GCC_ATTR __attribute__((__unused__, format(printf, 1, 2)))
+#  define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))
+# else
+   /* Use gnu_printf when supported (qemu uses standard format strings). */
+#  define GCC_ATTR __attribute__((__unused__, format(gnu_printf, 1, 2)))
+#  define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))
+# endif
+#else
+#define GCC_ATTR /**/
+#define GCC_FMT_ATTR(n, m)
+#endif
+
 #ifdef _WIN32
 #define fsync _commit
 #define lseek _lseeki64
Signature · 1 line
1.7.1
#5 lore raw

[Qemu-devel] Re: [PATCH] Move macros GCC_ATTR and GCC_FMT_ATTR to common header file

Please ignore this patch. It's wrong (= instead of ==). I'll send a fixed version.

Sorry,
Stefan

Am 20.09.2010 23:05, schrieb Stefan Weil:

Quoted 63 lines

By moving the definition of GCC_ATTR and GCC_FMT_ATTR from audio_int.h to qemu-common.h these macros are now generally available for further patches which add the gcc format attribute.

Newer gcc versions support format gnu_printf which is better suited for use in QEMU than format printf (QEMU always uses standard format strings (even with mingw32)).

---
  audio/audio_int.h |    8 --------
  qemu-common.h     |   16 ++++++++++++++++
  2 files changed, 16 insertions(+), 8 deletions(-)
audio/audio_int.h +0 -8
index f6a77ad..d8560b6 100644
@@ -236,14 +236,6 @@ static inline int audio_ring_dist (int dst, int src, int len)
      return (dst>= src) ? (dst - src) : (len - src + dst);
  }
 
-#if defined __GNUC__
-#define GCC_ATTR __attribute__ ((__unused__, format (gnu_printf, 1, 2)))
-#define GCC_FMT_ATTR(n, m) __attribute__ ((format (gnu_printf, n, m)))
-#else
-#define GCC_ATTR /**/
-#define GCC_FMT_ATTR(n, m)
-#endif
-
  static void GCC_ATTR dolog (const char *fmt, ...)
  {
      va_list ap;
qemu-common.h +16 -0
index 956b545..8a2872a 100644
@@ -70,6 +70,22 @@ struct iovec {
  #include<sys/uio.h>
  #endif
 
+#if defined __GNUC__
+# if (__GNUC__<  4) || \
+     defined(__GNUC_MINOR__)&&  (__GNUC__ = 4)&&  (__GNUC_MINOR__<  4)
+   /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */
+#  define GCC_ATTR __attribute__((__unused__, format(printf, 1, 2)))
+#  define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))
+# else
+   /* Use gnu_printf when supported (qemu uses standard format strings). */
+#  define GCC_ATTR __attribute__((__unused__, format(gnu_printf, 1, 2)))
+#  define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))
+# endif
+#else
+#define GCC_ATTR /**/
+#define GCC_FMT_ATTR(n, m)
+#endif
+
  #ifdef _WIN32
  #define fsync _commit
  #define lseek _lseeki64
#6 lore raw

PATCH 1/1 Move macros GCC_ATTR and GCC_FMT_ATTR to common header file

By moving the definition of GCC_ATTR and GCC_FMT_ATTR from audio_int.h to qemu-common.h these macros are now generally available for further patches which add the gcc format attribute.

Newer gcc versions support format gnu_printf which is better suited for use in QEMU than format printf (QEMU always uses standard format strings (even with mingw32)).

V2: Use correct operator '==' (instead of '=')

---
 audio/audio_int.h |    8 --------
 qemu-common.h     |   16 ++++++++++++++++
 2 files changed, 16 insertions(+), 8 deletions(-)
audio/audio_int.h +0 -8
index f6a77ad..d8560b6 100644
@@ -236,14 +236,6 @@ static inline int audio_ring_dist (int dst, int src, int len)
     return (dst >= src) ? (dst - src) : (len - src + dst);
 }
 
-#if defined __GNUC__
-#define GCC_ATTR __attribute__ ((__unused__, format (gnu_printf, 1, 2)))
-#define GCC_FMT_ATTR(n, m) __attribute__ ((format (gnu_printf, n, m)))
-#else
-#define GCC_ATTR /**/
-#define GCC_FMT_ATTR(n, m)
-#endif
-
 static void GCC_ATTR dolog (const char *fmt, ...)
 {
     va_list ap;
qemu-common.h +16 -0
index 956b545..e97a96e 100644
@@ -70,6 +70,22 @@ struct iovec {
 #include <sys/uio.h>
 #endif
 
+#if defined __GNUC__
+# if (__GNUC__ < 4) || \
+     defined(__GNUC_MINOR__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 4)
+   /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */
+#  define GCC_ATTR __attribute__((__unused__, format(printf, 1, 2)))
+#  define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))
+# else
+   /* Use gnu_printf when supported (qemu uses standard format strings). */
+#  define GCC_ATTR __attribute__((__unused__, format(gnu_printf, 1, 2)))
+#  define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))
+# endif
+#else
+#define GCC_ATTR /**/
+#define GCC_FMT_ATTR(n, m)
+#endif
+
 #ifdef _WIN32
 #define fsync _commit
 #define lseek _lseeki64
Signature · 1 line
1.7.1
#7 lore raw

[Qemu-devel] Re: [PATCH] Move macros GCC_ATTR and GCC_FMT_ATTR to common header file

Quoted 23 lines · Stefan Weil

By moving the definition of GCC_ATTR and GCC_FMT_ATTR from audio_int.h to qemu-common.h these macros are now generally available for further patches which add the gcc format attribute.

Newer gcc versions support format gnu_printf which is better suited for use in QEMU than format printf (QEMU always uses standard format strings (even with mingw32)).

V2: Use correct operator '==' (instead of '=')

---
 audio/audio_int.h |    8 -------- qemu-common.h     |   16 ++++++++++++++++

 2 files changed, 16 insertions(+), 8 deletions(-)
audio/audio_int.h +0 -0
index f6a77ad..d8560b6 100644

audio/audio_int.h static inline int audio_ring_dist (int dst, int src, int len) · line 241

Quoted 6 lines
audio/audio_int.h +0 -3
@@ -236,14 +236,6 @@ static inline int audio_ring_dist (int dst, int src, int len)
     return (dst >= src) ? (dst - src) : (len - src + dst);
  }
 
-#if defined __GNUC__
-#define GCC_ATTR __attribute__ ((__unused__, format (gnu_printf, 1, 2)))
-#define GCC_FMT_ATTR(n, m) __attribute__ ((format (gnu_printf, n, m)))

The patch doesn't apply, the above lines do not match HEAD.

#8 lore raw

PATCH 1/1 Move macros GCC_ATTR and GCC_FMT_ATTR to common header file

By moving the definition of GCC_ATTR and GCC_FMT_ATTR from audio_int.h to qemu-common.h these macros are now generally available for further patches which add the gcc format attribute.

Newer gcc versions support format gnu_printf which is better suited for use in QEMU than format printf (QEMU always uses standard format strings (even with mingw32)).

V2: Use correct operator '==' (instead of '=')

---
 audio/audio_int.h |    8 --------
 qemu-common.h     |   16 ++++++++++++++++
 2 files changed, 16 insertions(+), 8 deletions(-)
audio/audio_int.h +0 -8
index 06e313f..d8560b6 100644
@@ -236,14 +236,6 @@ static inline int audio_ring_dist (int dst, int src, int len)
     return (dst >= src) ? (dst - src) : (len - src + dst);
 }
 
-#if defined __GNUC__
-#define GCC_ATTR __attribute__ ((__unused__, __format__ (__printf__, 1, 2)))
-#define GCC_FMT_ATTR(n, m) __attribute__ ((__format__ (__printf__, n, m)))
-#else
-#define GCC_ATTR /**/
-#define GCC_FMT_ATTR(n, m)
-#endif
-
 static void GCC_ATTR dolog (const char *fmt, ...)
 {
     va_list ap;
qemu-common.h +16 -0
index dfd3dc0..96be1d4 100644
@@ -70,6 +70,22 @@ struct iovec {
 #include <sys/uio.h>
 #endif
 
+#if defined __GNUC__
+# if (__GNUC__ < 4) || \
+     defined(__GNUC_MINOR__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 4)
+   /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */
+#  define GCC_ATTR __attribute__((__unused__, format(printf, 1, 2)))
+#  define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))
+# else
+   /* Use gnu_printf when supported (qemu uses standard format strings). */
+#  define GCC_ATTR __attribute__((__unused__, format(gnu_printf, 1, 2)))
+#  define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))
+# endif
+#else
+#define GCC_ATTR /**/
+#define GCC_FMT_ATTR(n, m)
+#endif
+
 #ifdef _WIN32
 #define fsync _commit
 #define lseek _lseeki64
Signature · 1 line
1.7.1
Applied
#9 lore raw

[Qemu-devel] Re: [PATCH] Move macros GCC_ATTR and GCC_FMT_ATTR to common header file

Thanks, applied.

Quoted 68 lines · Stefan Weil

By moving the definition of GCC_ATTR and GCC_FMT_ATTR from audio_int.h to qemu-common.h these macros are now generally available for further patches which add the gcc format attribute.

Newer gcc versions support format gnu_printf which is better suited for use in QEMU than format printf (QEMU always uses standard format strings (even with mingw32)).

V2: Use correct operator '==' (instead of '=')

---
 audio/audio_int.h |    8 -------- qemu-common.h     |   16 ++++++++++++++++

 2 files changed, 16 insertions(+), 8 deletions(-)
audio/audio_int.h +0 -8
index 06e313f..d8560b6 100644
@@ -236,14 +236,6 @@ static inline int audio_ring_dist (int dst, int src, int len)
     return (dst >= src) ? (dst - src) : (len - src + dst);
  }
 
-#if defined __GNUC__
-#define GCC_ATTR __attribute__ ((__unused__, __format__ (__printf__, 1, 2)))
-#define GCC_FMT_ATTR(n, m) __attribute__ ((__format__ (__printf__, n, m)))
-#else
-#define GCC_ATTR /**/
-#define GCC_FMT_ATTR(n, m)
-#endif
-
  static void GCC_ATTR dolog (const char *fmt, ...)
  {
     va_list ap;
qemu-common.h +16 -1
index dfd3dc0..96be1d4 100644
@@ -70,6 +70,22 @@ struct iovec {
  #include <sys/uio.h>
  #endif
 
+#if defined __GNUC__
+# if (__GNUC__ < 4) || \
+     defined(__GNUC_MINOR__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 4)
+   /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */
+#  define GCC_ATTR __attribute__((__unused__, format(printf, 1, 2)))
+#  define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))
+# else
+   /* Use gnu_printf when supported (qemu uses standard format strings). */
+#  define GCC_ATTR __attribute__((__unused__, format(gnu_printf, 1, 2)))
+#  define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))
+# endif
+#else
+#define GCC_ATTR /**/
+#define GCC_FMT_ATTR(n, m)
+#endif
+
  #ifdef _WIN32
  #define fsync _commit
  #define lseek _lseeki64
--

1.7.1

#10 lore raw

Re: [Qemu-devel] [PATCH 1/2] Add support for gcc format attribute gnu_printf

In reply to Blue Swirl

Am 20.09.2010 21:18, schrieb Blue Swirl:

Quoted 37 lines · Stefan Weil
Quoted 23 lines

Since version 4.4.x, gcc supports additional format attributes.

    __attribute__ ((format (gnu_printf, 1, 2)))
should be used instead of
    __attribute__ ((format (printf, 1, 2))

because QEMU always uses standard format strings (even with mingw32).

For older compilers, we simply define gnu_printf = printf, so they work with the new format attribute, too.

---
  configure |   11 +++++++++++
  1 files changed, 11 insertions(+), 0 deletions(-)
configure +0 -0
index 4061cb7..1300879 100755
@@ -148,6 +148,17 @@ for flag in $gcc_flags; do
     fi
  done
 

configure for flag in $gcc_flags; do · line 160

Quoted 10 lines
configure +9 -0
+# Check gnu_printf (supported by gcc>= 4.4.x).
+cat>  $TMPC<<  EOF
+static void test(const char *format, ...)
+    __attribute__ ((format (gnu_printf, 1, 2)));
+static void test(const char *format, ...) {}
+int main(void) { test("\n"); return 0; }
+EOF
+if ! compile_prog "-Werror" ""; then
+    QEMU_CFLAGS="-Dgnu_printf=printf $QEMU_CFLAGS"
 

This could cause problems, for example if system headers declared a function called gnu_printf.

Quoted 4 lines

I'd introduce CONFIG_GCC_FMT_ATTR_GNU_PRINTF and adjust GCC_FMT_ATTR definition based on that.

Thanks for your review. I'll send a new patch which goes into the direction which you suggested.

Instead of defining a new macro CONFIG_GCC_FMT_ATTR_GNU_PRINTF, my new patch checks the gcc version (gnu_printf was introduced with gcc 4.4). Like this, no changes to file configure are needed.

Regards
Stefan

#11 lore raw

PATCH 2/2 Use new gcc format attribute gnu_printf

In reply to Stefan Weil

Since version 4.4.x, gcc supports additional format attributes.

    __attribute__ ((format (gnu_printf, 1, 2)))
should be used instead of
    __attribute__ ((format (printf, 1, 2))

because QEMU always uses standard format strings (even with mingw32).

The patch replaces format attribute printf / __printf__ by gnu_printf.

It also removes an #ifdef __GNUC__ (not needed as long as we compile with gcc, and for non-gcc compilers we need more changes than this).

The gcc documentation uses format (not __format__), the majority in QEMU uses this shorter form, too. Therefore the patch also replaces __format__ by format.

Spacing was unified (again as in the gcc documentation).

---
 audio/audio.h      |    5 +----
 audio/audio_int.h  |    4 ++--
 bsd-user/qemu.h    |    2 +-
 cpu-all.h          |    2 +-
 darwin-user/qemu.h |    2 +-
 hw/xen_backend.h   |    2 +-
 linux-user/qemu.h  |    2 +-
 monitor.h          |    2 +-
 qemu-common.h      |    2 +-
 qemu-error.h       |    8 +++++---
 qerror.h           |    2 +-
 qjson.h            |    2 +-
 12 files changed, 17 insertions(+), 18 deletions(-)
audio/audio.h +1 -4
index 454ade2..4439b82 100644
@@ -88,10 +88,7 @@ typedef struct QEMUAudioTimeStamp {
 
 void AUD_vlog (const char *cap, const char *fmt, va_list ap);
 void AUD_log (const char *cap, const char *fmt, ...)
-#ifdef __GNUC__
-    __attribute__ ((__format__ (__printf__, 2, 3)))
-#endif
-    ;
+    __attribute__ ((format (gnu_printf, 2, 3)));
 
 void AUD_help (void);
 void AUD_register_card (const char *name, QEMUSoundCard *card);
audio/audio_int.h +2 -2
index 06e313f..f6a77ad 100644
@@ -237,8 +237,8 @@ static inline int audio_ring_dist (int dst, int src, int len)
 }
 
 #if defined __GNUC__
-#define GCC_ATTR __attribute__ ((__unused__, __format__ (__printf__, 1, 2)))
-#define GCC_FMT_ATTR(n, m) __attribute__ ((__format__ (__printf__, n, m)))
+#define GCC_ATTR __attribute__ ((__unused__, format (gnu_printf, 1, 2)))
+#define GCC_FMT_ATTR(n, m) __attribute__ ((format (gnu_printf, n, m)))
 #else
 #define GCC_ATTR /**/
 #define GCC_FMT_ATTR(n, m)
bsd-user/qemu.h +1 -1
index 554ff8b..34407aa 100644
@@ -139,7 +139,7 @@ abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
 abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
                             abi_long arg2, abi_long arg3, abi_long arg4,
                             abi_long arg5, abi_long arg6);
-void gemu_log(const char *fmt, ...) __attribute__((format(printf,1,2)));
+void gemu_log(const char *fmt, ...) __attribute__((format (gnu_printf, 1, 2)));
 extern THREAD CPUState *thread_env;
 void cpu_loop(CPUState *env);
 char *target_strerror(int err);
cpu-all.h +1 -1
index 67a3266..183bb40 100644
@@ -773,7 +773,7 @@ void cpu_dump_statistics (CPUState *env, FILE *f,
                           int flags);
 
 void QEMU_NORETURN cpu_abort(CPUState *env, const char *fmt, ...)
-    __attribute__ ((__format__ (__printf__, 2, 3)));
+    __attribute__ ((format (gnu_printf, 2, 3)));
 extern CPUState *first_cpu;
 extern CPUState *cpu_single_env;
 
darwin-user/qemu.h +1 -1
index 462bbda..2df2bc9 100644
@@ -99,7 +99,7 @@ int do_sigaction(int sig, const struct sigaction *act,
                  struct sigaction *oact);
 int do_sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss);
 
-void gemu_log(const char *fmt, ...) __attribute__((format(printf,1,2)));
+void gemu_log(const char *fmt, ...) __attribute__((format (gnu_printf, 1, 2)));
 void qerror(const char *fmt, ...);
 
 void write_dt(void *ptr, unsigned long addr, unsigned long limit, int flags);
hw/xen_backend.h +1 -1
index 292126d..5b78182 100644
@@ -84,7 +84,7 @@ int xen_be_bind_evtchn(struct XenDevice *xendev);
 void xen_be_unbind_evtchn(struct XenDevice *xendev);
 int xen_be_send_notify(struct XenDevice *xendev);
 void xen_be_printf(struct XenDevice *xendev, int msg_level, const char *fmt, ...)
-    __attribute__ ((format(printf, 3, 4)));
+    __attribute__ ((format (gnu_printf, 3, 4)));
 
 /* actual backend drivers */
 extern struct XenDevOps xen_console_ops;      /* xen_console.c     */
linux-user/qemu.h +1 -1
index 794fe49..779d892 100644
@@ -186,7 +186,7 @@ void syscall_init(void);
 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                     abi_long arg2, abi_long arg3, abi_long arg4,
                     abi_long arg5, abi_long arg6);
-void gemu_log(const char *fmt, ...) __attribute__((format(printf,1,2)));
+void gemu_log(const char *fmt, ...) __attribute__ ((format (gnu_printf, 1, 2)));
 extern THREAD CPUState *thread_env;
 void cpu_loop(CPUState *env);
 char *target_strerror(int err);
monitor.h +1 -1
index 38b22a4..d4c3872 100644
@@ -51,7 +51,7 @@ int monitor_get_fd(Monitor *mon, const char *fdname);
 
 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap);
 void monitor_printf(Monitor *mon, const char *fmt, ...)
-    __attribute__ ((__format__ (__printf__, 2, 3)));
+    __attribute__ ((format (gnu_printf, 2, 3)));
 void monitor_print_filename(Monitor *mon, const char *filename);
 void monitor_flush(Monitor *mon);
 
qemu-common.h +1 -1
index dfd3dc0..956b545 100644
@@ -181,7 +181,7 @@ int qemu_pipe(int pipefd[2]);
 /* Error handling.  */
 
 void QEMU_NORETURN hw_error(const char *fmt, ...)
-    __attribute__ ((__format__ (__printf__, 1, 2)));
+    __attribute__ ((format (gnu_printf, 1, 2)));
 
 /* IO callbacks.  */
 typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
qemu-error.h +5 -3
index a45609f..25f2bfb 100644
@@ -31,11 +31,13 @@ void loc_set_cmdline(char **argv, int idx, int cnt);
 void loc_set_file(const char *fname, int lno);
 
 void error_vprintf(const char *fmt, va_list ap);
-void error_printf(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
+void error_printf(const char *fmt, ...)
+    __attribute__ ((format (gnu_printf, 1, 2)));
 void error_printf_unless_qmp(const char *fmt, ...)
-    __attribute__ ((format(printf, 1, 2)));
+    __attribute__ ((format (gnu_printf, 1, 2)));
 void error_print_loc(void);
 void error_set_progname(const char *argv0);
-void error_report(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
+void error_report(const char *fmt, ...)
+    __attribute__ ((format (gnu_printf, 1, 2)));
 
 #endif
qerror.h +1 -1
index 62802ea..fbabe89 100644
@@ -39,7 +39,7 @@ QString *qerror_human(const QError *qerror);
 void qerror_print(QError *qerror);
 void qerror_report_internal(const char *file, int linenr, const char *func,
                             const char *fmt, ...)
-    __attribute__ ((format(printf, 4, 5)));
+    __attribute__ ((format (gnu_printf, 4, 5)));
 #define qerror_report(fmt, ...) \
     qerror_report_internal(__FILE__, __LINE__, __func__, fmt, ## __VA_ARGS__)
 QError *qobject_to_qerror(const QObject *obj);
qjson.h +1 -1
index 7afec2e..b21423a 100644
@@ -20,7 +20,7 @@
 
 QObject *qobject_from_json(const char *string);
 QObject *qobject_from_jsonf(const char *string, ...)
-    __attribute__((__format__ (__printf__, 1, 2)));
+    __attribute__((format (gnu_printf, 1, 2)));
 QObject *qobject_from_jsonv(const char *string, va_list *ap);
 
 QString *qobject_to_json(const QObject *obj);
Signature · 1 line
1.7.1
#12 lore raw

Re: [Qemu-devel] [PATCH 2/2] Use new gcc format attribute gnu_printf

Quoted 8 lines · Stefan Weil

Since version 4.4.x, gcc supports additional format attributes. __attribute__ ((format (gnu_printf, 1, 2)))
should be used instead of
   __attribute__ ((format (printf, 1, 2))
because QEMU always uses standard format strings (even with mingw32).

The patch replaces format attribute printf / __printf__ by gnu_printf.

Quoted 8 lines

It also removes an #ifdef __GNUC__ (not needed as long as we compile with gcc, and for non-gcc compilers we need more changes than this).

The gcc documentation uses format (not __format__), the majority in QEMU uses this shorter form, too. Therefore the patch also replaces __format__ by format.

Spacing was unified (again as in the gcc documentation).

Please avoid GNU style spacing. This is C, not LISP.

Quoted 37 lines

---
 audio/audio.h      |    5 +----
 audio/audio_int.h  |    4 ++--
 bsd-user/qemu.h    |    2 +-
 cpu-all.h          |    2 +-
 darwin-user/qemu.h |    2 +-
 hw/xen_backend.h   |    2 +-
 linux-user/qemu.h  |    2 +-
 monitor.h          |    2 +-
 qemu-common.h      |    2 +-
 qemu-error.h       |    8 +++++---
 qerror.h           |    2 +-
 qjson.h            |    2 +-

 12 files changed, 17 insertions(+), 18 deletions(-)
audio/audio.h +1 -4
index 454ade2..4439b82 100644
@@ -88,10 +88,7 @@ typedef struct QEMUAudioTimeStamp {
 
  void AUD_vlog (const char *cap, const char *fmt, va_list ap);
  void AUD_log (const char *cap, const char *fmt, ...)
-#ifdef __GNUC__
-    __attribute__ ((__format__ (__printf__, 2, 3)))
-#endif
-    ;
+    __attribute__ ((format (gnu_printf, 2, 3)));
 
  void AUD_help (void);
  void AUD_register_card (const char *name, QEMUSoundCard *card);
audio/audio_int.h +0 -0
index 06e313f..f6a77ad 100644

audio/audio_int.h static inline int audio_ring_dist (int dst, int src, int len) · line 241

Quoted 7 lines
audio/audio_int.h +2 -2
@@ -237,8 +237,8 @@ static inline int audio_ring_dist (int dst, int src, int len)
  }
 
  #if defined __GNUC__
-#define GCC_ATTR __attribute__ ((__unused__, __format__ (__printf__, 1, 2)))
-#define GCC_FMT_ATTR(n, m) __attribute__ ((__format__ (__printf__, n, m)))
+#define GCC_ATTR __attribute__ ((__unused__, format (gnu_printf, 1, 2)))
+#define GCC_FMT_ATTR(n, m) __attribute__ ((format (gnu_printf, n, m)))

I'd rather move this macro to a central header and converting instead users of other syntax. The macro could be defined differently depending on whether gnu_printf is supported or not. Then we would not need any -Dgnu_printf=printf in 1/2, which can cause problems.

#13 lore raw

Re: [Qemu-devel] [PATCH] mips_malta: Detect format errors for function prom_set

In reply to Stefan Weil
Quoted 1 line · Stefan Weil

Add the necessary gcc attribute and fix the detected errors.

This is identical to fulong patch, so my comments on that one apply also here.

Quoted 41 lines
Cc Aurelien Jarno <[email protected]>

---
 hw/mips_malta.c |    6 ++++--

 1 files changed, 4 insertions(+), 2 deletions(-)
hw/mips_malta.c +4 -3
index a458c97..deee273 100644
@@ -684,6 +684,8 @@ static void write_bootloader (CPUState *env, uint8_t *base,
  }
 
  static void prom_set(uint32_t* prom_buf, int index, const char *string, ...)
+    __attribute__ ((format (gnu_printf, 3, 4)));
+static void prom_set(uint32_t* prom_buf, int index, const char *string, ...)
  {
     va_list ap;
     int32_t table_addr;
@@ -750,13 +752,13 @@ static int64_t load_kernel(int big_endian)
     prom_size = ENVP_NB_ENTRIES * (sizeof(int32_t) + ENVP_ENTRY_SIZE);
     prom_buf = qemu_malloc(prom_size);
 
-    prom_set(prom_buf, prom_index++, loaderparams.kernel_filename);
+    prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_filename);
     if (initrd_size > 0) {
         prom_set(prom_buf, prom_index++, "rd_start=0x%" PRIx64 " rd_size=%li %s",
                  cpu_mips_phys_to_kseg0(NULL, initrd_offset), initrd_size,
                  loaderparams.kernel_cmdline);
     } else {
-        prom_set(prom_buf, prom_index++, loaderparams.kernel_cmdline);
+        prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_cmdline);
     }
 
     prom_set(prom_buf, prom_index++, "memsize");
--

1.7.0.4

#14 lore raw
In reply to Stefan Weil
Quoted 1 line · Stefan Weil

Add the necessary gcc attribute and fix the detected errors.

The fixes are correct, so those parts could be applied.

However the gnu_printf part will break with older compilers unless 1/2 is applied first. That part should also use GCC_FMT_ATTR macro. Please split the patch.

Quoted 42 lines
Cc Aurelien Jarno <[email protected]>

---
 hw/mips_fulong2e.c |    8 +++++---

 1 files changed, 5 insertions(+), 3 deletions(-)
hw/mips_fulong2e.c +5 -4
index cbe7156..2ef5070 100644
@@ -77,6 +77,8 @@ static struct _loaderparams {
  } loaderparams;
 
  static void prom_set(uint32_t* prom_buf, int index, const char *string, ...)
+    __attribute__ ((format (gnu_printf, 3, 4)));
+static void prom_set(uint32_t* prom_buf, int index, const char *string, ...)
  {
     va_list ap;
     int32_t table_addr;
@@ -141,13 +143,13 @@ static int64_t load_kernel (CPUState *env)
     prom_size = ENVP_NB_ENTRIES * (sizeof(int32_t) + ENVP_ENTRY_SIZE);
     prom_buf = qemu_malloc(prom_size);
 
-    prom_set(prom_buf, index++, loaderparams.kernel_filename);
+    prom_set(prom_buf, index++, "%s", loaderparams.kernel_filename);
     if (initrd_size > 0) {
-        prom_set(prom_buf, index++, "rd_start=0x" PRIx64 " rd_size=%li %s",
+        prom_set(prom_buf, index++, "rd_start=0x%" PRIx64 " rd_size=%li %s",
                  cpu_mips_phys_to_kseg0(NULL, initrd_offset), initrd_size,
                  loaderparams.kernel_cmdline);
     } else {
-        prom_set(prom_buf, index++, loaderparams.kernel_cmdline);
+        prom_set(prom_buf, index++, "%s", loaderparams.kernel_cmdline);
     }
 
     /* Setup minimum environment variables */
--

1.7.0.4

#15 lore raw

PATCH 1/2 mips_fulong2e: Fix format strings

Fix two compiler warnings (when format attribute is applied) and one error (missing %) in format strings.

Cc Aurelien Jarno <[email protected]>
Cc Blue Swirl <[email protected]>
---
 hw/mips_fulong2e.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
hw/mips_fulong2e.c +3 -3
index ac82067..61ca9c4 100644
@@ -141,13 +141,13 @@ static int64_t load_kernel (CPUState *env)
     prom_size = ENVP_NB_ENTRIES * (sizeof(int32_t) + ENVP_ENTRY_SIZE);
     prom_buf = qemu_malloc(prom_size);
 
-    prom_set(prom_buf, index++, loaderparams.kernel_filename);
+    prom_set(prom_buf, index++, "%s", loaderparams.kernel_filename);
     if (initrd_size > 0) {
-        prom_set(prom_buf, index++, "rd_start=0x" PRIx64 " rd_size=%li %s",
+        prom_set(prom_buf, index++, "rd_start=0x%" PRIx64 " rd_size=%li %s",
                  cpu_mips_phys_to_kseg0(NULL, initrd_offset), initrd_size,
                  loaderparams.kernel_cmdline);
     } else {
-        prom_set(prom_buf, index++, loaderparams.kernel_cmdline);
+        prom_set(prom_buf, index++, "%s", loaderparams.kernel_cmdline);
     }
 
     /* Setup minimum environment variables */
Signature · 1 line
1.7.1
Applied
#16 lore raw

[Qemu-devel] Re: [PATCH 1/2] mips_fulong2e: Fix format strings

Thanks, applied.

Quoted 35 lines · Stefan Weil

Fix two compiler warnings (when format attribute is applied) and one error (missing %) in format strings.

Cc Aurelien Jarno <[email protected]>
Cc Blue Swirl <[email protected]>

---
 hw/mips_fulong2e.c |    6 +++---

 1 files changed, 3 insertions(+), 3 deletions(-)
hw/mips_fulong2e.c +3 -4
index ac82067..61ca9c4 100644
@@ -141,13 +141,13 @@ static int64_t load_kernel (CPUState *env)
     prom_size = ENVP_NB_ENTRIES * (sizeof(int32_t) + ENVP_ENTRY_SIZE);
     prom_buf = qemu_malloc(prom_size);
 
-    prom_set(prom_buf, index++, loaderparams.kernel_filename);
+    prom_set(prom_buf, index++, "%s", loaderparams.kernel_filename);
     if (initrd_size > 0) {
-        prom_set(prom_buf, index++, "rd_start=0x" PRIx64 " rd_size=%li %s",
+        prom_set(prom_buf, index++, "rd_start=0x%" PRIx64 " rd_size=%li %s",
                  cpu_mips_phys_to_kseg0(NULL, initrd_offset), initrd_size,
                  loaderparams.kernel_cmdline);
     } else {
-        prom_set(prom_buf, index++, loaderparams.kernel_cmdline);
+        prom_set(prom_buf, index++, "%s", loaderparams.kernel_cmdline);
     }
 
     /* Setup minimum environment variables */
--

1.7.1

#17 lore raw

PATCH 2/2 mips_malta: Fix format strings

In reply to Blue Swirl

Fix two compiler warnings (when format attribute is applied).

Cc Aurelien Jarno <[email protected]>
Cc Blue Swirl <[email protected]>
---
 hw/mips_malta.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
hw/mips_malta.c +2 -2
index ec95cd8..1cb7880 100644
@@ -728,13 +728,13 @@ static int64_t load_kernel (void)
     prom_size = ENVP_NB_ENTRIES * (sizeof(int32_t) + ENVP_ENTRY_SIZE);
     prom_buf = qemu_malloc(prom_size);
 
-    prom_set(prom_buf, prom_index++, loaderparams.kernel_filename);
+    prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_filename);
     if (initrd_size > 0) {
         prom_set(prom_buf, prom_index++, "rd_start=0x%" PRIx64 " rd_size=%li %s",
                  cpu_mips_phys_to_kseg0(NULL, initrd_offset), initrd_size,
                  loaderparams.kernel_cmdline);
     } else {
-        prom_set(prom_buf, prom_index++, loaderparams.kernel_cmdline);
+        prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_cmdline);
     }
 
     prom_set(prom_buf, prom_index++, "memsize");
Signature · 1 line
1.7.1
Applied
#18 lore raw

[Qemu-devel] Re: [PATCH 2/2] mips_malta: Fix format strings

Thanks, applied.

Quoted 33 lines · Stefan Weil

Fix two compiler warnings (when format attribute is applied).

Cc Aurelien Jarno <[email protected]>
Cc Blue Swirl <[email protected]>

---
 hw/mips_malta.c |    4 ++--

 1 files changed, 2 insertions(+), 2 deletions(-)
hw/mips_malta.c +2 -3
index ec95cd8..1cb7880 100644
@@ -728,13 +728,13 @@ static int64_t load_kernel (void)
     prom_size = ENVP_NB_ENTRIES * (sizeof(int32_t) + ENVP_ENTRY_SIZE);
     prom_buf = qemu_malloc(prom_size);
 
-    prom_set(prom_buf, prom_index++, loaderparams.kernel_filename);
+    prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_filename);
     if (initrd_size > 0) {
         prom_set(prom_buf, prom_index++, "rd_start=0x%" PRIx64 " rd_size=%li %s",
                  cpu_mips_phys_to_kseg0(NULL, initrd_offset), initrd_size,
                  loaderparams.kernel_cmdline);
     } else {
-        prom_set(prom_buf, prom_index++, loaderparams.kernel_cmdline);
+        prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_cmdline);
     }
 
     prom_set(prom_buf, prom_index++, "memsize");
--

1.7.1