From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1062) id F07C3385141E; Thu, 3 Aug 2023 11:51:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F07C3385141E Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Alan Modra To: bfd-cvs@sourceware.org Subject: [binutils-gdb] binutils sprintf optimisation X-Act-Checkin: binutils-gdb X-Git-Author: Alan Modra X-Git-Refname: refs/heads/master X-Git-Oldrev: 75747be51e6d11287e12c4504f2607d36a0edfd2 X-Git-Newrev: ad923ded82d0ede5a2506a9fbd0f5a5a6728e1a6 Message-Id: <20230803115153.F07C3385141E@sourceware.org> Date: Thu, 3 Aug 2023 11:51:53 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 03 Aug 2023 11:51:54 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dad923ded82d0= ede5a2506a9fbd0f5a5a6728e1a6 commit ad923ded82d0ede5a2506a9fbd0f5a5a6728e1a6 Author: Alan Modra Date: Thu Aug 3 12:32:40 2023 +0930 binutils sprintf optimisation =20 Avoid the use of sprintf with a "%s" format string, replacing with strcpy or stpcpy. Use sprintf return value rather than a later strlen. Don't use strcat where we can keep track of the end of a string output buffer. =20 * dlltool.c (look_for_prog): memcpy prefix and strcpy prog_name. * dllwrap.c (look_for_prog): Likewise. * resrc.c (look_for_default): Likewise. Add quotes with memmove rather than allocating another buffer. * size.c (size_number): Use sprintf return value. * stabs.c (parse_stab_argtypes): Likewise. * windmc.c (write_bin): Likewes, and use stpcpy. * wrstabs.c: Similarly throughout. Diff: --- binutils/dlltool.c | 4 +-- binutils/dllwrap.c | 4 +-- binutils/resrc.c | 14 ++++----- binutils/size.c | 8 ++--- binutils/stabs.c | 10 +++---- binutils/windmc.c | 12 ++++---- binutils/wrstabs.c | 86 ++++++++++++++++++++++++++------------------------= ---- 7 files changed, 68 insertions(+), 70 deletions(-) diff --git a/binutils/dlltool.c b/binutils/dlltool.c index efa54f1bd72..085d4c2ce41 100644 --- a/binutils/dlltool.c +++ b/binutils/dlltool.c @@ -4188,9 +4188,9 @@ look_for_prog (const char *prog_name, const char *pre= fix, int end_prefix) + strlen (EXECUTABLE_SUFFIX) #endif + 10); - strcpy (cmd, prefix); + memcpy (cmd, prefix, end_prefix); =20 - sprintf (cmd + end_prefix, "%s", prog_name); + strcpy (cmd + end_prefix, prog_name); =20 if (strchr (cmd, '/') !=3D NULL) { diff --git a/binutils/dllwrap.c b/binutils/dllwrap.c index e6fee8bb061..f333eb561e2 100644 --- a/binutils/dllwrap.c +++ b/binutils/dllwrap.c @@ -186,9 +186,9 @@ look_for_prog (const char *progname, const char *prefix= , int end_prefix) + strlen (EXECUTABLE_SUFFIX) #endif + 10); - strcpy (cmd, prefix); + memcpy (cmd, prefix, end_prefix); =20 - sprintf (cmd + end_prefix, "%s", progname); + strcpy (cmd + end_prefix, progname); =20 if (strchr (cmd, '/') !=3D NULL) { diff --git a/binutils/resrc.c b/binutils/resrc.c index 0d7a6e1cdbc..468e2fd8fe1 100644 --- a/binutils/resrc.c +++ b/binutils/resrc.c @@ -383,9 +383,9 @@ look_for_default (char *cmd, const char *prefix, int en= d_prefix, struct stat s; const char *fnquotes =3D (filename_need_quotes (filename) ? "\"" : ""); =20 - strcpy (cmd, prefix); + memcpy (cmd, prefix, end_prefix); =20 - sprintf (cmd + end_prefix, "%s", DEFAULT_PREPROCESSOR_CMD); + char *out =3D stpcpy (cmd + end_prefix, DEFAULT_PREPROCESSOR_CMD); =20 if ( #if defined (__DJGPP__) || defined (__CYGWIN__) || defined (_WIN32) @@ -409,13 +409,13 @@ look_for_default (char *cmd, const char *prefix, int = end_prefix, =20 if (filename_need_quotes (cmd)) { - char *cmd_copy =3D xmalloc (strlen (cmd)); - strcpy (cmd_copy, cmd); - sprintf (cmd, "\"%s\"", cmd_copy); - free (cmd_copy); + memmove (cmd + 1, cmd, out - cmd); + cmd[0] =3D '"'; + out++; + *out++ =3D '"'; } =20 - sprintf (cmd + strlen (cmd), " %s %s %s%s%s", + sprintf (out, " %s %s %s%s%s", DEFAULT_PREPROCESSOR_ARGS, preprocargs, fnquotes, filename, fnquotes); =20 if (verbose) diff --git a/binutils/size.c b/binutils/size.c index 94738977cb8..243c740159a 100644 --- a/binutils/size.c +++ b/binutils/size.c @@ -441,11 +441,9 @@ size_number (bfd_size_type num) { char buffer[40]; =20 - sprintf (buffer, (radix =3D=3D decimal ? "%" PRIu64 - : radix =3D=3D octal ? "0%" PRIo64 : "0x%" PRIx64), - (uint64_t) num); - - return strlen (buffer); + return sprintf (buffer, (radix =3D=3D decimal ? "%" PRIu64 + : radix =3D=3D octal ? "0%" PRIo64 : "0x%" PRIx64), + (uint64_t) num); } =20 static void diff --git a/binutils/stabs.c b/binutils/stabs.c index 311b73b8418..0acb7f1e0db 100644 --- a/binutils/stabs.c +++ b/binutils/stabs.c @@ -3030,7 +3030,7 @@ parse_stab_argtypes (void *dhandle, struct stab_handl= e *info, =20 if (!(is_destructor || is_full_physname_constructor || is_v3)) { - unsigned int len; + unsigned int len, buf_len; const char *const_prefix; const char *volatile_prefix; char buf[20]; @@ -3042,19 +3042,19 @@ parse_stab_argtypes (void *dhandle, struct stab_han= dle *info, volatile_prefix =3D volatilep ? "V" : ""; =20 if (len =3D=3D 0) - sprintf (buf, "__%s%s", const_prefix, volatile_prefix); + buf_len =3D sprintf (buf, "__%s%s", const_prefix, volatile_prefix); else if (tagname !=3D NULL && strchr (tagname, '<') !=3D NULL) { /* Template methods are fully mangled. */ - sprintf (buf, "__%s%s", const_prefix, volatile_prefix); + buf_len =3D sprintf (buf, "__%s%s", const_prefix, volatile_prefix); tagname =3D NULL; len =3D 0; } else - sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len); + buf_len =3D sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len); =20 mangled_name_len =3D ((is_constructor ? 0 : strlen (fieldname)) - + strlen (buf) + + buf_len + len + strlen (argtypes) + 1); diff --git a/binutils/windmc.c b/binutils/windmc.c index b1db2b538ae..9e9ed9e87ab 100644 --- a/binutils/windmc.c +++ b/binutils/windmc.c @@ -746,12 +746,14 @@ write_bin (void) nd =3D convert_unicode_to_ACP (n->lang->sval); =20 /* Prepare filename for binary output. */ - filename =3D xmalloc (strlen (nd) + 4 + 1 + strlen (mcset_mc_basenam= e) + 1 + strlen (mcset_rc_dir)); - strcpy (filename, mcset_rc_dir); + filename =3D xmalloc (strlen (nd) + 4 + 1 + strlen (mcset_mc_basenam= e) + + 1 + strlen (mcset_rc_dir)); + char *out =3D filename; + out =3D stpcpy (out, mcset_rc_dir); if (mcset_prefix_bin) - sprintf (filename + strlen (filename), "%s_", mcset_mc_basename); - strcat (filename, nd); - strcat (filename, ".bin"); + out +=3D sprintf (out, "%s_", mcset_mc_basename); + out =3D stpcpy (out, nd); + out =3D stpcpy (out, ".bin"); =20 /* Write message file. */ windmc_write_bin (filename, &mc_nodes_lang[i], (c - i)); diff --git a/binutils/wrstabs.c b/binutils/wrstabs.c index 955b610cd59..293189db2f8 100644 --- a/binutils/wrstabs.c +++ b/binutils/wrstabs.c @@ -678,27 +678,29 @@ stab_int_type (void *p, unsigned int size, bool unsig= nedp) =20 cache[size - 1] =3D tindex; =20 - sprintf (buf, "%ld=3Dr%ld;", tindex, tindex); + int len =3D sprintf (buf, "%ld=3Dr%ld;", tindex, tindex); if (unsignedp) { - strcat (buf, "0;"); + strcpy (buf + len, "0;"); + len +=3D 2; if (size < sizeof (long)) - sprintf (buf + strlen (buf), "%ld;", ((long) 1 << (size * 8)) - 1); + sprintf (buf + len, "%ld;", ((long) 1 << (size * 8)) - 1); else if (size =3D=3D sizeof (long)) - strcat (buf, "-1;"); + strcpy (buf + len, "-1;"); else if (size =3D=3D 8) - strcat (buf, "01777777777777777777777;"); + strcpy (buf + len, "01777777777777777777777;"); else abort (); } else { if (size <=3D sizeof (long)) - sprintf (buf + strlen (buf), "%ld;%ld;", + sprintf (buf + len, "%ld;%ld;", (long) - ((unsigned long) 1 << (size * 8 - 1)), (long) (((unsigned long) 1 << (size * 8 - 1)) - 1)); else if (size =3D=3D 8) - strcat (buf, "01000000000000000000000;0777777777777777777777;"); + strcpy (buf + len, + "01000000000000000000000;0777777777777777777777;"); else abort (); } @@ -828,19 +830,19 @@ stab_enum_type (void *p, const char *tag, const char = **names, len +=3D strlen (*pn) + 22; =20 buf =3D xmalloc (len); - + char *out =3D buf; if (tag =3D=3D NULL) - strcpy (buf, "e"); + out =3D stpcpy (out, "e"); else { tindex =3D info->type_index; ++info->type_index; - sprintf (buf, "%s:T%ld=3De", tag, tindex); + out +=3D sprintf (out, "%s:T%ld=3De", tag, tindex); } =20 for (pn =3D names, pv =3D vals; *pn !=3D NULL; pn++, pv++) - sprintf (buf + strlen (buf), "%s:%ld,", *pn, (long) *pv); - strcat (buf, ";"); + out +=3D sprintf (out, "%s:%ld,", *pn, (long) *pv); + strcpy (out, ";"); =20 if (tag =3D=3D NULL) { @@ -1031,12 +1033,9 @@ stab_array_type (void *p, bfd_signed_vma low, bfd_si= gned_vma high, element =3D stab_pop_type (info); =20 buf =3D xmalloc (strlen (range) + strlen (element) + 70); - + char *out =3D buf; if (! stringp) - { - tindex =3D 0; - *buf =3D '\0'; - } + tindex =3D 0; else { /* We need to define a type in order to include the string @@ -1044,10 +1043,10 @@ stab_array_type (void *p, bfd_signed_vma low, bfd_s= igned_vma high, tindex =3D info->type_index; ++info->type_index; definition =3D true; - sprintf (buf, "%ld=3D@S;", tindex); + out +=3D sprintf (out, "%ld=3D@S;", tindex); } =20 - sprintf (buf + strlen (buf), "ar%s;%ld;%ld;%s", + sprintf (out, "ar%s;%ld;%ld;%s", range, (long) low, (long) high, element); free (range); free (element); @@ -1073,12 +1072,9 @@ stab_set_type (void *p, bool bitstringp) =20 s =3D stab_pop_type (info); buf =3D xmalloc (strlen (s) + 26); - + char *out =3D buf; if (! bitstringp) - { - *buf =3D '\0'; - tindex =3D 0; - } + tindex =3D 0; else { /* We need to define a type in order to include the string @@ -1086,10 +1082,10 @@ stab_set_type (void *p, bool bitstringp) tindex =3D info->type_index; ++info->type_index; definition =3D true; - sprintf (buf, "%ld=3D@S;", tindex); + out +=3D sprintf (out, "%ld=3D@S;", tindex); } =20 - sprintf (buf + strlen (buf), "S%s", s); + sprintf (out, "S%s", s); free (s); =20 return stab_push_string (info, buf, tindex, definition, 0); @@ -1304,11 +1300,11 @@ stab_start_struct_type (void *p, const char *tag, u= nsigned int id, long tindex; bool definition; char buf[40]; + char *out =3D buf; =20 if (id =3D=3D 0) { tindex =3D 0; - *buf =3D '\0'; definition =3D false; } else @@ -1317,11 +1313,11 @@ stab_start_struct_type (void *p, const char *tag, u= nsigned int id, &size); if (tindex < 0) return false; - sprintf (buf, "%ld=3D", tindex); + out +=3D sprintf (out, "%ld=3D", tindex); definition =3D true; } =20 - sprintf (buf + strlen (buf), "%c%u", + sprintf (out, "%c%u", structp ? 's' : 'u', size); =20 @@ -1699,19 +1695,21 @@ stab_class_method_var (struct stab_write_handle *in= fo, const char *physname, else typec =3D '*'; =20 + size_t cur_len =3D strlen (info->type_stack->methods); info->type_stack->methods =3D - xrealloc (info->type_stack->methods, - (strlen (info->type_stack->methods) + strlen (type) - + strlen (physname) + (contextp ? strlen (context) : 0) + 40)); - - sprintf (info->type_stack->methods + strlen (info->type_stack->methods), - "%s:%s;%c%c%c", type, physname, visc, qualc, typec); + xrealloc (info->type_stack->methods, (cur_len + + strlen (type) + + strlen (physname) + + (contextp ? strlen (context) : 0) + + 40)); + + char *out =3D info->type_stack->methods + cur_len; + out +=3D sprintf (out, "%s:%s;%c%c%c", type, physname, visc, qualc, type= c); free (type); =20 if (contextp) { - sprintf (info->type_stack->methods + strlen (info->type_stack->metho= ds), - "%ld;%s;", (long) voffset, context); + sprintf (out, "%ld;%s;", (long) voffset, context); free (context); } =20 @@ -1800,36 +1798,36 @@ stab_end_class_type (void *p) =20 buf =3D xmalloc (len); =20 - strcpy (buf, info->type_stack->string); + char *out =3D stpcpy (buf, info->type_stack->string); =20 if (info->type_stack->baseclasses !=3D NULL) { - sprintf (buf + strlen (buf), "!%u,", i); + out +=3D sprintf (out, "!%u,", i); for (i =3D 0; info->type_stack->baseclasses[i] !=3D NULL; i++) { - strcat (buf, info->type_stack->baseclasses[i]); + out =3D stpcpy (out, info->type_stack->baseclasses[i]); free (info->type_stack->baseclasses[i]); } free (info->type_stack->baseclasses); info->type_stack->baseclasses =3D NULL; } =20 - strcat (buf, info->type_stack->fields); + out =3D stpcpy (out, info->type_stack->fields); free (info->type_stack->fields); info->type_stack->fields =3D NULL; =20 if (info->type_stack->methods !=3D NULL) { - strcat (buf, info->type_stack->methods); + out =3D stpcpy (out, info->type_stack->methods); free (info->type_stack->methods); info->type_stack->methods =3D NULL; } =20 - strcat (buf, ";"); + out =3D stpcpy (out, ";"); =20 if (info->type_stack->vtable !=3D NULL) { - strcat (buf, info->type_stack->vtable); + out =3D stpcpy (out, info->type_stack->vtable); free (info->type_stack->vtable); info->type_stack->vtable =3D NULL; }