summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.c-torture
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2020-01-15 01:28:43 +0100
committerJakub Jelinek <jakub@redhat.com>2020-01-15 01:28:43 +0100
commit623c6fddd605f8f225142d714440320e4ef54d84 (patch)
tree307ab325b0611135185827e918ffcff7bf0abfef /gcc/testsuite/gcc.c-torture
parentd8998708ca316249e475d139c89ae7d169e64d34 (diff)
tree-optimization: Fix tree dse of strncpy PR93249
As the testcase shows, tail trimming of strncpy in tree-ssa-dse.c is fine, we just copy or clear fewer bytes in the destination, but unlike memcpy/memset etc., head trimming is problematic in certain cases. If we can prove that there are no zero bytes among initial head_trim bytes, it is ok to trim it, if we can prove there is at least one zero byte among initial head_trim bytes, we could (not implemented in the patch) turn the strncpy into memset 0, but otherwise we need to avoid the head trimming, because the presence or absence of NUL byte there changes the behavior for subsequent bytes, whether further bytes from src are copied or if further bytes are cleared. 2020-01-15 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/93249 * tree-ssa-dse.c: Include builtins.h and gimple-fold.h. (maybe_trim_memstar_call): Move head_trim and tail_trim vars to function body scope, reindent. For BUILTIN_IN_STRNCPY*, don't perform head trim unless we can prove there are no '\0' chars from the source among the first head_trim chars. * gcc.c-torture/execute/pr93249.c: New test.
Diffstat (limited to 'gcc/testsuite/gcc.c-torture')
-rw-r--r--gcc/testsuite/gcc.c-torture/execute/pr93249.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr93249.c b/gcc/testsuite/gcc.c-torture/execute/pr93249.c
new file mode 100644
index 00000000000..2c9a6b4dee0
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/pr93249.c
@@ -0,0 +1,40 @@
+/* PR tree-optimization/93249 */
+
+char a[2], b[4], c[6];
+
+void
+foo (void)
+{
+ char d[2] = { 0x00, 0x11 };
+ __builtin_strncpy (&b[2], d, 2);
+ __builtin_strncpy (&b[1], a, 2);
+ if (b[0] || b[1] || b[2] || b[3])
+ __builtin_abort ();
+}
+
+void
+bar (void)
+{
+ __builtin_strncpy (&b[2], "\0\x11", 2);
+ __builtin_strncpy (&b[1], a, 2);
+ if (b[0] || b[1] || b[2] || b[3])
+ __builtin_abort ();
+}
+
+void
+baz (void)
+{
+ __builtin_strncpy (&c[2], "\x11\x11\0\x11", 4);
+ __builtin_strncpy (&c[1], a, 2);
+ if (c[0] || c[1] || c[2] || c[3] != 0x11 || c[4] || c[5])
+ __builtin_abort ();
+}
+
+int
+main ()
+{
+ foo ();
+ bar ();
+ baz ();
+ return 0;
+}