D: fix UBSAN

Message ID c2e46d42-ac94-6bdb-e0bc-662f2b089fb6@suse.cz
State New
Headers
Series D: fix UBSAN |

Commit Message

Martin Liška Dec. 6, 2021, 12:03 p.m. UTC
  Fixes:
gcc/d/expr.cc:2596:9: runtime error: null pointer passed as argument 2, which is declared to never be null

Ready for master?
Thanks,
Martin

gcc/d/ChangeLog:

	* expr.cc: Call memcpy only when length != 0.
---
  gcc/d/expr.cc | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
  

Comments

Martin Liška Dec. 6, 2021, 12:36 p.m. UTC | #1
On 12/6/21 13:03, Martin Liška wrote:
> gcc/d/expr.cc:2596:9: runtime error: null pointer passed as argument 2, which is declared to never be null

I forgot to mention that it happens for gcc/testsuite/gdc.dg/attr_optimize1.d
test-case.

Cheers,
Martin
  
Iain Buclaw Dec. 8, 2021, 8:52 p.m. UTC | #2
Excerpts from Martin Liška's message of December 6, 2021 1:03 pm:
> Fixes:
> gcc/d/expr.cc:2596:9: runtime error: null pointer passed as argument 2, which is declared to never be null
> 
> Ready for master?
> Thanks,
> Martin
> 

Looks reasonable to me.

Iain.
  

Patch

diff --git a/gcc/d/expr.cc b/gcc/d/expr.cc
index 31680564bdd..22acbc07cde 100644
--- a/gcc/d/expr.cc
+++ b/gcc/d/expr.cc
@@ -2593,7 +2593,8 @@  public:
  	/* Copy the string contents to a null terminated string.  */
  	dinteger_t length = (e->len * e->sz);
  	char *string = XALLOCAVEC (char, length + 1);
-	memcpy (string, e->string, length);
+	if (length > 0)
+	  memcpy (string, e->string, length);
  	string[length] = '\0';
  
  	/* String value and type includes the null terminator.  */