Skip to contentSkip to conversation

Do not use %m in common code to print error messages

PATCH Applied Thomas Huth 6 participants 9 messages Oct 18, 2019 – Oct 21, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Do not use %m in common code to print error messages

The %m format specifier is an extension from glibc - and when compiling QEMU for NetBSD, the compiler correctly complains, e.g.:

/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c: In function 'sigfd_handler':
/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c:64:13: warning: %m is only
allowed in syslog(3) like functions [-Wformat=]

             printf("read from sigfd returned %zd: %m\n", len);
             ^

Let's use g_strerror() here instead, which is an easy-to-use wrapper around the thread-safe strerror_r() function.

While we're at it, also convert the "printf()" in main-loop.c into the preferred "error_report()".

---
v2: Do not try to g_free() the strings

 hw/misc/tmp421.c | 4 ++--
 util/main-loop.c | 3 ++-
 util/systemd.c   | 4 ++--
 3 files changed, 6 insertions(+), 5 deletions(-)
hw/misc/tmp421.c +2 -2
index 9f044705fa..c0bc150bca 100644
@@ -120,7 +120,7 @@ static void tmp421_get_temperature(Object *obj, Visitor *v, const char *name,
     int tempid;
 
     if (sscanf(name, "temperature%d", &tempid) != 1) {
-        error_setg(errp, "error reading %s: %m", name);
+        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
         return;
     }
 
@@ -160,7 +160,7 @@ static void tmp421_set_temperature(Object *obj, Visitor *v, const char *name,
     }
 
     if (sscanf(name, "temperature%d", &tempid) != 1) {
-        error_setg(errp, "error reading %s: %m", name);
+        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
         return;
     }
 
util/main-loop.c +2 -1
index e3eaa55866..eda63fe4e0 100644
@@ -61,7 +61,8 @@ static void sigfd_handler(void *opaque)
         }
 
         if (len != sizeof(info)) {
-            printf("read from sigfd returned %zd: %m\n", len);
+            error_report("read from sigfd returned %zd: %s", len,
+                         g_strerror(errno));
             return;
         }
 
util/systemd.c +2 -2
index d22e86c707..1dd0367d9a 100644
@@ -60,8 +60,8 @@ unsigned int check_socket_activation(void)
              * and we should exit.
              */
             error_report("Socket activation failed: "
-                         "invalid file descriptor fd = %d: %m",
-                         fd);
+                         "invalid file descriptor fd = %d: %s",
+                         fd, g_strerror(errno));
             exit(EXIT_FAILURE);
         }
     }
Signature · 1 line
2.18.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 14 lines · Thomas Huth

The %m format specifier is an extension from glibc - and when compiling QEMU for NetBSD, the compiler correctly complains, e.g.:

/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c: In function 'sigfd_handler':
/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c:64:13: warning: %m is only
allowed in syslog(3) like functions [-Wformat=]

             printf("read from sigfd returned %zd: %m\n", len);
             ^

Let's use g_strerror() here instead, which is an easy-to-use wrapper around the thread-safe strerror_r() function.

While we're at it, also convert the "printf()" in main-loop.c into the preferred "error_report()".

Quoted 8 lines

---
v2: Do not try to g_free() the strings

 hw/misc/tmp421.c | 4 ++--
 util/main-loop.c | 3 ++-
 util/systemd.c   | 4 ++--
 3 files changed, 6 insertions(+), 5 deletions(-)

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by on v2 1/1
#2 lore raw
In reply to Thomas Huth
Quoted 14 lines · Thomas Huth

The %m format specifier is an extension from glibc - and when compiling QEMU for NetBSD, the compiler correctly complains, e.g.:

/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c: In function 'sigfd_handler':
/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c:64:13: warning: %m is only
allowed in syslog(3) like functions [-Wformat=]

             printf("read from sigfd returned %zd: %m\n", len);
             ^

Let's use g_strerror() here instead, which is an easy-to-use wrapper around the thread-safe strerror_r() function.

While we're at it, also convert the "printf()" in main-loop.c into the preferred "error_report()".

