public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gcc-patches@gcc.gnu.org
Subject: RFA: libiberty: avoid UBSAN complaint in cplus-dem.c
Date: Fri, 27 Jul 2018 17:12:00 -0000	[thread overview]
Message-ID: <8736w4ppq6.fsf@tromey.com> (raw)

I built gdb with -fsanitize=undefined, and there was a complaint coming
from cplus-dem.c.  remember_Btype can call memcpy with a NULL pointer,
which is undefined behavior according to the C standard.

This patch fixes the problem for me.  I tested this by rebuilding gdb
(with -fsanitize=undefined) and re-running the test suite.

Ok?

Tom

2018-07-27  Tom Tromey  <tom@tromey.com>

	* cplus-dem.c (remember_Btype): Don't call memcpy with LEN==0.

diff --git a/libiberty/cplus-dem.c b/libiberty/cplus-dem.c
index 6d58bd899bf..4f29d54d089 100644
--- a/libiberty/cplus-dem.c
+++ b/libiberty/cplus-dem.c
@@ -4471,7 +4471,8 @@ remember_Btype (struct work_stuff *work, const char *start,
   char *tem;
 
   tem = XNEWVEC (char, len + 1);
-  memcpy (tem, start, len);
+  if (len > 0)
+    memcpy (tem, start, len);
   tem[len] = '\0';
   work -> btypevec[index] = tem;
 }

             reply	other threads:[~2018-07-27 17:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-27 17:12 Tom Tromey [this message]
2018-07-27 20:18 ` Ian Lance Taylor via gcc-patches

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=8736w4ppq6.fsf@tromey.com \
    --to=tom@tromey.com \
    --cc=gcc-patches@gcc.gnu.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).