diff mbox series

Handle overlength strings in C++ FE

Message ID AM5PR0701MB2657F441FCF09F440C9C87E7E42D0@AM5PR0701MB2657.eurprd07.prod.outlook.com
State New
Headers show
Series Handle overlength strings in C++ FE | expand

Commit Message

Bernd Edlinger Aug. 1, 2018, 11:27 a.m. UTC
Hi,

this makes too long string constants shorter,
and fixes one place where a string constant is created
non-zero terminated.  This is a cleanup in preparation
of a more thorough check on the STRING_CST objects
in the middle-end.

Bootstrapped and reg-tested on x86_64-pc-linux-gnu.
Is it OK for trunk?


Thanks
Bernd.

Comments

Nathan Sidwell Aug. 1, 2018, 2:59 p.m. UTC | #1
On 08/01/2018 04:27 AM, Bernd Edlinger wrote:
> Hi,
> 
> this makes too long string constants shorter,
> and fixes one place where a string constant is created
> non-zero terminated.  This is a cleanup in preparation
> of a more thorough check on the STRING_CST objects
> in the middle-end.
> 
> Bootstrapped and reg-tested on x86_64-pc-linux-gnu.
> Is it OK for trunk?

ok.
diff mbox series

Patch

2018-08-01  Bernd Edlinger  <bernd.edlinger@hotmail.de>

	* typeck2.c (digest_init_r): Fix overlength strings.
	* vtable-class-hierarchy.c (build_key_buffer_arg): Make string literal
	NUL terminated.

diff -pur gcc/cp/typeck2.c gcc/cp/typeck2.c
--- gcc/cp/typeck2.c	2018-06-27 04:59:44.000000000 +0200
+++ gcc/cp/typeck2.c	2018-08-01 06:57:15.275041272 +0200
@@ -1105,8 +1105,23 @@  digest_init_r (tree type, tree init, int
 		 counted in the length of the constant, but in C++ this would
 		 be invalid.  */
 	      if (size < TREE_STRING_LENGTH (init))
-		permerror (loc, "initializer-string for array "
-			   "of chars is too long");
+		{
+		  permerror (loc, "initializer-string for array "
+			     "of chars is too long");
+		  if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
+		    {
+		      unsigned unit
+			= TYPE_PRECISION (TREE_TYPE (type)) / BITS_PER_UNIT;
+		      const char *p = TREE_STRING_POINTER (init);
+		      char *q = (char *)xmalloc (size + unit);
+
+		      memcpy (q, p, size);
+		      memset (q + size, 0, unit);
+		      init = build_string (size + unit, q);
+		      TREE_TYPE (init) = type;
+		      free (q);
+		    }
+		}
 	    }
 	  return init;
 	}
diff -pur gcc/cp/vtable-class-hierarchy.c gcc/cp/vtable-class-hierarchy.c
--- gcc/cp/vtable-class-hierarchy.c	2018-01-03 11:03:58.000000000 +0100
+++ gcc/cp/vtable-class-hierarchy.c	2018-08-01 09:13:38.522646005 +0200
@@ -738,7 +738,7 @@  build_key_buffer_arg (tree base_ptr_var_
   tree ret_value;
 
   /* Set the len and hash for the string.  */
-  *value_ptr = len1;
+  *value_ptr = len1++;
   value_ptr++;
   *value_ptr = hash_value;