]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR tree-optimization/86572 (unsafe strlen folding of const arguments with non...
authorBernd Edlinger <bernd.edlinger@hotmail.de>
Sun, 4 Nov 2018 19:51:09 +0000 (19:51 +0000)
committerBernd Edlinger <edlinger@gcc.gnu.org>
Sun, 4 Nov 2018 19:51:09 +0000 (19:51 +0000)
gcc:
2018-11-04  Bernd Edlinger  <bernd.edlinger@hotmail.de>

        PR tree-optimization/86572
        * builtins.c (c_strlen): Handle negative offsets in a safe way.

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

        PR tree-optimization/86572
        * gcc.dg/pr86572.c: New test.

From-SVN: r265778

gcc/ChangeLog
gcc/builtins.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr86572.c [new file with mode: 0644]

index a7f3afdba7661aa134a78e48aac485e9da896a2c..ac121a8366018e374d2a6ba5da52d88246b6f891 100644 (file)
@@ -1,3 +1,8 @@
+2018-11-04  Bernd Edlinger  <bernd.edlinger@hotmail.de>
+
+       PR tree-optimization/86572
+       * builtins.c (c_strlen): Handle negative offsets in a safe way.
+
 2018-11-04  Bernd Edlinger  <bernd.edlinger@hotmail.de>
 
        PR tree-optimization/87672
index f64b3d4065b3f3977dbfe15587c4f49d5fabfb5d..9f2a90a4edf770b7740da07061029fa75ba0714e 100644 (file)
@@ -734,11 +734,14 @@ c_strlen (tree src, int only_value, c_strlen_data *data, unsigned eltsize)
         of the string subtract the offset from the length of the string,
         and return that.  Otherwise the length is zero.  Take care to
         use SAVE_EXPR in case the OFFSET has side-effects.  */
-      tree offsave = TREE_SIDE_EFFECTS (byteoff) ? save_expr (byteoff) : byteoff;
-      offsave = fold_convert (ssizetype, offsave);
+      tree offsave = TREE_SIDE_EFFECTS (byteoff) ? save_expr (byteoff)
+                                                : byteoff;
+      offsave = fold_convert_loc (loc, sizetype, offsave);
       tree condexp = fold_build2_loc (loc, LE_EXPR, boolean_type_node, offsave,
-                                     build_int_cst (ssizetype, len));
-      tree lenexp = size_diffop_loc (loc, ssize_int (len), offsave);
+                                     size_int (len));
+      tree lenexp = fold_build2_loc (loc, MINUS_EXPR, sizetype, size_int (len),
+                                    offsave);
+      lenexp = fold_convert_loc (loc, ssizetype, lenexp);
       return fold_build3_loc (loc, COND_EXPR, ssizetype, condexp, lenexp,
                              build_zero_cst (ssizetype));
     }
index 79a7859e537b39104bde7405ad6e52ff62a66e7d..a28d9a043eb109947ae1ff2258378fafcbc1e1d9 100644 (file)
@@ -1,3 +1,8 @@
+2018-11-04  Bernd Edlinger  <bernd.edlinger@hotmail.de>
+
+       PR tree-optimization/86572
+       * gcc.dg/pr86572.c: New test.
+
 2018-11-04  Bernd Edlinger  <bernd.edlinger@hotmail.de>
 
        PR tree-optimization/87672
diff --git a/gcc/testsuite/gcc.dg/pr86572.c b/gcc/testsuite/gcc.dg/pr86572.c
new file mode 100644 (file)
index 0000000..6cda55b
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+const char buf[40] = "test";
+void test (int x)
+{
+  if (__builtin_strlen (buf + x) > 4)
+    __builtin_abort ();
+}
+
+/* { dg-final { scan-tree-dump-not "abort" "optimized" } } */