[v2] linux-user: avoid string truncation warnings in uname field copying
diff mbox series

Message ID 20190501144646.4851-1-berrange@redhat.com
State New
Headers show
Series
  • [v2] linux-user: avoid string truncation warnings in uname field copying
Related show

Commit Message

Daniel P. Berrangé May 1, 2019, 2:46 p.m. UTC
In file included from /usr/include/string.h:494,
                 from include/qemu/osdep.h:101,
                 from linux-user/uname.c:20:
In function ‘strncpy’,
    inlined from ‘sys_uname’ at linux-user/uname.c:94:3:
/usr/include/bits/string_fortified.h:106:10: warning: ‘__builtin_strncpy’ output may be truncated copying 64 bytes from a string of length 64 [-Wstringop-truncation]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We don't care where the NUL terminator in the original uname
field was. It suffices to copy the entire original field and
simply force a NUL terminator at the end of the new field.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---

Changed in v2:

 - Always use sizeof() in preference to __NEW_UTS_LEN

 linux-user/uname.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Comments

no-reply@patchew.org May 1, 2019, 2:51 p.m. UTC | #1
Patchew URL: https://patchew.org/QEMU/20190501144646.4851-1-berrange@redhat.com/



Hi,

This series seems to have some coding style problems. See output below for
more information:

Type: series
Message-id: 20190501144646.4851-1-berrange@redhat.com
Subject: [Qemu-devel] [PATCH v2] linux-user: avoid string truncation warnings in uname field copying

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
 * [new tag]               patchew/20190501144646.4851-1-berrange@redhat.com -> patchew/20190501144646.4851-1-berrange@redhat.com
Switched to a new branch 'test'
c08b5ed306 linux-user: avoid string truncation warnings in uname field copying

=== OUTPUT BEGIN ===
ERROR: suspect code indent for conditional statements (2, 6)
#30: FILE: linux-user/uname.c:74:
   do { \
+      memcpy((dest), (src), MIN(sizeof(src), sizeof(dest))); \

total: 1 errors, 0 warnings, 11 lines checked

Commit c08b5ed30674 (linux-user: avoid string truncation warnings in uname field copying) has style problems, please review.  If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===

Test command exited with code: 1


The full log is available at
http://patchew.org/logs/20190501144646.4851-1-berrange@redhat.com/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to patchew-devel@redhat.com
Philippe Mathieu-Daudé May 1, 2019, 3:24 p.m. UTC | #2
On 5/1/19 4:46 PM, Daniel P. Berrangé wrote:
> In file included from /usr/include/string.h:494,
>                  from include/qemu/osdep.h:101,
>                  from linux-user/uname.c:20:
> In function ‘strncpy’,
>     inlined from ‘sys_uname’ at linux-user/uname.c:94:3:
> /usr/include/bits/string_fortified.h:106:10: warning: ‘__builtin_strncpy’ output may be truncated copying 64 bytes from a string of length 64 [-Wstringop-truncation]
>   106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> We don't care where the NUL terminator in the original uname
> field was. It suffices to copy the entire original field and
> simply force a NUL terminator at the end of the new field.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

> ---
> 
> Changed in v2:
> 
>  - Always use sizeof() in preference to __NEW_UTS_LEN
> 
>  linux-user/uname.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/linux-user/uname.c b/linux-user/uname.c
> index 313b79dbad..1c05f95387 100644
> --- a/linux-user/uname.c
> +++ b/linux-user/uname.c
> @@ -72,9 +72,8 @@ 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); \
> -      (dest)[__NEW_UTS_LEN] = '\0'; \
> +      memcpy((dest), (src), MIN(sizeof(src), sizeof(dest))); \
> +      (dest)[sizeof(dest) - 1] = '\0'; \
>    } while (0)
>  
>  int sys_uname(struct new_utsname *buf)
>
no-reply@patchew.org May 1, 2019, 3:28 p.m. UTC | #3
Patchew URL: https://patchew.org/QEMU/20190501144646.4851-1-berrange@redhat.com/



Hi,

This series seems to have some coding style problems. See output below for
more information:

Type: series
Message-id: 20190501144646.4851-1-berrange@redhat.com
Subject: [Qemu-devel] [PATCH v2] linux-user: avoid string truncation warnings in uname field copying

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
 t [tag update]            patchew/20190501144646.4851-1-berrange@redhat.com -> patchew/20190501144646.4851-1-berrange@redhat.com
Switched to a new branch 'test'
b97fa7be34 linux-user: avoid string truncation warnings in uname field copying

=== OUTPUT BEGIN ===
ERROR: suspect code indent for conditional statements (2, 6)
#31: FILE: linux-user/uname.c:74:
   do { \
+      memcpy((dest), (src), MIN(sizeof(src), sizeof(dest))); \

total: 1 errors, 0 warnings, 11 lines checked

Commit b97fa7be344e (linux-user: avoid string truncation warnings in uname field copying) has style problems, please review.  If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===

Test command exited with code: 1


The full log is available at
http://patchew.org/logs/20190501144646.4851-1-berrange@redhat.com/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to patchew-devel@redhat.com
Laurent Vivier May 2, 2019, 8:16 a.m. UTC | #4
On 01/05/2019 16:46, Daniel P. Berrangé wrote:
> In file included from /usr/include/string.h:494,
>                  from include/qemu/osdep.h:101,
>                  from linux-user/uname.c:20:
> In function ‘strncpy’,
>     inlined from ‘sys_uname’ at linux-user/uname.c:94:3:
> /usr/include/bits/string_fortified.h:106:10: warning: ‘__builtin_strncpy’ output may be truncated copying 64 bytes from a string of length 64 [-Wstringop-truncation]
>   106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> We don't care where the NUL terminator in the original uname
> field was. It suffices to copy the entire original field and
> simply force a NUL terminator at the end of the new field.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
> 
> Changed in v2:
> 
>  - Always use sizeof() in preference to __NEW_UTS_LEN
> 
>  linux-user/uname.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/linux-user/uname.c b/linux-user/uname.c
> index 313b79dbad..1c05f95387 100644
> --- a/linux-user/uname.c
> +++ b/linux-user/uname.c
> @@ -72,9 +72,8 @@ 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); \
> -      (dest)[__NEW_UTS_LEN] = '\0'; \
> +      memcpy((dest), (src), MIN(sizeof(src), sizeof(dest))); \
> +      (dest)[sizeof(dest) - 1] = '\0'; \
>    } while (0)
>  
>  int sys_uname(struct new_utsname *buf)
> 

Applied to my linux-user branch.

Thanks,
Laurent

Patch
diff mbox series

diff --git a/linux-user/uname.c b/linux-user/uname.c
index 313b79dbad..1c05f95387 100644
--- a/linux-user/uname.c
+++ b/linux-user/uname.c
@@ -72,9 +72,8 @@  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); \
-      (dest)[__NEW_UTS_LEN] = '\0'; \
+      memcpy((dest), (src), MIN(sizeof(src), sizeof(dest))); \
+      (dest)[sizeof(dest) - 1] = '\0'; \
   } while (0)
 
 int sys_uname(struct new_utsname *buf)