RFA: libiberty: avoid UBSAN complaint in cplus-dem.c

Message ID 8736w4ppq6.fsf@tromey.com
State New
Headers show
Series
  • RFA: libiberty: avoid UBSAN complaint in cplus-dem.c
Related show

Commit Message

Tom Tromey July 27, 2018, 5:12 p.m.
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.

Comments

Jose E. Marchesi via Gcc-patches July 27, 2018, 8:18 p.m. | #1
On Fri, Jul 27, 2018 at 10:12 AM, Tom Tromey <tom@tromey.com> wrote:
> 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.


This is OK.

Thanks.

Ian

Patch

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;
 }