qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 05/14] linux-user: avoid string truncation warnings


From: Daniel P . Berrangé
Subject: [Qemu-devel] [PATCH 05/14] linux-user: avoid string truncation warnings in elf field copying
Date: Fri, 29 Mar 2019 11:10:55 +0000

In file included from /usr/include/string.h:494,
                 from include/qemu/osdep.h:101,
                 from linux-user/elfload.c:2:
In function ‘strncpy’,
    inlined from ‘fill_psinfo’ at linux-user/elfload.c:3208:12,
    inlined from ‘fill_note_info’ at linux-user/elfload.c:3390:5,
    inlined from ‘elf_core_dump’ at linux-user/elfload.c:3539:9:
/usr/include/bits/string_fortified.h:106:10: warning: ‘__builtin_strncpy’ 
specified bound 16 equals destination size [-Wstringop-truncation]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We don't require the field to be NUL terminated, so can just
copy the lower of the string length and the target field size
using memcpy.

Signed-off-by: Daniel P. Berrangé <address@hidden>
---
 linux-user/elfload.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index c1a26021f8..caa060f7b7 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -3180,6 +3180,7 @@ static int fill_psinfo(struct target_elf_prpsinfo 
*psinfo, const TaskState *ts)
 {
     char *base_filename;
     unsigned int i, len;
+    size_t pathlen;
 
     (void) memset(psinfo, 0, sizeof (*psinfo));
 
@@ -3201,12 +3202,9 @@ static int fill_psinfo(struct target_elf_prpsinfo 
*psinfo, const TaskState *ts)
     psinfo->pr_gid = getgid();
 
     base_filename = g_path_get_basename(ts->bprm->filename);
-    /*
-     * Using strncpy here is fine: at max-length,
-     * this field is not NUL-terminated.
-     */
-    (void) strncpy(psinfo->pr_fname, base_filename,
-                   sizeof(psinfo->pr_fname));
+    pathlen = strlen(base_filename) + 1;
+    pathlen = MIN(pathlen, sizeof(psinfo->pr_fname));
+    memcpy(psinfo->pr_fname, base_filename, pathlen);
 
     g_free(base_filename);
     bswap_psinfo(psinfo);
-- 
2.20.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]