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

Alistair Francis posted 5 patches 6 years, 6 months ago
There is a newer version of this series
[Qemu-devel] [PATCH v1 4/5] linux-user/uname: Fix GCC 9 build warnings
Posted by Alistair Francis 6 years, 6 months ago
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));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
---
 linux-user/uname.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/linux-user/uname.c b/linux-user/uname.c
index 313b79dbad..293b2238f2 100644
--- a/linux-user/uname.c
+++ b/linux-user/uname.c
@@ -90,6 +90,11 @@ int sys_uname(struct new_utsname *buf)
    * struct linux kernel uses).
    */
 
+#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wstringop-truncation"
+#endif
+
   memset(buf, 0, sizeof(*buf));
   COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
   COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
@@ -101,6 +106,9 @@ int sys_uname(struct new_utsname *buf)
 #endif
   return (0);
 
+#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
+#pragma GCC diagnostic pop
+#endif
 #undef COPY_UTSNAME_FIELD
 }
 
-- 
2.21.0

Re: [Qemu-devel] [PATCH v1 4/5] linux-user/uname: Fix GCC 9 build warnings
Posted by Laurent Vivier 6 years, 6 months ago
Le 30/04/2019 à 22:09, Alistair Francis a écrit :
> 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));
>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
> ---
>  linux-user/uname.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/linux-user/uname.c b/linux-user/uname.c
> index 313b79dbad..293b2238f2 100644
> --- a/linux-user/uname.c
> +++ b/linux-user/uname.c
> @@ -90,6 +90,11 @@ int sys_uname(struct new_utsname *buf)
>     * struct linux kernel uses).
>     */
>  
> +#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
> +#pragma GCC diagnostic push
> +#pragma GCC diagnostic ignored "-Wstringop-truncation"
> +#endif
> +
>    memset(buf, 0, sizeof(*buf));
>    COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
>    COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
> @@ -101,6 +106,9 @@ int sys_uname(struct new_utsname *buf)
>  #endif
>    return (0);
>  
> +#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
> +#pragma GCC diagnostic pop
> +#endif
>  #undef COPY_UTSNAME_FIELD
>  }
>  
> 

You should use PRAGMA_DISABLE_PACKED_WARNING and
PRAGMA_REENABLE_PACKED_WARNING from linux-user/qemu.h.

Thanks,
Laurent

Re: [Qemu-devel] [PATCH v1 4/5] linux-user/uname: Fix GCC 9 build warnings
Posted by Laurent Vivier 6 years, 6 months ago
Le 30/04/2019 à 22:30, Laurent Vivier a écrit :
> Le 30/04/2019 à 22:09, Alistair Francis a écrit :
>> 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));
>>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
>> ---
>>  linux-user/uname.c | 8 ++++++++
>>  1 file changed, 8 insertions(+)
>>
>> diff --git a/linux-user/uname.c b/linux-user/uname.c
>> index 313b79dbad..293b2238f2 100644
>> --- a/linux-user/uname.c
>> +++ b/linux-user/uname.c
>> @@ -90,6 +90,11 @@ int sys_uname(struct new_utsname *buf)
>>     * struct linux kernel uses).
>>     */
>>  
>> +#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
>> +#pragma GCC diagnostic push
>> +#pragma GCC diagnostic ignored "-Wstringop-truncation"
>> +#endif
>> +
>>    memset(buf, 0, sizeof(*buf));
>>    COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
>>    COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
>> @@ -101,6 +106,9 @@ int sys_uname(struct new_utsname *buf)
>>  #endif
>>    return (0);
>>  
>> +#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
>> +#pragma GCC diagnostic pop
>> +#endif
>>  #undef COPY_UTSNAME_FIELD
>>  }
>>  
>>
> 
> You should use PRAGMA_DISABLE_PACKED_WARNING and
> PRAGMA_REENABLE_PACKED_WARNING from linux-user/qemu.h.

Ok, I should read correctly, the pragma here is not related to them

Thanks,
Laurent


Re: [Qemu-devel] [PATCH v1 4/5] linux-user/uname: Fix GCC 9 build warnings
Posted by Eric Blake 6 years, 6 months ago
On 4/30/19 3:09 PM, Alistair Francis wrote:
> 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));
>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
> ---
>  linux-user/uname.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/linux-user/uname.c b/linux-user/uname.c
> index 313b79dbad..293b2238f2 100644
> --- a/linux-user/uname.c
> +++ b/linux-user/uname.c
> @@ -90,6 +90,11 @@ int sys_uname(struct new_utsname *buf)
>     * struct linux kernel uses).
>     */
>  
> +#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
> +#pragma GCC diagnostic push
> +#pragma GCC diagnostic ignored "-Wstringop-truncation"
> +#endif

