Commit e8bf3d5e by Bernd Edlinger Committed by Bernd Edlinger

re PR tree-optimization/86572 (unsafe strlen folding of const arguments with non-const offset)

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
parent 770fe3a3
2018-11-04 Bernd Edlinger <bernd.edlinger@hotmail.de> 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 PR tree-optimization/87672
* gimple-fold.c (gimple_fold_builtin_stxcpy_chk): Gimplify. * gimple-fold.c (gimple_fold_builtin_stxcpy_chk): Gimplify.
* tree-ssa-strlen.c (handle_builtin_strcat): Adjust object size. * tree-ssa-strlen.c (handle_builtin_strcat): Adjust object size.
...@@ -734,11 +734,14 @@ c_strlen (tree src, int only_value, c_strlen_data *data, unsigned eltsize) ...@@ -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, of the string subtract the offset from the length of the string,
and return that. Otherwise the length is zero. Take care to and return that. Otherwise the length is zero. Take care to
use SAVE_EXPR in case the OFFSET has side-effects. */ use SAVE_EXPR in case the OFFSET has side-effects. */
tree offsave = TREE_SIDE_EFFECTS (byteoff) ? save_expr (byteoff) : byteoff; tree offsave = TREE_SIDE_EFFECTS (byteoff) ? save_expr (byteoff)
offsave = fold_convert (ssizetype, offsave); : byteoff;
offsave = fold_convert_loc (loc, sizetype, offsave);
tree condexp = fold_build2_loc (loc, LE_EXPR, boolean_type_node, offsave, tree condexp = fold_build2_loc (loc, LE_EXPR, boolean_type_node, offsave,
build_int_cst (ssizetype, len)); size_int (len));
tree lenexp = size_diffop_loc (loc, ssize_int (len), offsave); 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, return fold_build3_loc (loc, COND_EXPR, ssizetype, condexp, lenexp,
build_zero_cst (ssizetype)); build_zero_cst (ssizetype));
} }
......
2018-11-04 Bernd Edlinger <bernd.edlinger@hotmail.de> 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 PR tree-optimization/87672
* gcc.dg/pr87672.c: New test. * gcc.dg/pr87672.c: New test.
......
/* { 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" } } */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment