From c5cb79681df13acc02894b4df8314c797895e2bb Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Mon, 5 Sep 2016 10:50:29 +0200 Subject: [PATCH] re PR other/77421 (Bugs found in GCC with the help of PVS-Studio) 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 | 5 +++++ gcc/gensupport.c | 29 +++++++++++++---------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index c65af836910..2eb8986637f 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,5 +1,10 @@ 2016-09-05 Jakub Jelinek + 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. diff --git a/gcc/gensupport.c b/gcc/gensupport.c index 7e3aad33000..4645eadfff4 100644 --- a/gcc/gensupport.c +++ b/gcc/gensupport.c @@ -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; -- 2.30.2