Quoted 8 lines

---
v2: Do not try to g_free() the strings

 hw/misc/tmp421.c | 4 ++--
 util/main-loop.c | 3 ++-
 util/systemd.c   | 4 ++--
 3 files changed, 6 insertions(+), 5 deletions(-)

There are many uses of %m also in hw/vfio/ but that's Linux stuff. Should we change those too or it doesn't matter since it never really compiled on NetBSD?

Anyway, this patch LGTM:

Thanks,
Stefano

Quoted 56 lines
hw/misc/tmp421.c +2 -2
index 9f044705fa..c0bc150bca 100644
@@ -120,7 +120,7 @@ static void tmp421_get_temperature(Object *obj, Visitor *v, const char *name,
     int tempid;
 
     if (sscanf(name, "temperature%d", &tempid) != 1) {
-        error_setg(errp, "error reading %s: %m", name);
+        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
         return;
     }
 
@@ -160,7 +160,7 @@ static void tmp421_set_temperature(Object *obj, Visitor *v, const char *name,
     }
 
     if (sscanf(name, "temperature%d", &tempid) != 1) {
-        error_setg(errp, "error reading %s: %m", name);
+        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
         return;
     }
 
util/main-loop.c +2 -1
index e3eaa55866..eda63fe4e0 100644
@@ -61,7 +61,8 @@ static void sigfd_handler(void *opaque)
         }
 
         if (len != sizeof(info)) {
-            printf("read from sigfd returned %zd: %m\n", len);
+            error_report("read from sigfd returned %zd: %s", len,
+                         g_strerror(errno));
             return;
         }
 
util/systemd.c +2 -2
index d22e86c707..1dd0367d9a 100644
@@ -60,8 +60,8 @@ unsigned int check_socket_activation(void)
              * and we should exit.
              */
             error_report("Socket activation failed: "
-                         "invalid file descriptor fd = %d: %m",
-                         fd);
+                         "invalid file descriptor fd = %d: %s",
+                         fd, g_strerror(errno));
             exit(EXIT_FAILURE);
         }
     }
Signature · 1 line
2.18.1
#3 lore raw
Quoted 19 lines · Stefano Garzarella
Quoted 18 lines · Thomas Huth

The %m format specifier is an extension from glibc - and when compiling QEMU for NetBSD, the compiler correctly complains, e.g.:

/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c: In function 'sigfd_handler':
/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c:64:13: warning: %m is only
allowed in syslog(3) like functions [-Wformat=]

             printf("read from sigfd returned %zd: %m\n", len);
             ^

Let's use g_strerror() here instead, which is an easy-to-use wrapper around the thread-safe strerror_r() function.

While we're at it, also convert the "printf()" in main-loop.c into the preferred "error_report()".

---
v2: Do not try to g_free() the strings

Quoted 9 lines
Quoted 4 lines
 hw/misc/tmp421.c | 4 ++--
 util/main-loop.c | 3 ++-
 util/systemd.c   | 4 ++--
 3 files changed, 6 insertions(+), 5 deletions(-)

There are many uses of %m also in hw/vfio/ but that's Linux stuff. Should we change those too or it doesn't matter since it never really compiled on NetBSD?

It's a gnu (glibc) extension and linux can use alternative libc implementations. Probably most of them capable to host qemu use %m.

Quoted 63 lines

Anyway, this patch LGTM:

Thanks,
Stefano

Quoted 56 lines
hw/misc/tmp421.c +2 -2
index 9f044705fa..c0bc150bca 100644
@@ -120,7 +120,7 @@ static void tmp421_get_temperature(Object *obj, Visitor *v, const char *name,
     int tempid;
 
     if (sscanf(name, "temperature%d", &tempid) != 1) {
-        error_setg(errp, "error reading %s: %m", name);
+        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
         return;
     }
 
@@ -160,7 +160,7 @@ static void tmp421_set_temperature(Object *obj, Visitor *v, const char *name,
     }
 
     if (sscanf(name, "temperature%d", &tempid) != 1) {
-        error_setg(errp, "error reading %s: %m", name);
+        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
         return;
     }
 
util/main-loop.c +2 -1
index e3eaa55866..eda63fe4e0 100644
@@ -61,7 +61,8 @@ static void sigfd_handler(void *opaque)
         }
 
         if (len != sizeof(info)) {
-            printf("read from sigfd returned %zd: %m\n", len);
+            error_report("read from sigfd returned %zd: %s", len,
+                         g_strerror(errno));
             return;
         }
 
