https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86532
--- Comment #7 from Bernd Edlinger <bernd.edlinger at hotmail dot de> ---
Maybe something like the following?
--- expr.c.kk 2018-07-17 10:14:43.668347058 +0200
+++ expr.c 2018-07-17 10:21:13.101779984 +0200
@@ -11282,6 +11282,7 @@ string_constant (tree arg, tree *ptr_off
/* Non-constant index into the character array in an ARRAY_REF
expression or null. */
tree varidx = NULL_TREE;
+ tree varsize = NULL_TREE;
poly_int64 base_off = 0;
@@ -11289,6 +11290,7 @@ string_constant (tree arg, tree *ptr_off
{
arg = TREE_OPERAND (arg, 0);
tree ref = arg;
+ varsize = TYPE_SIZE_UNIT (TREE_TYPE (ref));
if (TREE_CODE (arg) == ARRAY_REF)
{
tree idx = TREE_OPERAND (arg, 1);
@@ -11369,7 +11371,7 @@ string_constant (tree arg, tree *ptr_off
/* Handle variables initialized with string literals. */
if (!init || init == error_mark_node)
return NULL_TREE;
- if (TREE_CODE (init) == CONSTRUCTOR)
+ if (TREE_CODE (init) == CONSTRUCTOR && !varidx)
{
if (TREE_CODE (arg) != ARRAY_REF
&& TREE_CODE (arg) == COMPONENT_REF
@@ -11401,8 +11403,7 @@ string_constant (tree arg, tree *ptr_off
if (!init || TREE_CODE (init) != STRING_CST)
return NULL_TREE;
- tree array_size = DECL_SIZE_UNIT (array);
- if (!array_size || TREE_CODE (array_size) != INTEGER_CST)
+ if (!varsize || TREE_CODE (varsize) != INTEGER_CST)
return NULL_TREE;
/* Avoid returning a string that doesn't fit in the array
@@ -11415,7 +11416,7 @@ string_constant (tree arg, tree *ptr_off
but not to strlen(). */
unsigned HOST_WIDE_INT length
= strnlen (TREE_STRING_POINTER (init), TREE_STRING_LENGTH (init));
- if (compare_tree_int (array_size, length + 1) < 0)
+ if (compare_tree_int (varsize, length + 1) < 0)
return NULL_TREE;
*ptr_offset = offset;