Skip to contentSkip to conversation

Fix some GCC 9 build warnings

PATCH Applied Alistair Francis 8 participants 25 messages Apr 30, 2019 – May 2, 2019

lore
Share & tools
lore
#0 lore raw

v2 cover Fix some GCC 9 build warnings

v2:
- Rewrite most patches based on the feedback from v1

Alistair Francis (5):
util/qemu-sockets: Fix GCC 9 build warnings
hw/usb/hcd-xhci: Fix GCC 9 build warning
hw/usb/dev-mtp: Fix GCC 9 build warning
linux-user/uname: Fix GCC 9 build warnings
linux-user/elfload: Fix GCC 9 build warnings

 hw/usb/dev-mtp.c     | 13 +++++++++++++
 hw/usb/hcd-xhci.c    |  1 +
 linux-user/elfload.c |  2 +-
 linux-user/uname.c   |  2 +-
 util/qemu-sockets.c  |  4 ++--
 5 files changed, 18 insertions(+), 4 deletions(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH v2 1/5 util/qemu-sockets: Fix GCC 9 build warnings

Fix this warning when building with GCC9 on Fedora 30:
In function ‘strncpy’,
inlined from ‘unix_connect_saddr.isra.0’ at util/qemu-sockets.c:925:5:
/usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ specified bound 108 equals destination size [-Werror=stringop-truncation]

  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In function ‘strncpy’,
inlined from ‘unix_listen_saddr.isra.0’ at util/qemu-sockets.c:880:5:

---
 util/qemu-sockets.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
util/qemu-sockets.c +2 -2
index 9705051690..8c3322958f 100644
@@ -829,7 +829,7 @@ static int unix_listen_saddr(UnixSocketAddress *saddr,
     struct sockaddr_un un;
     int sock, fd;
     char *pathbuf = NULL;
-    const char *path;
+    const char *path QEMU_NONSTRING;
 
     sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
     if (sock < 0) {
@@ -922,7 +922,7 @@ static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp)
 
     memset(&un, 0, sizeof(un));
     un.sun_family = AF_UNIX;
-    strncpy(un.sun_path, saddr->path, sizeof(un.sun_path));
+    memcpy(un.sun_path, saddr->path, MIN(strlen(saddr->path), sizeof(un.sun_path)));
 
     /* connect to peer */
     do {
Signature · 1 line
2.21.0
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/5] util/qemu-sockets: Fix GCC 9 build warnings

Le 01/05/2019 à 01:28, Alistair Francis a écrit :

Quoted 19 lines

Fix this warning when building with GCC9 on Fedora 30:
In function ‘strncpy’,
inlined from ‘unix_connect_saddr.isra.0’ at util/qemu-sockets.c:925:5:
/usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ specified bound 108 equals destination size [-Werror=stringop-truncation]

  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In function ‘strncpy’,
inlined from ‘unix_listen_saddr.isra.0’ at util/qemu-sockets.c:880:5:

---
 util/qemu-sockets.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
util/qemu-sockets.c +0 -0
index 9705051690..8c3322958f 100644

util/qemu-sockets.c static int unix_listen_saddr(UnixSocketAddress *saddr, · line 832

Quoted 5 lines
util/qemu-sockets.c +1 -1
@@ -829,7 +829,7 @@ static int unix_listen_saddr(UnixSocketAddress *saddr,
     struct sockaddr_un un;
     int sock, fd;
     char *pathbuf = NULL;
-    const char *path;
+    const char *path QEMU_NONSTRING;

Do we need this with memcpy()?

Thanks,
Laurent

#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/5] util/qemu-sockets: Fix GCC 9 build warnings

In reply to Alistair Francis
Quoted 28 lines · Alistair Francis

Fix this warning when building with GCC9 on Fedora 30:
In function ‘strncpy’,
inlined from ‘unix_connect_saddr.isra.0’ at util/qemu-sockets.c:925:5:
/usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ specified bound 108 equals destination size [-Werror=stringop-truncation]

  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In function ‘strncpy’,
inlined from ‘unix_listen_saddr.isra.0’ at util/qemu-sockets.c:880:5:

---
 util/qemu-sockets.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
util/qemu-sockets.c +1 -1
index 9705051690..8c3322958f 100644
@@ -829,7 +829,7 @@ static int unix_listen_saddr(UnixSocketAddress *saddr,
     struct sockaddr_un un;
     int sock, fd;
     char *pathbuf = NULL;
-    const char *path;
+    const char *path QEMU_NONSTRING;
 
     sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
     if (sock < 0) {

util/qemu-sockets.c static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp) · line 928

Quoted 8 lines
util/qemu-sockets.c +1 -1
@@ -922,7 +922,7 @@ static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp)
 
     memset(&un, 0, sizeof(un));
     un.sun_family = AF_UNIX;
-    strncpy(un.sun_path, saddr->path, sizeof(un.sun_path));
+    memcpy(un.sun_path, saddr->path, MIN(strlen(saddr->path), sizeof(un.sun_path)));
 
     /* connect to peer */
     do {

I think my proposed fix for this file is preferrable as it avoids repeated strlen calls

https://lists.gnu.org/archive/html/qemu-devel/2019-04/msg02124.html

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 :|
#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/5] util/qemu-sockets: Fix GCC 9 build warnings

Quoted 36 lines · Daniel P. Berrangé
Quoted 34 lines · Alistair Francis

Fix this warning when building with GCC9 on Fedora 30:
In function ‘strncpy’,
inlined from ‘unix_connect_saddr.isra.0’ at util/qemu-sockets.c:925:5:
/usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ specified bound 108 equals destination size [-Werror=stringop-truncation]

  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In function ‘strncpy’,
inlined from ‘unix_listen_saddr.isra.0’ at util/qemu-sockets.c:880:5:

---
 util/qemu-sockets.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
util/qemu-sockets.c +2 -2
index 9705051690..8c3322958f 100644
@@ -829,7 +829,7 @@ static int unix_listen_saddr(UnixSocketAddress *saddr,
     struct sockaddr_un un;
     int sock, fd;
     char *pathbuf = NULL;
-    const char *path;
+    const char *path QEMU_NONSTRING;
 
     sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
     if (sock < 0) {
@@ -922,7 +922,7 @@ static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp)
 
     memset(&un, 0, sizeof(un));
     un.sun_family = AF_UNIX;
-    strncpy(un.sun_path, saddr->path, sizeof(un.sun_path));
+    memcpy(un.sun_path, saddr->path, MIN(strlen(saddr->path), sizeof(un.sun_path)));
 
Quoted 7 lines

util/qemu-sockets.c static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp) · line 928

Quoted 2 lines
util/qemu-sockets.c +0 -0
     /* connect to peer */
     do {

I think my proposed fix for this file is preferrable as it avoids repeated strlen calls

https://lists.gnu.org/archive/html/qemu-devel/2019-04/msg02124.html

That's fine with me, I have dropped this patch.

Alistair

Quoted 8 lines

Regards,
Daniel
--

|: 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 :|
#5 lore raw

PATCH v2 2/5 hw/usb/hcd-xhci: Fix GCC 9 build warning

In reply to Alistair Francis

Fix this build warning with GCC 9 on Fedora 30:
hw/usb/hcd-xhci.c:3339:66: error: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 5 [-Werror=format-truncation=]
3339 | snprintf(port->name, sizeof(port->name), "usb2 port #%d", i+1);
| ^~
hw/usb/hcd-xhci.c:3339:54: note: directive argument in the range [1, 2147483647]
3339 | snprintf(port->name, sizeof(port->name), "usb2 port #%d", i+1);
| ^~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:867,

                 from /home/alistair/qemu/include/qemu/osdep.h:99,
                 from hw/usb/hcd-xhci.c:21:
/usr/include/bits/stdio2.h:67:10: note: ‘__builtin___snprintf_chk’ output between 13 and 22 bytes into a destination of size 16
   67 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   68 |        __bos (__s), __fmt, __va_arg_pack ());
      |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 hw/usb/hcd-xhci.c | 1 +
 1 file changed, 1 insertion(+)
hw/usb/hcd-xhci.c +1 -0
index ec28bee319..2b061772b2 100644
@@ -3322,6 +3322,7 @@ static void usb_xhci_init(XHCIState *xhci)
     usb_bus_new(&xhci->bus, sizeof(xhci->bus), &xhci_bus_ops, dev);
 
     for (i = 0; i < usbports; i++) {
+        g_assert(i < MAX(MAXPORTS_2, MAXPORTS_3));
         speedmask = 0;
         if (i < xhci->numports_2) {
             if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) {
Signature · 1 line
2.21.0
Reviewed-by on v2 2/5
#6 lore raw

Re: [Qemu-devel] [PATCH v2 2/5] hw/usb/hcd-xhci: Fix GCC 9 build warning

Le 01/05/2019 à 01:28, Alistair Francis a écrit :

Quoted 27 lines

Fix this build warning with GCC 9 on Fedora 30:
hw/usb/hcd-xhci.c:3339:66: error: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 5 [-Werror=format-truncation=]
3339 | snprintf(port->name, sizeof(port->name), "usb2 port #%d", i+1);
| ^~
hw/usb/hcd-xhci.c:3339:54: note: directive argument in the range [1, 2147483647]
3339 | snprintf(port->name, sizeof(port->name), "usb2 port #%d", i+1);
| ^~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:867,

                 from /home/alistair/qemu/include/qemu/osdep.h:99,
                 from hw/usb/hcd-xhci.c:21:
/usr/include/bits/stdio2.h:67:10: note: ‘__builtin___snprintf_chk’ output between 13 and 22 bytes into a destination of size 16
   67 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   68 |        __bos (__s), __fmt, __va_arg_pack ());
      |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 hw/usb/hcd-xhci.c | 1 +
 1 file changed, 1 insertion(+)
hw/usb/hcd-xhci.c +0 -0
index ec28bee319..2b061772b2 100644

hw/usb/hcd-xhci.c static void usb_xhci_init(XHCIState *xhci) · line 3328

Quoted 7 lines
hw/usb/hcd-xhci.c +1 -0
@@ -3322,6 +3322,7 @@ static void usb_xhci_init(XHCIState *xhci)
     usb_bus_new(&xhci->bus, sizeof(xhci->bus), &xhci_bus_ops, dev);
 
     for (i = 0; i < usbports; i++) {
+        g_assert(i < MAX(MAXPORTS_2, MAXPORTS_3));
         speedmask = 0;
         if (i < xhci->numports_2) {
             if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) {
Reviewed-by on v2 2/5
#7 lore raw

Re: [Qemu-devel] [PATCH v2 2/5] hw/usb/hcd-xhci: Fix GCC 9 build warning

In reply to Alistair Francis
Quoted 26 lines · Alistair Francis

Fix this build warning with GCC 9 on Fedora 30:
hw/usb/hcd-xhci.c:3339:66: error: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 5 [-Werror=format-truncation=]
3339 | snprintf(port->name, sizeof(port->name), "usb2 port #%d", i+1);
| ^~
hw/usb/hcd-xhci.c:3339:54: note: directive argument in the range [1, 2147483647]
3339 | snprintf(port->name, sizeof(port->name), "usb2 port #%d", i+1);
| ^~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:867,

                 from /home/alistair/qemu/include/qemu/osdep.h:99,
                 from hw/usb/hcd-xhci.c:21:
/usr/include/bits/stdio2.h:67:10: note: ‘__builtin___snprintf_chk’ output between 13 and 22 bytes into a destination of size 16
   67 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   68 |        __bos (__s), __fmt, __va_arg_pack ());
      |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 hw/usb/hcd-xhci.c | 1 +
 1 file changed, 1 insertion(+)
hw/usb/hcd-xhci.c +0 -0
index ec28bee319..2b061772b2 100644

hw/usb/hcd-xhci.c static void usb_xhci_init(XHCIState *xhci) · line 3328

Quoted 7 lines
hw/usb/hcd-xhci.c +1 -0
@@ -3322,6 +3322,7 @@ static void usb_xhci_init(XHCIState *xhci)
     usb_bus_new(&xhci->bus, sizeof(xhci->bus), &xhci_bus_ops, dev);
 
     for (i = 0; i < usbports; i++) {
+        g_assert(i < MAX(MAXPORTS_2, MAXPORTS_3));
         speedmask = 0;
         if (i < xhci->numports_2) {
             if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) {

I proposed a slightly different fix here:

https://lists.gnu.org/archive/html/qemu-devel/2019-04/msg02125.html

but both have the same effect

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 :|
#8 lore raw

Re: [Qemu-devel] [PATCH v2 2/5] hw/usb/hcd-xhci: Fix GCC 9 build warning

In reply to Alistair Francis
Quoted 2 lines · Alistair Francis
     for (i = 0; i < usbports; i++) {

+ g_assert(i < MAX(MAXPORTS_2, MAXPORTS_3));

I would hope that it works to move this out of the loop:

  g_assert(usbports <= MAX(MAXPORTS_2, MAXPORTS_3));

r~

#9 lore raw

Re: [Qemu-devel] [PATCH v2 2/5] hw/usb/hcd-xhci: Fix GCC 9 build warning

Quoted 7 lines · Richard Henderson
Quoted 2 lines · Alistair Francis
     for (i = 0; i < usbports; i++) {

+ g_assert(i < MAX(MAXPORTS_2, MAXPORTS_3));

I would hope that it works to move this out of the loop:

  g_assert(usbports <= MAX(MAXPORTS_2, MAXPORTS_3));

With Richard suggestion:

#10 lore raw

Re: [Qemu-devel] [PATCH v2 2/5] hw/usb/hcd-xhci: Fix GCC 9 build warning

In reply to Richard Henderson
Quoted 8 lines · Richard Henderson
Quoted 2 lines · Alistair Francis
     for (i = 0; i < usbports; i++) {

+ g_assert(i < MAX(MAXPORTS_2, MAXPORTS_3));

I would hope that it works to move this out of the loop:

  g_assert(usbports <= MAX(MAXPORTS_2, MAXPORTS_3));

Yes, that also works. I have updated the patch.

Alistair

Quoted 3 lines

r~

#11 lore raw

PATCH v2 3/5 hw/usb/dev-mtp: Fix GCC 9 build warning

In reply to Alistair Francis

Fix this warning with GCC 9 on Fedora 30:
hw/usb/dev-mtp.c:1715:36: error: taking address of packed member of ‘struct <anonymous>’ may result in an unaligned pointer value [-Werror=address-of-packed-member]

 1715 |                             dataset->filename);
      |                             ~~~~~~~^~~~~~~~~~
---
 hw/usb/dev-mtp.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
hw/usb/dev-mtp.c +13 -0
index 99548b012d..8233beacab 100644
@@ -1711,9 +1711,22 @@ static void usb_mtp_write_metadata(MTPState *s, uint64_t dlen)
     assert(!s->write_pending);
     assert(p != NULL);
 
+/*
+ * We are about to access a packed struct. We are confident that the pointer
+ * address won't be unaligned, so we ignore GCC warnings.
+ */
+#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
+#endif
+
     filename = utf16_to_str(MIN(dataset->length, filename_chars),
                             dataset->filename);
 
+#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
+#pragma GCC diagnostic pop
+#endif
+
     if (strchr(filename, '/')) {
         usb_mtp_queue_result(s, RES_PARAMETER_NOT_SUPPORTED, d->trans,
                              0, 0, 0, 0);
Signature · 1 line
2.21.0
#12 lore raw

Re: [Qemu-devel] [PATCH v2 3/5] hw/usb/dev-mtp: Fix GCC 9 build warning

Quoted 23 lines · Alistair Francis

Fix this warning with GCC 9 on Fedora 30:
hw/usb/dev-mtp.c:1715:36: error: taking address of packed member of ‘struct <anonymous>’ may result in an unaligned pointer value [-Werror=address-of-packed-member]

 1715 |                             dataset->filename);
      |                             ~~~~~~~^~~~~~~~~~
---
 hw/usb/dev-mtp.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
hw/usb/dev-mtp.c +5 -0
index 99548b012d..8233beacab 100644
@@ -1711,9 +1711,22 @@ static void usb_mtp_write_metadata(MTPState *s, uint64_t dlen)
     assert(!s->write_pending);
     assert(p != NULL);
 
+/*
+ * We are about to access a packed struct. We are confident that the pointer
+ * address won't be unaligned, so we ignore GCC warnings.
+ */
+#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)

hw/usb/dev-mtp.c static void usb_mtp_write_metadata(MTPState *s, uint64_t dlen) · line 1728

Quoted 10 lines
hw/usb/dev-mtp.c +7 -0
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
+#endif
+
     filename = utf16_to_str(MIN(dataset->length, filename_chars),
                             dataset->filename);
 
+#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
+#pragma GCC diagnostic pop
+#endif

Would it be possible to use an assert() instead? Something like

g_assert((dataset->filename & 1) == 0)

?

Thomas

#13 lore raw

Re: [Qemu-devel] [PATCH v2 3/5] hw/usb/dev-mtp: Fix GCC 9 build warning

In reply to Alistair Francis
Quoted 15 lines · Alistair Francis

Fix this warning with GCC 9 on Fedora 30:
hw/usb/dev-mtp.c:1715:36: error: taking address of packed member of ‘struct <anonymous>’ may result in an unaligned pointer value [-Werror=address-of-packed-member]

 1715 |                             dataset->filename);
      |                             ~~~~~~~^~~~~~~~~~
---
 hw/usb/dev-mtp.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
hw/usb/dev-mtp.c +0 -0
index 99548b012d..8233beacab 100644

hw/usb/dev-mtp.c static void usb_mtp_write_metadata(MTPState *s, uint64_t dlen) · line 1717

Quoted 7 lines
hw/usb/dev-mtp.c +4 -0
@@ -1711,9 +1711,22 @@ static void usb_mtp_write_metadata(MTPState *s, uint64_t dlen)
     assert(!s->write_pending);
     assert(p != NULL);
 
+/*
+ * We are about to access a packed struct. We are confident that the pointer
+ * address won't be unaligned, so we ignore GCC warnings.
+ */

The data is mis-aligned as we're accessing an int16 array that is immediately following an int8 field in a packed struct

This problem is fixed by the following series which Gerd has in the USB queue:

https://lists.gnu.org/archive/html/qemu-devel/2019-04/msg02524.html

Quoted 5 lines
hw/usb/dev-mtp.c +5 -0
+#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
+#endif
+

hw/usb/dev-mtp.c static void usb_mtp_write_metadata(MTPState *s, uint64_t dlen) · line 1732

Quoted 10 lines
hw/usb/dev-mtp.c +4 -0
     filename = utf16_to_str(MIN(dataset->length, filename_chars),
                             dataset->filename);
 
+#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
+#pragma GCC diagnostic pop
+#endif
+
     if (strchr(filename, '/')) {
         usb_mtp_queue_result(s, RES_PARAMETER_NOT_SUPPORTED, d->trans,
                              0, 0, 0, 0);

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 :|
#14 lore raw

Re: [Qemu-devel] [PATCH v2 3/5] hw/usb/dev-mtp: Fix GCC 9 build warning

Quoted 28 lines · Daniel P. Berrangé
Quoted 15 lines · Alistair Francis

Fix this warning with GCC 9 on Fedora 30:
hw/usb/dev-mtp.c:1715:36: error: taking address of packed member of ‘struct <anonymous>’ may result in an unaligned pointer value [-Werror=address-of-packed-member]

 1715 |                             dataset->filename);
      |                             ~~~~~~~^~~~~~~~~~
---
 hw/usb/dev-mtp.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
hw/usb/dev-mtp.c +0 -0
index 99548b012d..8233beacab 100644

hw/usb/dev-mtp.c static void usb_mtp_write_metadata(MTPState *s, uint64_t dlen) · line 1717

Quoted 7 lines
hw/usb/dev-mtp.c +4 -0
@@ -1711,9 +1711,22 @@ static void usb_mtp_write_metadata(MTPState *s, uint64_t dlen)
     assert(!s->write_pending);
     assert(p != NULL);
 
+/*
+ * We are about to access a packed struct. We are confident that the pointer
+ * address won't be unaligned, so we ignore GCC warnings.
+ */

The data is mis-aligned as we're accessing an int16 array that is immediately following an int8 field in a packed struct

Quoted 4 lines

This problem is fixed by the following series which Gerd has in the USB queue:

https://lists.gnu.org/archive/html/qemu-devel/2019-04/msg02524.html

Great, that fixes the build issues. Dropping this patch.

Alistair

Quoted 23 lines
Quoted 6 lines
hw/usb/dev-mtp.c +5 -0
+#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
+#endif
+

hw/usb/dev-mtp.c static void usb_mtp_write_metadata(MTPState *s, uint64_t dlen) · line 1732

Quoted 10 lines
hw/usb/dev-mtp.c +4 -0
     filename = utf16_to_str(MIN(dataset->length, filename_chars),
                             dataset->filename);
 
+#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
+#pragma GCC diagnostic pop
+#endif
+
     if (strchr(filename, '/')) {
         usb_mtp_queue_result(s, RES_PARAMETER_NOT_SUPPORTED, d->trans,
                              0, 0, 0, 0);

Regards,
Daniel
--

|: 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 :|
#15 lore raw

PATCH v2 4/5 linux-user/uname: Fix GCC 9 build warnings

In reply to Alistair Francis

Fix this warning when building with GCC9 on Fedora 30:
In function ‘strncpy’,
inlined from ‘sys_uname’ at /home/alistair/qemu/linux-user/uname.c:94:3:
/usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ output may be truncated copying 64 bytes from a string of length 64 [-Werror=stringop-truncation]

  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 linux-user/uname.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/uname.c +1 -1
index 313b79dbad..2fc6096a5b 100644
@@ -73,7 +73,7 @@ const char *cpu_to_uname_machine(void *cpu_env)
 #define COPY_UTSNAME_FIELD(dest, src) \
   do { \
       /* __NEW_UTS_LEN doesn't include terminating null */ \
-      (void) strncpy((dest), (src), __NEW_UTS_LEN); \
+      (void) memcpy((dest), (src), MIN(strlen(src), __NEW_UTS_LEN)); \
       (dest)[__NEW_UTS_LEN] = '\0'; \
   } while (0)
 
Signature · 1 line
2.21.0
#16 lore raw

Re: [Qemu-devel] [PATCH v2 4/5] linux-user/uname: Fix GCC 9 build warnings

Quoted 17 lines · Alistair Francis

Fix this warning when building with GCC9 on Fedora 30:
In function ‘strncpy’,
inlined from ‘sys_uname’ at /home/alistair/qemu/linux-user/uname.c:94:3:
/usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ output may be truncated copying 64 bytes from a string of length 64 [-Werror=stringop-truncation]

  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 linux-user/uname.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/uname.c +0 -0
index 313b79dbad..2fc6096a5b 100644

linux-user/uname.c const char *cpu_to_uname_machine(void *cpu_env) · line 76

Quoted 5 lines
linux-user/uname.c +1 -1
@@ -73,7 +73,7 @@ const char *cpu_to_uname_machine(void *cpu_env)
 #define COPY_UTSNAME_FIELD(dest, src) \
   do { \
       /* __NEW_UTS_LEN doesn't include terminating null */ \
-      (void) strncpy((dest), (src), __NEW_UTS_LEN); \
+      (void) memcpy((dest), (src), MIN(strlen(src), __NEW_UTS_LEN)); \

You should use MIN(strlen(src) + 1, __NEW_UTS_LEN) to copy the NUL character if it is present and fit in __NEW_UTS_LEN.

Quoted 4 lines
linux-user/uname.c +0 -0
       (dest)[__NEW_UTS_LEN] = '\0'; \
   } while (0)
 
#17 lore raw

Re: [Qemu-devel] [PATCH v2 4/5] linux-user/uname: Fix GCC 9 build warnings

Quoted 13 lines · Laurent Vivier
Quoted 12 lines · Alistair Francis

Fix this warning when building with GCC9 on Fedora 30:
In function ‘strncpy’,
inlined from ‘sys_uname’ at /home/alistair/qemu/linux-user/uname.c:94:3:
/usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ output may be truncated copying 64 bytes from a string of length 64 [-Werror=stringop-truncation]

  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 linux-user/uname.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

linux-user/uname.c const char *cpu_to_uname_machine(void *cpu_env) · line 76

Quoted 10 lines
linux-user/uname.c +1 -1
index 313b79dbad..2fc6096a5b 100644
@@ -73,7 +73,7 @@ const char *cpu_to_uname_machine(void *cpu_env)
 #define COPY_UTSNAME_FIELD(dest, src) \
   do { \
       /* __NEW_UTS_LEN doesn't include terminating null */ \
-      (void) strncpy((dest), (src), __NEW_UTS_LEN); \
+      (void) memcpy((dest), (src), MIN(strlen(src), __NEW_UTS_LEN)); \

You should use MIN(strlen(src) + 1, __NEW_UTS_LEN) to copy the NUL character if it is present and fit in __NEW_UTS_LEN.

IMHO we shouldn't use strlen at all. I proposed fixing it using sizeof() instead here:

https://lists.gnu.org/archive/html/qemu-devel/2019-04/msg02154.html

Quoted 3 lines

linux-user/uname.c const char *cpu_to_uname_machine(void *cpu_env) · line 78

Quoted 3 lines
linux-user/uname.c +0 -0
       (dest)[__NEW_UTS_LEN] = '\0'; \
   } while (0)

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 :|
#18 lore raw

Re: [Qemu-devel] [PATCH v2 4/5] linux-user/uname: Fix GCC 9 build warnings

Quoted 28 lines · Daniel P. Berrangé
Quoted 13 lines · Laurent Vivier
Quoted 12 lines · Alistair Francis

Fix this warning when building with GCC9 on Fedora 30:
In function ‘strncpy’,
inlined from ‘sys_uname’ at /home/alistair/qemu/linux-user/uname.c:94:3:
/usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ output may be truncated copying 64 bytes from a string of length 64 [-Werror=stringop-truncation]

  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 linux-user/uname.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

linux-user/uname.c const char *cpu_to_uname_machine(void *cpu_env) · line 76

Quoted 10 lines
linux-user/uname.c +1 -1
index 313b79dbad..2fc6096a5b 100644
@@ -73,7 +73,7 @@ const char *cpu_to_uname_machine(void *cpu_env)
 #define COPY_UTSNAME_FIELD(dest, src) \
   do { \
       /* __NEW_UTS_LEN doesn't include terminating null */ \
-      (void) strncpy((dest), (src), __NEW_UTS_LEN); \
+      (void) memcpy((dest), (src), MIN(strlen(src), __NEW_UTS_LEN)); \

You should use MIN(strlen(src) + 1, __NEW_UTS_LEN) to copy the NUL character if it is present and fit in __NEW_UTS_LEN.

Quoted 5 lines

IMHO we shouldn't use strlen at all. I proposed fixing it using sizeof() instead here:

https://lists.gnu.org/archive/html/qemu-devel/2019-04/msg02154.html

Yes, it's better.

Thanks,
Laurent

#19 lore raw

Re: [Qemu-devel] [PATCH v2 4/5] linux-user/uname: Fix GCC 9 build warnings

In reply to Laurent Vivier
Quoted 13 lines · Laurent Vivier
Quoted 12 lines · Alistair Francis

Fix this warning when building with GCC9 on Fedora 30:
In function ‘strncpy’,
inlined from ‘sys_uname’ at /home/alistair/qemu/linux-user/uname.c:94:3:
/usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ output may be truncated copying 64 bytes from a string of length 64 [-Werror=stringop-truncation]

  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 linux-user/uname.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

linux-user/uname.c const char *cpu_to_uname_machine(void *cpu_env) · line 76

Quoted 10 lines
linux-user/uname.c +1 -1
index 313b79dbad..2fc6096a5b 100644
@@ -73,7 +73,7 @@ const char *cpu_to_uname_machine(void *cpu_env)
 #define COPY_UTSNAME_FIELD(dest, src) \
   do { \
       /* __NEW_UTS_LEN doesn't include terminating null */ \
-      (void) strncpy((dest), (src), __NEW_UTS_LEN); \
+      (void) memcpy((dest), (src), MIN(strlen(src), __NEW_UTS_LEN)); \

You should use MIN(strlen(src) + 1, __NEW_UTS_LEN) to copy the NUL character if it is present and fit in __NEW_UTS_LEN.

No, the NUL character is already present, due to the memset() prior to any use of COPY_UTSNAME_FIELD(). However, the commit message should call that out, as it is not part of the default 3-line diff.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#20 lore raw

Re: [Qemu-devel] [PATCH v2 4/5] linux-user/uname: Fix GCC 9 build warnings

Quoted 11 lines · Eric Blake
Quoted 9 lines · Laurent Vivier
Quoted 8 lines · Alistair Francis

Fix this warning when building with GCC9 on Fedora 30:
In function ‘strncpy’,
inlined from ‘sys_uname’ at /home/alistair/qemu/linux-user/uname.c:94:3:
/usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ output may be truncated copying 64 bytes from a string of length 64 [-Werror=stringop-truncation]

  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I'm dropping this patch in favour of the other one.

Alistair

Quoted 27 lines
Quoted 4 lines
Quoted 4 lines
---
 linux-user/uname.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

linux-user/uname.c const char *cpu_to_uname_machine(void *cpu_env) · line 76

Quoted 10 lines
linux-user/uname.c +1 -1
index 313b79dbad..2fc6096a5b 100644
@@ -73,7 +73,7 @@ const char *cpu_to_uname_machine(void *cpu_env)
 #define COPY_UTSNAME_FIELD(dest, src) \
   do { \
       /* __NEW_UTS_LEN doesn't include terminating null */ \
-      (void) strncpy((dest), (src), __NEW_UTS_LEN); \
+      (void) memcpy((dest), (src), MIN(strlen(src), __NEW_UTS_LEN)); \

You should use MIN(strlen(src) + 1, __NEW_UTS_LEN) to copy the NUL character if it is present and fit in __NEW_UTS_LEN.

No, the NUL character is already present, due to the memset() prior to any use of COPY_UTSNAME_FIELD(). However, the commit message should call that out, as it is not part of the default 3-line diff.

--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization: qemu.org | libvirt.org

#21 lore raw

PATCH v2 5/5 linux-user/elfload: Fix GCC 9 build warnings

In reply to Alistair Francis

Fix this warning when building with GCC9 on Fedora 30:
In function ‘strncpy’,
inlined from ‘fill_psinfo’ at /home/alistair/qemu/linux-user/elfload.c:3208:12,
inlined from ‘fill_note_info’ at /home/alistair/qemu/linux-user/elfload.c:3390:5,
inlined from ‘elf_core_dump’ at /home/alistair/qemu/linux-user/elfload.c:3539:9:
/usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ specified bound 16 equals destination size [-Werror=stringop-truncation]

  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 linux-user/elfload.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/elfload.c +1 -1
index c1a26021f8..d08fe23466 100644
@@ -2872,7 +2872,7 @@ struct target_elf_prpsinfo {
     target_gid_t pr_gid;
     target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
     /* Lots missing */
-    char    pr_fname[16];           /* filename of executable */
+    char    pr_fname[16] QEMU_NONSTRING; /* filename of executable */
     char    pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
 };
 
Signature · 1 line
2.21.0
Reviewed-by on v2 5/5
#22 lore raw

Re: [Qemu-devel] [PATCH v2 5/5] linux-user/elfload: Fix GCC 9 build warnings

Quoted 20 lines · Alistair Francis

Fix this warning when building with GCC9 on Fedora 30:
In function ‘strncpy’,
inlined from ‘fill_psinfo’ at /home/alistair/qemu/linux-user/elfload.c:3208:12,
inlined from ‘fill_note_info’ at /home/alistair/qemu/linux-user/elfload.c:3390:5,
inlined from ‘elf_core_dump’ at /home/alistair/qemu/linux-user/elfload.c:3539:9:
/usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ specified bound 16 equals destination size [-Werror=stringop-truncation]

  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 linux-user/elfload.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/elfload.c +0 -0
index c1a26021f8..d08fe23466 100644

linux-user/elfload.c struct target_elf_prpsinfo { · line 2878

Quoted 8 lines
linux-user/elfload.c +1 -1
@@ -2872,7 +2872,7 @@ struct target_elf_prpsinfo {
     target_gid_t pr_gid;
     target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
     /* Lots missing */
-    char    pr_fname[16];           /* filename of executable */
+    char    pr_fname[16] QEMU_NONSTRING; /* filename of executable */
     char    pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
 };
 
Reviewed-by on v2 5/5
#23 lore raw

Re: [Qemu-devel] [PATCH v2 5/5] linux-user/elfload: Fix GCC 9 build warnings

In reply to Alistair Francis

linux-user/elfload.c struct target_elf_prpsinfo { · line 2875

Quoted 6 lines · Alistair Francis
linux-user/elfload.c +1 -1
@@ -2872,7 +2872,7 @@ struct target_elf_prpsinfo {
     target_gid_t pr_gid;
     target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
     /* Lots missing */
-    char    pr_fname[16];           /* filename of executable */
+    char    pr_fname[16] QEMU_NONSTRING; /* filename of executable */

r~

#24 lore raw

Re: [Qemu-devel] [PATCH v2 5/5] linux-user/elfload: Fix GCC 9 build warnings

In reply to Alistair Francis
Quoted 20 lines · Alistair Francis

Fix this warning when building with GCC9 on Fedora 30:
In function ‘strncpy’,
inlined from ‘fill_psinfo’ at /home/alistair/qemu/linux-user/elfload.c:3208:12,
inlined from ‘fill_note_info’ at /home/alistair/qemu/linux-user/elfload.c:3390:5,
inlined from ‘elf_core_dump’ at /home/alistair/qemu/linux-user/elfload.c:3539:9:
/usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ specified bound 16 equals destination size [-Werror=stringop-truncation]

  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 linux-user/elfload.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/elfload.c +0 -0
index c1a26021f8..d08fe23466 100644

linux-user/elfload.c struct target_elf_prpsinfo { · line 2878

Quoted 8 lines
linux-user/elfload.c +1 -1
@@ -2872,7 +2872,7 @@ struct target_elf_prpsinfo {
     target_gid_t pr_gid;
     target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
     /* Lots missing */
-    char    pr_fname[16];           /* filename of executable */
+    char    pr_fname[16] QEMU_NONSTRING; /* filename of executable */
     char    pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
 };
 

Applied to my linux-user branch.

Thanks,
Laurent