Why do we need the pragma?

> +
>    memset(buf, 0, sizeof(*buf));

We are prezeroing the entire field, at which point...

>    COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
>    COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);

...using strncpy() for a shorter string is wasteful (we're writing the
tail end twice), and for a long string is warning-prone.  Why not
rewrite COPY_UTSNAME_FIELD() to use memcpy() for the MIN(strlen(src),
__NEW_UTS_LEN) and drop the write of the trailing NUL, since it will
already be NUL from your memset()?

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

Re: [Qemu-devel] [PATCH v1 4/5] linux-user/uname: Fix GCC 9 build warnings
Posted by Laurent Vivier 6 years, 6 months ago
Le 30/04/2019 à 22:40, Eric Blake a écrit :
> On 4/30/19 3:09 PM, Alistair Francis wrote:
>> 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));
>>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
>> ---
>>  linux-user/uname.c | 8 ++++++++
>>  1 file changed, 8 insertions(+)
>>
>> diff --git a/linux-user/uname.c b/linux-user/uname.c
>> index 313b79dbad..293b2238f2 100644
>> --- a/linux-user/uname.c
>> +++ b/linux-user/uname.c
>> @@ -90,6 +90,11 @@ int sys_uname(struct new_utsname *buf)
>>     * struct linux kernel uses).
>>     */
>>  
>> +#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
>> +#pragma GCC diagnostic push
>> +#pragma GCC diagnostic ignored "-Wstringop-truncation"
>> +#endif
> 
> Why do we need the pragma?
> 
>> +
>>    memset(buf, 0, sizeof(*buf));
> 
> We are prezeroing the entire field, at which point...
> 
>>    COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
>>    COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
> 
> ...using strncpy() for a shorter string is wasteful (we're writing the
> tail end twice), and for a long string is warning-prone.  Why not
> rewrite COPY_UTSNAME_FIELD() to use memcpy() for the MIN(strlen(src),
> __NEW_UTS_LEN) and drop the write of the trailing NUL, since it will
> already be NUL from your memset()?
> 

We must modify this very carefully because I think there is some magic
in COPY_UTSNAME_FIELD() we could miss.

#define COPY_UTSNAME_FIELD(dest, src) \
  do { \
      /* __NEW_UTS_LEN doesn't include terminating null */ \
      (void) strncpy((dest), (src), __NEW_UTS_LEN); \
      (dest)[__NEW_UTS_LEN] = '\0'; \
  } while (0)

Thanks,
Laurent

Re: [Qemu-devel] [PATCH v1 4/5] linux-user/uname: Fix GCC 9 build warnings
Posted by Alistair Francis 6 years, 6 months ago
On Tue, Apr 30, 2019 at 1:40 PM Eric Blake <eblake@redhat.com> wrote:
>
> On 4/30/19 3:09 PM, Alistair Francis wrote:
> > 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));
> >       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >
> > Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
> > ---
> >  linux-user/uname.c | 8 ++++++++
> >  1 file changed, 8 insertions(+)
> >
> > diff --git a/linux-user/uname.c b/linux-user/uname.c
> > index 313b79dbad..293b2238f2 100644
> > --- a/linux-user/uname.c
> > +++ b/linux-user/uname.c
> > @@ -90,6 +90,11 @@ int sys_uname(struct new_utsname *buf)
> >     * struct linux kernel uses).
> >     */
> >
> > +#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && QEMU_GNUC_PREREQ(9, 0)
> > +#pragma GCC diagnostic push
> > +#pragma GCC diagnostic ignored "-Wstringop-truncation"
> > +#endif
>
> Why do we need the pragma?
>
> > +
> >    memset(buf, 0, sizeof(*buf));
>
> We are prezeroing the entire field, at which point...
>
> >    COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
> >    COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
>
> ...using strncpy() for a shorter string is wasteful (we're writing the
> tail end twice), and for a long string is warning-prone.  Why not
> rewrite COPY_UTSNAME_FIELD() to use memcpy() for the MIN(strlen(src),
> __NEW_UTS_LEN) and drop the write of the trailing NUL, since it will
> already be NUL from your memset()?

I'm happy to do that, I'll change it to memcpy().

Alistair

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