util/systemd.c +2 -3
index d22e86c707..1dd0367d9a 100644
@@ -60,8 +60,8 @@ unsigned int check_socket_activation(void)
              * and we should exit.
              */
             error_report("Socket activation failed: "
-                         "invalid file descriptor fd = %d: %m",
-                         fd);
+                         "invalid file descriptor fd = %d: %s",
+                         fd, g_strerror(errno));
             exit(EXIT_FAILURE);
         }
     }
--

2.18.1

#4 lore raw
Quoted 25 lines · Kamil Rytarowski
Quoted 24 lines · Stefano Garzarella
Quoted 22 lines · Thomas Huth

The %m format specifier is an extension from glibc - and when compiling QEMU for NetBSD, the compiler correctly complains, e.g.:

/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c: In function 'sigfd_handler':
/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c:64:13: warning: %m is only
allowed in syslog(3) like functions [-Wformat=]

             printf("read from sigfd returned %zd: %m\n", len);
             ^

Let's use g_strerror() here instead, which is an easy-to-use wrapper around the thread-safe strerror_r() function.

While we're at it, also convert the "printf()" in main-loop.c into the preferred "error_report()".

---
v2: Do not try to g_free() the strings

 hw/misc/tmp421.c | 4 ++--
 util/main-loop.c | 3 ++-
 util/systemd.c   | 4 ++--
 3 files changed, 6 insertions(+), 5 deletions(-)
Quoted 6 lines
Quoted 3 lines

There are many uses of %m also in hw/vfio/ but that's Linux stuff. Should we change those too or it doesn't matter since it never really compiled on NetBSD?

It's a gnu (glibc) extension and linux can use alternative libc implementations. Probably most of them capable to host qemu use %m.

I think I read somewhere that other libcs on Linux also support %m (like musl), but I just can't find that reference anymore. Anyway, we can still fix that later in case someone hits the issue.

Quoted 2 lines
Quoted 2 lines

Anyway, this patch LGTM:

Thanks,
Thomas

#5 lore raw
Quoted 26 lines · Thomas Huth
Quoted 25 lines · Kamil Rytarowski
Quoted 24 lines · Stefano Garzarella
Quoted 22 lines · Thomas Huth

The %m format specifier is an extension from glibc - and when compiling QEMU for NetBSD, the compiler correctly complains, e.g.:

/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c: In function 'sigfd_handler':
/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c:64:13: warning: %m is only
allowed in syslog(3) like functions [-Wformat=]

             printf("read from sigfd returned %zd: %m\n", len);
             ^

Let's use g_strerror() here instead, which is an easy-to-use wrapper around the thread-safe strerror_r() function.

While we're at it, also convert the "printf()" in main-loop.c into the preferred "error_report()".

---
v2: Do not try to g_free() the strings

 hw/misc/tmp421.c | 4 ++--
 util/main-loop.c | 3 ++-
 util/systemd.c   | 4 ++--
 3 files changed, 6 insertions(+), 5 deletions(-)
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

There are many uses of %m also in hw/vfio/ but that's Linux stuff. Should we change those too or it doesn't matter since it never really compiled on NetBSD?

It's a gnu (glibc) extension and linux can use alternative libc implementations. Probably most of them capable to host qemu use %m.

I think I read somewhere that other libcs on Linux also support %m (like

Good to know!

Quoted 3 lines

musl), but I just can't find that reference anymore. Anyway, we can still fix that later in case someone hits the issue.

Yes, make sense to me.

Thanks,
Stefano

#6 lore raw
In reply to Thomas Huth
Quoted 65 lines · Thomas Huth

