Fix middle-end/86528

Message ID AM5PR0701MB2657B6FEB522B8CD7D93F922E45D0@AM5PR0701MB2657.eurprd07.prod.outlook.com
State New
Headers show
Series
  • Fix middle-end/86528
Related show

Commit Message

Bernd Edlinger July 16, 2018, 3:36 p.m.
Hi,

this fixes PR middle-end/86528.


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


Thanks
Bernd.

Comments

Martin Sebor July 16, 2018, 5:51 p.m. | #1
On 07/16/2018 09:36 AM, Bernd Edlinger wrote:
> Hi,

>

> this fixes PR middle-end/86528.

>

>

> Bootstrapped and reg-tested on x86_64-pc-linux-gnu.

> Is it OK for trunk?


Thanks -- the string_constant change also fixes bug 86532.

There is another problem in the subsequent handling of
the CONSTRUCTOR -- it ignores the non-constant VARIDX.  Fixing
that is easy but unfortunately prevents many strlen calls that
could be folded from folding (and makes the newly added tests
fail). For instance:

   static const char a[2][3] = { "1", "12" };
   if (strlen (&a[1][i]) > 2)
     abort ();

That needs some other changes to handle.  Let me take care of
that.

Martin
Richard Biener July 16, 2018, 5:56 p.m. | #2
On July 16, 2018 5:36:22 PM GMT+02:00, Bernd Edlinger <bernd.edlinger@hotmail.de> wrote:
>Hi,

>

>this fixes PR middle-end/86528.

>

>

>Bootstrapped and reg-tested on x86_64-pc-linux-gnu.

>Is it OK for trunk?


OK. 

Richard. 

>

>Thanks

>Bernd.

Patch

gcc:
2018-07-04  Bernd Edlinger  <bernd.edlinger@hotmail.de>

	PR middle-end/86528
	* builtins.c (check_access): Bail out if range[0] is no INTEGER_CST.
	* expr.c (string_constant): Fix the element size of ARRAY_TYPE.

testsuite:
2018-07-04  Bernd Edlinger  <bernd.edlinger@hotmail.de>

	PR middle-end/86528
	* gcc.c-torture/execute/pr86528.c: New test.
	* gcc.dg/Wrestrict-10.c (test_arr_strcat_2): Fix typo.

--- /dev/null	2018-07-02 16:09:41.095282291 +0200
+++ gcc/testsuite/gcc.c-torture/execute/pr86528.c	2018-07-16 14:32:24.555426245 +0200
@@ -0,0 +1,20 @@ 
+/* PR middle-end/86528 */
+
+void __attribute__((noinline, noclone))
+test(char *data, __SIZE_TYPE__ len)
+{
+    static char const appended[] = "/./";
+    char *buf = __builtin_alloca (len + sizeof appended);
+    __builtin_memcpy (buf, data, len);
+    __builtin_strcpy (buf + len, &appended[data[len - 1] == '/']);
+    if (__builtin_strcmp(buf, "test1234/./"))
+        __builtin_abort();
+}
+
+int
+main()
+{
+   char *arg = "test1234/";
+   test(arg, __builtin_strlen(arg));
+   return 0;
+}
--- gcc/testsuite/gcc.dg/Wrestrict-10.c.jj	2018-05-16 04:30:38.000000000 +0200
+++ gcc/testsuite/gcc.dg/Wrestrict-10.c	2018-07-16 16:06:13.250852255 +0200
@@ -39,8 +39,7 @@  test_arr_strcat_1 (void)
 void __attribute__ ((noclone, noinline))
 test_arr_strcat_2 (void)
 {
-  /* This probably deserves a warning.  */
-  strcpy (b.a, &b.a[i]);
+  strcat (b.a, &b.a[i]);            /* { dg-warning "\\\[-Wrestrict" } */
 }
 
 void __attribute__ ((noclone, noinline))
--- gcc/builtins.c.jj	2018-07-13 16:10:45.000000000 +0200
+++ gcc/builtins.c	2018-07-16 12:48:18.880896706 +0200
@@ -3192,6 +3192,10 @@  check_access (tree exp, tree, tree, tree
   if (dstwrite)
     get_size_range (dstwrite, range);
 
+  /* This can happen at -O0.  */
+  if (range[0] && TREE_CODE (range[0]) != INTEGER_CST)
+    return false;
+
   tree func = get_callee_fndecl (exp);
 
   /* First check the number of bytes to be written against the maximum
--- gcc/expr.c.jj	2018-07-09 22:33:48.000000000 +0200
+++ gcc/expr.c	2018-07-16 13:18:03.433353514 +0200
@@ -11341,7 +11341,9 @@  string_constant (tree arg, tree *ptr_off
   tree offset = wide_int_to_tree (sizetype, base_off);
   if (varidx)
     {
-      if (tree eltsize = TYPE_SIZE_UNIT (TREE_TYPE (array)))
+      if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
+	return NULL_TREE;
+      if (tree eltsize = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (array))))
 	{
 	  /* Add the scaled variable index to the constant offset.  */
 	  tree eltoff = fold_build2 (MULT_EXPR, TREE_TYPE (offset),