Commit 25b15e95 by Martin Sebor Committed by Martin Sebor

PR tree-optimization/83075 - Invalid strncpy optimization

gcc/ChangeLog:

	PR tree-optimization/83075
	* tree-ssa-strlen.c (handle_builtin_stxncpy): Avoid assuming
	strncat/strncpy don't change length of source string.

gcc/testsuite/ChangeLog:

	PR tree-optimization/83075
	* gcc.dg/tree-ssa/strncat.c: New test.
	* gcc.dg/tree-ssa/strncpy-2.c: Same.

From-SVN: r255446
parent 4c413747
2017-12-06 Martin Sebor <msebor@redhat.com>
PR tree-optimization/83075
* tree-ssa-strlen.c (handle_builtin_stxncpy): Avoid assuming
strncat/strncpy don't change length of source string.
2017-12-06 Eric Botcazou <ebotcazou@adacore.com> 2017-12-06 Eric Botcazou <ebotcazou@adacore.com>
Revert Revert
2017-12-06 Martin Sebor <msebor@redhat.com>
PR tree-optimization/83075
* gcc.dg/tree-ssa/strncat.c: New test.
* gcc.dg/tree-ssa/strncpy-2.c: Same.
2017-12-06 Bin Cheng <bin.cheng@arm.com> 2017-12-06 Bin Cheng <bin.cheng@arm.com>
* g++.dg/graphite/pr41305.C: Refine test option. * g++.dg/graphite/pr41305.C: Refine test option.
......
/* PR tree-optimization/83075 - Invalid strncpy optimization
{ dg-do run }
{ dg-options "-O2 -Wno-stringop-overflow" } */
int main (void)
{
char a[8] = "";
__builtin_strcpy (a, "123");
unsigned n0 = __builtin_strlen (a);
__builtin_strncat (a + 3, a, n0);
unsigned n1 = __builtin_strlen (a);
if (n1 == n0)
__builtin_abort ();
}
/* PR tree-optimization/83075 - Invalid strncpy optimization
{ dg-do run }
{ dg-options "-O2 -Wno-stringop-overflow" } */
int main (void)
{
char a[8] = "";
__builtin_strcpy (a, "123");
unsigned n0 = __builtin_strlen (a);
__builtin_strncpy (a + 3, a, n0);
unsigned n1 = __builtin_strlen (a);
if (n1 == n0)
__builtin_abort ();
}
...@@ -1941,10 +1941,9 @@ handle_builtin_stxncpy (built_in_function, gimple_stmt_iterator *gsi) ...@@ -1941,10 +1941,9 @@ handle_builtin_stxncpy (built_in_function, gimple_stmt_iterator *gsi)
int sidx = get_stridx (src); int sidx = get_stridx (src);
strinfo *sisrc = sidx > 0 ? get_strinfo (sidx) : NULL; strinfo *sisrc = sidx > 0 ? get_strinfo (sidx) : NULL;
/* Strncpy() et al. cannot modify the source string. Prevent the rest /* strncat() and strncpy() can modify the source string by writing
of the pass from invalidating the strinfo data. */ over the terminating nul so SISRC->DONT_INVALIDATE must be left
if (sisrc) clear. */
sisrc->dont_invalidate = true;
/* Retrieve the strinfo data for the string S that LEN was computed /* Retrieve the strinfo data for the string S that LEN was computed
from as some function F of strlen (S) (i.e., LEN need not be equal from as some function F of strlen (S) (i.e., LEN need not be equal
......
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