re PR other/77421 (Bugs found in GCC with the help of PVS-Studio)
authorJakub Jelinek <jakub@redhat.com>
Mon, 5 Sep 2016 08:50:29 +0000 (10:50 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Mon, 5 Sep 2016 08:50:29 +0000 (10:50 +0200)
PR other/77421
* gensupport.c (alter_output_for_subst_insn): Remove redundant
*insn_out == '*' test.  Don't copy unnecessary to yet another
memory buffer, and don't leak it.

From-SVN: r239987

gcc/ChangeLog
gcc/gensupport.c

index c65af836910e442195e7f6b0f392d8e2e3dcff81..2eb8986637ff9ed8a83c5a7c22a79b43d505ddfb 100644 (file)
@@ -1,5 +1,10 @@
 2016-09-05  Jakub Jelinek  <jakub@redhat.com>
 
+       PR other/77421
+       * gensupport.c (alter_output_for_subst_insn): Remove redundant
+       *insn_out == '*' test.  Don't copy unnecessary to yet another
+       memory buffer, and don't leak it.
+
        PR rtl-optimization/77425
        * ipa-devirt.c (get_odr_type): Set val->id unconditionally.
 
index 7e3aad33000a7a990d11b11aa5c6ac7470f64afa..4645eadfff403ca591212d6d5414c7f773ee3a93 100644 (file)
@@ -1632,33 +1632,30 @@ duplicate_each_alternative (const char * str, int n_dup)
 static const char *
 alter_output_for_subst_insn (rtx insn, int alt)
 {
-  const char *insn_out, *sp ;
-  char *old_out, *new_out, *cp;
-  int i, j, new_len;
+  const char *insn_out, *old_out;
+  char *new_out, *cp;
+  size_t old_len, new_len;
+  int j;
 
   insn_out = XTMPL (insn, 3);
 
-  if (alt < 2 || *insn_out == '*' || *insn_out != '@')
+  if (alt < 2 || *insn_out != '@')
     return insn_out;
 
-  old_out = XNEWVEC (char, strlen (insn_out)),
-  sp = insn_out;
+  old_out = insn_out + 1;
+  while (ISSPACE (*old_out))
+    old_out++;
+  old_len = strlen (old_out);
 
-  while (ISSPACE (*sp) || *sp == '@')
-    sp++;
-
-  for (i = 0; *sp;)
-    old_out[i++] = *sp++;
-
-  new_len = alt * (i + 1) + 1;
+  new_len = alt * (old_len + 1) + 1;
 
   new_out = XNEWVEC (char, new_len);
   new_out[0] = '@';
 
-  for (j = 0, cp = new_out + 1; j < alt; j++, cp += i + 1)
+  for (j = 0, cp = new_out + 1; j < alt; j++, cp += old_len + 1)
     {
-      memcpy (cp, old_out, i);
-      *(cp+i) = (j == alt - 1) ? '\0' : '\n';
+      memcpy (cp, old_out, old_len);
+      cp[old_len] = (j == alt - 1) ? '\0' : '\n';
     }
 
   return new_out;