The %m format specifier is an extension from glibc - and when compiling QEMU for NetBSD, the compiler correctly complains, e.g.:

/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c: In function 'sigfd_handler':
/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c:64:13: warning: %m is only
allowed in syslog(3) like functions [-Wformat=]

             printf("read from sigfd returned %zd: %m\n", len);
             ^

Let's use g_strerror() here instead, which is an easy-to-use wrapper around the thread-safe strerror_r() function.

While we're at it, also convert the "printf()" in main-loop.c into the preferred "error_report()".

---
v2: Do not try to g_free() the strings

 hw/misc/tmp421.c | 4 ++--
 util/main-loop.c | 3 ++-
 util/systemd.c   | 4 ++--
 3 files changed, 6 insertions(+), 5 deletions(-)
hw/misc/tmp421.c +2 -2
index 9f044705fa..c0bc150bca 100644
@@ -120,7 +120,7 @@ static void tmp421_get_temperature(Object *obj, Visitor *v, const char *name,
     int tempid;
 
     if (sscanf(name, "temperature%d", &tempid) != 1) {
-        error_setg(errp, "error reading %s: %m", name);
+        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
         return;
     }
 
@@ -160,7 +160,7 @@ static void tmp421_set_temperature(Object *obj, Visitor *v, const char *name,
     }
 
     if (sscanf(name, "temperature%d", &tempid) != 1) {
-        error_setg(errp, "error reading %s: %m", name);
+        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
         return;
     }
 
util/main-loop.c +2 -1
index e3eaa55866..eda63fe4e0 100644
@@ -61,7 +61,8 @@ static void sigfd_handler(void *opaque)
         }
 
         if (len != sizeof(info)) {
-            printf("read from sigfd returned %zd: %m\n", len);
+            error_report("read from sigfd returned %zd: %s", len,
+                         g_strerror(errno));
             return;
         }
 
util/systemd.c +0 -0
index d22e86c707..1dd0367d9a 100644

util/systemd.c unsigned int check_socket_activation(void) · line 67

Quoted 10 lines
util/systemd.c +2 -2
@@ -60,8 +60,8 @@ unsigned int check_socket_activation(void)
              * and we should exit.
              */
             error_report("Socket activation failed: "
-                         "invalid file descriptor fd = %d: %m",
-                         fd);
+                         "invalid file descriptor fd = %d: %s",
+                         fd, g_strerror(errno));
             exit(EXIT_FAILURE);
         }
     }

Queued, thanks.

Paolo

#7 lore raw
In reply to Thomas Huth

Le 18/10/2019 à 15:07, Thomas Huth a écrit :

Quoted 65 lines

The %m format specifier is an extension from glibc - and when compiling QEMU for NetBSD, the compiler correctly complains, e.g.:

/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c: In function 'sigfd_handler':
/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c:64:13: warning: %m is only
allowed in syslog(3) like functions [-Wformat=]

             printf("read from sigfd returned %zd: %m\n", len);
             ^

Let's use g_strerror() here instead, which is an easy-to-use wrapper around the thread-safe strerror_r() function.

While we're at it, also convert the "printf()" in main-loop.c into the preferred "error_report()".

---
v2: Do not try to g_free() the strings

 hw/misc/tmp421.c | 4 ++--
 util/main-loop.c | 3 ++-
 util/systemd.c   | 4 ++--
 3 files changed, 6 insertions(+), 5 deletions(-)
hw/misc/tmp421.c +2 -2
index 9f044705fa..c0bc150bca 100644
@@ -120,7 +120,7 @@ static void tmp421_get_temperature(Object *obj, Visitor *v, const char *name,
     int tempid;
 
     if (sscanf(name, "temperature%d", &tempid) != 1) {
-        error_setg(errp, "error reading %s: %m", name);
+        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
         return;
     }
 
@@ -160,7 +160,7 @@ static void tmp421_set_temperature(Object *obj, Visitor *v, const char *name,
     }
 
     if (sscanf(name, "temperature%d", &tempid) != 1) {
-        error_setg(errp, "error reading %s: %m", name);
+        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
         return;
     }
 
util/main-loop.c +2 -1
index e3eaa55866..eda63fe4e0 100644
@@ -61,7 +61,8 @@ static void sigfd_handler(void *opaque)
         }
 
         if (len != sizeof(info)) {
-            printf("read from sigfd returned %zd: %m\n", len);
+            error_report("read from sigfd returned %zd: %s", len,
+                         g_strerror(errno));
             return;
         }
 
