public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 3/3] Fix ubsan build
Date: Mon,  3 May 2021 13:32:06 -0600	[thread overview]
Message-ID: <20210503193206.4008066-4-tromey@adacore.com> (raw)
In-Reply-To: <20210503193206.4008066-1-tromey@adacore.com>

I tried a build using the undefined behavior sanitizer, and gcc gave
this error:

In file included from /usr/include/string.h:495,
                 from ../gnulib/import/string.h:41,
                 from ../../binutils-gdb/gdb/../gdbsupport/common-defs.h:95,
                 from ../../binutils-gdb/gdb/nat/linux-osdata.c:20:
In function ‘char* strncpy(char*, const char*, size_t)’,
    inlined from ‘void time_from_time_t(char*, int, TIME_T)’ at ../../binutils-gdb/gdb/nat/linux-osdata.c:923:15,
    inlined from ‘void time_from_time_t(char*, int, TIME_T)’ at ../../binutils-gdb/gdb/nat/linux-osdata.c:911:1,
    inlined from ‘void linux_xfer_osdata_sem(buffer*)’ at ../../binutils-gdb/gdb/nat/linux-osdata.c:1082:22:
/usr/include/bits/string_fortified.h:106:34: error: ‘char* __builtin_strncpy(char*, const char*, long unsigned int)’ specified bound 32 equals destination size [-Werror=stringop-truncation]

This patch fixes the problem by subtracting one from the length
parameter to strncpy.

I changed a couple of other similar functions -- gcc does not warn
about these, but I didn't see any substantial difference between the
different cases, and I think these are just latent warnings, to be
triggered in the future by a change to inlining heuristics.

gdb/ChangeLog
2021-05-03  Tom Tromey  <tromey@adacore.com>

	* nat/linux-osdata.c (user_from_uid, time_from_time_t)
	(group_from_gid): Subtract one from strncpy length.
---
 gdb/ChangeLog          | 5 +++++
 gdb/nat/linux-osdata.c | 6 +++---
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c
index 7034dd82376..12f66d3c981 100644
--- a/gdb/nat/linux-osdata.c
+++ b/gdb/nat/linux-osdata.c
@@ -212,7 +212,7 @@ user_from_uid (char *user, int maxlen, uid_t uid)
 
   if (pwentry)
     {
-      strncpy (user, pwentry->pw_name, maxlen);
+      strncpy (user, pwentry->pw_name, maxlen - 1);
       /* Ensure that the user name is null-terminated.  */
       user[maxlen - 1] = '\0';
     }
@@ -920,7 +920,7 @@ time_from_time_t (char *time, int maxlen, TIME_T seconds)
 	 characters long.  */
       char buf[30];
       const char *time_str = ctime_r (&t, buf);
-      strncpy (time, time_str, maxlen);
+      strncpy (time, time_str, maxlen - 1);
       time[maxlen - 1] = '\0';
     }
 }
@@ -935,7 +935,7 @@ group_from_gid (char *group, int maxlen, gid_t gid)
 
   if (grentry)
     {
-      strncpy (group, grentry->gr_name, maxlen);
+      strncpy (group, grentry->gr_name, maxlen - 1);
       /* Ensure that the group name is null-terminated.  */
       group[maxlen - 1] = '\0';
     }
-- 
2.26.3


  parent reply	other threads:[~2021-05-03 19:32 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-03 19:32 [PATCH 0/3] Fix some sanitizer errors Tom Tromey
2021-05-03 19:32 ` [PATCH 1/3] Add virtual destructor to dwarf2_per_cu_data Tom Tromey
2021-05-04 15:34   ` Tom Tromey
2021-05-06 17:23     ` Tom Tromey
2021-05-03 19:32 ` [PATCH 2/3] Fix buffer underflow in add_path Tom Tromey
2021-05-06 14:53   ` Tom de Vries
2021-05-03 19:32 ` Tom Tromey [this message]
2021-05-17 18:53 ` [PATCH 0/3] Fix some sanitizer errors Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210503193206.4008066-4-tromey@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).