Avoid stringop-truncation errors
authorTom Tromey <tromey@adacore.com>
Fri, 20 Mar 2020 13:30:13 +0000 (07:30 -0600)
committerTom Tromey <tromey@adacore.com>
Fri, 20 Mar 2020 14:31:17 +0000 (08:31 -0600)
I configured with -fsanitize=address and built gdb.  linux-tdep.c and
ada-tasks.c failed to build due to some stringop-truncation errors,
e.g.:

In function ‘char* strncpy(char*, const char*, size_t)’,
    inlined from ‘int linux_fill_prpsinfo(elf_internal_linux_prpsinfo*)’ at ../../binutils-gdb/gdb/linux-tdep.c:1742:11,
    inlined from ‘char* linux_make_corefile_notes(gdbarch*, bfd*, int*)’ at ../../binutils-gdb/gdb/linux-tdep.c:1878:27:
/usr/include/bits/string_fortified.h:106:34: error: ‘char* __builtin_strncpy(char*, const char*, long unsigned int)’ specified bound 81 equals destination size [-Werror=stringop-truncation]

This patch fixes the problem by using "sizeof - 1" in the call to
strndup, as recommended in the GCC manual.  This doesn't make a
difference here because the next line, in all cases, sets the final
element to '\0' anyway.

gdb/ChangeLog
2020-03-20  Tom Tromey  <tromey@adacore.com>

* ada-tasks.c (read_atcb): Use smaller length in strncpy call.
* linux-tdep.c (linux_fill_prpsinfo): Use smaller length in
strncpy call.

gdb/ChangeLog
gdb/ada-tasks.c
gdb/linux-tdep.c

index f1b007b1ba3de216837927cd71287e4345a367be..583ec9c81e32c9a5961c61796a9d4e8afa963889 100644 (file)
@@ -1,3 +1,9 @@
+2020-03-20  Tom Tromey  <tromey@adacore.com>
+
+       * ada-tasks.c (read_atcb): Use smaller length in strncpy call.
+       * linux-tdep.c (linux_fill_prpsinfo): Use smaller length in
+       strncpy call.
+
 2020-03-20  Tom Tromey  <tromey@adacore.com>
 
        * symmisc.c (maintenance_print_one_line_table): Use ui_out.
index 0a81c3c6922ee5479a54fa6abecd1bd3a8979566..589d5e84e0a0f60905c94cf5825b5e1f16358e9c 100644 (file)
@@ -679,7 +679,8 @@ read_atcb (CORE_ADDR task_id, struct ada_task_info *task_info)
                  task_name = p + 2;
 
              /* Copy the task name.  */
-             strncpy (task_info->name, task_name, sizeof (task_info->name));
+             strncpy (task_info->name, task_name,
+                      sizeof (task_info->name) - 1);
              task_info->name[sizeof (task_info->name) - 1] = 0;
            }
          else
index b6374ce399c72affcd29119a05755981559556b7..e50946ce379af94793436572889631484c537e93 100644 (file)
@@ -1729,7 +1729,7 @@ linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
 
   /* Copying the program name.  Only the basename matters.  */
   basename = lbasename (fname.get ());
-  strncpy (p->pr_fname, basename, sizeof (p->pr_fname));
+  strncpy (p->pr_fname, basename, sizeof (p->pr_fname) - 1);
   p->pr_fname[sizeof (p->pr_fname) - 1] = '\0';
 
   infargs = get_inferior_args ();
@@ -1739,7 +1739,7 @@ linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
   if (infargs != NULL)
     psargs = psargs + " " + infargs;
 
-  strncpy (p->pr_psargs, psargs.c_str (), sizeof (p->pr_psargs));
+  strncpy (p->pr_psargs, psargs.c_str (), sizeof (p->pr_psargs) - 1);
   p->pr_psargs[sizeof (p->pr_psargs) - 1] = '\0';
 
   xsnprintf (filename, sizeof (filename), "/proc/%d/stat", (int) pid);
This page took 0.028384 seconds and 4 git commands to generate.