util/systemd.c +0 -0
index d22e86c707..1dd0367d9a 100644

util/systemd.c unsigned int check_socket_activation(void) · line 67

Quoted 10 lines
util/systemd.c +2 -2
@@ -60,8 +60,8 @@ unsigned int check_socket_activation(void)
              * and we should exit.
              */
             error_report("Socket activation failed: "
-                         "invalid file descriptor fd = %d: %m",
-                         fd);
+                         "invalid file descriptor fd = %d: %s",
+                         fd, g_strerror(errno));
             exit(EXIT_FAILURE);
         }
     }

Applied to my trivial-patches branch.

Thanks,
Laurent

#8 lore raw

Le 21/10/2019 à 18:29, Laurent Vivier a écrit :

Quoted 70 lines

Le 18/10/2019 à 15:07, Thomas Huth a écrit :

Quoted 69 lines

The %m format specifier is an extension from glibc - and when compiling QEMU for NetBSD, the compiler correctly complains, e.g.:

/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c: In function 'sigfd_handler':
/home/qemu/qemu-test.ELjfrQ/src/util/main-loop.c:64:13: warning: %m is only
allowed in syslog(3) like functions [-Wformat=]

             printf("read from sigfd returned %zd: %m\n", len);
             ^

Let's use g_strerror() here instead, which is an easy-to-use wrapper around the thread-safe strerror_r() function.

While we're at it, also convert the "printf()" in main-loop.c into the preferred "error_report()".

---
v2: Do not try to g_free() the strings

 hw/misc/tmp421.c | 4 ++--
 util/main-loop.c | 3 ++-
 util/systemd.c   | 4 ++--
 3 files changed, 6 insertions(+), 5 deletions(-)
hw/misc/tmp421.c +2 -2
index 9f044705fa..c0bc150bca 100644
@@ -120,7 +120,7 @@ static void tmp421_get_temperature(Object *obj, Visitor *v, const char *name,
     int tempid;
 
     if (sscanf(name, "temperature%d", &tempid) != 1) {
-        error_setg(errp, "error reading %s: %m", name);
+        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
         return;
     }
 
@@ -160,7 +160,7 @@ static void tmp421_set_temperature(Object *obj, Visitor *v, const char *name,
     }
 
     if (sscanf(name, "temperature%d", &tempid) != 1) {
-        error_setg(errp, "error reading %s: %m", name);
+        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
         return;
     }
 
util/main-loop.c +2 -1
index e3eaa55866..eda63fe4e0 100644
@@ -61,7 +61,8 @@ static void sigfd_handler(void *opaque)
         }
 
         if (len != sizeof(info)) {
-            printf("read from sigfd returned %zd: %m\n", len);
+            error_report("read from sigfd returned %zd: %s", len,
+                         g_strerror(errno));
             return;
         }
 
util/systemd.c +0 -2
index d22e86c707..1dd0367d9a 100644
@@ -60,8 +60,8 @@ unsigned int check_socket_activation(void)
              * and we should exit.
              */
             error_report("Socket activation failed: "
-                         "invalid file descriptor fd = %d: %m",
-                         fd);
Quoted 8 lines

util/systemd.c unsigned int check_socket_activation(void) · line 67

Quoted 6 lines
util/systemd.c +2 -0
+                         "invalid file descriptor fd = %d: %s",
+                         fd, g_strerror(errno));
             exit(EXIT_FAILURE);
         }
     }

Applied to my trivial-patches branch.

As Paolo has already queued it, I remove it from my queue.

Thanks,
Laurent