PR middle-end/30473
authorjakub <jakub@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 2 Feb 2007 12:21:13 +0000 (12:21 +0000)
committerjakub <jakub@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 2 Feb 2007 12:21:13 +0000 (12:21 +0000)
* builtins.c (fold_builtin_sprintf): Do not attempt to optimize
sprintf (str, "%s").  Do not optimize sprintf (str, "nopercent", p++).

* gcc.dg/pr30473.c: New test.
* gcc.c-torture/execute/20070201-1.c: New test.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@121495 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/builtins.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.c-torture/execute/20070201-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr30473.c [new file with mode: 0644]

index 1e1cefa..25b8df0 100644 (file)
@@ -1,3 +1,9 @@
+2007-02-02  Jakub Jelinek  <jakub@redhat.com>
+
+       PR middle-end/30473
+       * builtins.c (fold_builtin_sprintf): Do not attempt to optimize
+       sprintf (str, "%s").  Do not optimize sprintf (str, "nopercent", p++).
+
 2007-02-02  Maxim Kuvyrkov  <mkuvyrkov@ispras.ru>
 
        * sched-int.h (ds_to_dk, dk_to_ds): Declare functions.
index 73d0a11..5b9a87f 100644 (file)
@@ -10553,6 +10553,7 @@ fold_builtin_sprintf (tree arglist, int ignored)
   /* Get the destination string and the format specifier.  */
   dest = TREE_VALUE (arglist);
   fmt = TREE_VALUE (TREE_CHAIN (arglist));
+  arglist = TREE_CHAIN (TREE_CHAIN (arglist));
 
   /* Check whether the format is a literal string constant.  */
   fmt_str = c_getstr (fmt);
@@ -10573,6 +10574,10 @@ fold_builtin_sprintf (tree arglist, int ignored)
       if (!fn)
        return NULL_TREE;
 
+      /* Don't optimize sprintf (buf, "abc", ptr++).  */
+      if (arglist)
+       return NULL_TREE;
+
       /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
         'format' is known to contain no % formats.  */
       arglist = build_tree_list (NULL_TREE, fmt);
@@ -10591,8 +10596,12 @@ fold_builtin_sprintf (tree arglist, int ignored)
       if (!fn)
        return NULL_TREE;
 
+      /* Don't crash on sprintf (str1, "%s").  */
+      if (!arglist)
+       return NULL_TREE;
+
       /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2).  */
-      orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
+      orig = TREE_VALUE (arglist);
       arglist = build_tree_list (NULL_TREE, orig);
       arglist = tree_cons (NULL_TREE, dest, arglist);
       if (!ignored)
index 31d23b7..e699db6 100644 (file)
@@ -1,3 +1,9 @@
+2007-02-02  Jakub Jelinek  <jakub@redhat.com>
+
+       PR middle-end/30473
+       * gcc.dg/pr30473.c: New test.
+       * gcc.c-torture/execute/20070201-1.c: New test.
+
 2007-02-01  Roger Sayle  <roger@eyesopen.com>
 
        * gfortran.dg/dependency_20.f90: New test case.
diff --git a/gcc/testsuite/gcc.c-torture/execute/20070201-1.c b/gcc/testsuite/gcc.c-torture/execute/20070201-1.c
new file mode 100644 (file)
index 0000000..c676c34
--- /dev/null
@@ -0,0 +1,20 @@
+/* PR middle-end/30473 */
+
+extern int sprintf (char *, const char *, ...);
+extern void abort (void);
+
+char *
+foo (char *buf, char *p)
+{
+  sprintf (buf, "abcde", p++);
+  return p;
+}
+
+int
+main (void)
+{
+  char buf[6];
+  if (foo (buf, &buf[2]) != &buf[3])
+    abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/pr30473.c b/gcc/testsuite/gcc.dg/pr30473.c
new file mode 100644 (file)
index 0000000..f01c1cc
--- /dev/null
@@ -0,0 +1,13 @@
+/* PR middle-end/30473 */
+/* Make sure this doesn't ICE.  */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+extern int sprintf (char *, const char *, ...);
+
+void
+foo (char *buf1, char *buf2)
+{
+  sprintf (buf1, "%s", "abcde");
+  sprintf (buf2, "%s");
+}