From patchwork Thu Aug 3 11:47:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 130517 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp1090173vqx; Thu, 3 Aug 2023 04:50:23 -0700 (PDT) X-Google-Smtp-Source: APBJJlFJ85JhrDJvEzN/jR6cpfORRVx8tCtKiXcqGrYgkGqnrnR/Hqya/H10sDWhJlOJ/IbUPRFF X-Received: by 2002:aa7:d311:0:b0:522:4dd0:de6e with SMTP id p17-20020aa7d311000000b005224dd0de6emr7539494edq.8.1691063422958; Thu, 03 Aug 2023 04:50:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691063422; cv=none; d=google.com; s=arc-20160816; b=V25JQYpeonuW5pnqhnnr4M4bV42YCmYxHog3bE9LVgmkogvhxrAxW5DyJ6oIFSr/5a BMIXTSY8+nQzvD0BhnPd9Is4Dam2dCfkW5aA1QSIhv3nUOPmZzj12E99cVGnHiS+YKMY qF5uQEdm5EbulbtnFbEmFx8MA0xt66Btls2U7I27xyLiuAhDry8RzE5C4i/flpUhIFMX DmcKRdAkHe8cEwKF+My4MOvpouvRxerPiWyRoAXqm+EKYjbe1cXReZw57yOfqsPLWgkz tzkrWGBNjqviFwoyGl+AZadg/U5PuPCkesFESkpQh/HyRnTyOLgDMyReVPGFAGNfC37J NpOQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=PTeD5zyorHfwADOb1efgE5h7En/rw4XNUKKDrrPpIgM=; fh=HNIqzTA9Dp00ovCAyCdXfuEvWDT00OlFxFRPLybXRis=; b=vFP6ai5eQYGa8knoY2LDqfYBhibnO2Fc6eUf5HmdRykESYgEtcBsM5+x45VDVuKU7r 1QjPdHA+Mp+1bIIaUKerYaW4qC8I3TMxlNQmeL7pEx2zdsJ/uRDGrLETKYHz8EPhtD2x t19dzbBwv2yubU0VCOLrEI3KvBKSc9xVLbaAGOFAqMUdmh7B5o1VRfVBYDbKYgRj0G4p T2ikJfhG27h5XydoeulHQxr5s07iwkQ/vSdeT8yrvYTw/ywLPGowjMGiZ/EPtg35SJG7 NYu8pxLpgvVFsitcE7sKDgA5epOUldbRXgNEZOGLqb7nPL73um4wXsAgybO5UIj1BZ3j HsyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=A4Rf3YsQ; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id d8-20020a056402078800b0052279c57c79si430945edy.341.2023.08.03.04.50.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Aug 2023 04:50:22 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=A4Rf3YsQ; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DA242385C6DB for ; Thu, 3 Aug 2023 11:48:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DA242385C6DB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1691063288; bh=PTeD5zyorHfwADOb1efgE5h7En/rw4XNUKKDrrPpIgM=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=A4Rf3YsQaCCNjWooWT37BqTaBIoGiS6LcWKBA+tYqCRToHzTjguOrq/rlxxd05tya LGEKSHO3rDrgJ96utrPdD63jkJoGT5lfsT6bqXJGOzQpPRJ/yft9JQdcQm2vDi1IzW /jjC8tIpm3S78q2B6LWoLRg01Pzl74NaK7CyfKbA= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id EAA87385B800 for ; Thu, 3 Aug 2023 11:47:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EAA87385B800 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1b8b2886364so5997495ad.0 for ; Thu, 03 Aug 2023 04:47:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691063260; x=1691668060; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=PTeD5zyorHfwADOb1efgE5h7En/rw4XNUKKDrrPpIgM=; b=exP6Ie2fr4T7MOmqTWtKIL4WN6XFTNldKm/Mha+dd97zttnAEYno6U5jN3ldeFqCCe yjFTGV/t1QU4LrwQapIuzUJl7w6B5IUh+lOVcv2gLPBb9+wA7G71Rs/zvPviYBba72w+ fE4/ts1w5MlYqChiRKe8IiPzbzvajNOpPnp66YQIKkxXQJg7zcQHaEj/xZ7dZTQ/fbVE 149Rby7/qtoX0Swdh6QnZqcazgSA+at5GTSsONLE8cDfp/cG+O2IC3tY/hJvI3jDubQX oemSNnZNi1B6UrVSbYPNSCii1BgiGWlssDA1qZGKsldoR4r0l02l7ILcE2fdIl9yzcAx fcGw== X-Gm-Message-State: ABy/qLZNlrcHL6FENXmtpWRz5++wJyX37bRcZIZILvuqjhz9GiX7XkiF y7l2kAvCloPwG5Orjtlt8AqJzVNPbJ0= X-Received: by 2002:a17:903:2301:b0:1b8:9f6a:39de with SMTP id d1-20020a170903230100b001b89f6a39demr18019568plh.65.1691063260305; Thu, 03 Aug 2023 04:47:40 -0700 (PDT) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id jj19-20020a170903049300b001b694140d96sm14206431plb.170.2023.08.03.04.47.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Aug 2023 04:47:39 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 6F43811427AD; Thu, 3 Aug 2023 21:17:37 +0930 (ACST) Date: Thu, 3 Aug 2023 21:17:37 +0930 To: binutils@sourceware.org Subject: binutils sprintf optimisation Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3034.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773208519735654833 X-GMAIL-MSGID: 1773208519735654833 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. * 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 --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 *prefix, int end_prefix) + strlen (EXECUTABLE_SUFFIX) #endif + 10); - strcpy (cmd, prefix); + memcpy (cmd, prefix, end_prefix); - sprintf (cmd + end_prefix, "%s", prog_name); + strcpy (cmd + end_prefix, prog_name); if (strchr (cmd, '/') != 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); - sprintf (cmd + end_prefix, "%s", progname); + strcpy (cmd + end_prefix, progname); if (strchr (cmd, '/') != 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 end_prefix, struct stat s; const char *fnquotes = (filename_need_quotes (filename) ? "\"" : ""); - strcpy (cmd, prefix); + memcpy (cmd, prefix, end_prefix); - sprintf (cmd + end_prefix, "%s", DEFAULT_PREPROCESSOR_CMD); + char *out = stpcpy (cmd + end_prefix, DEFAULT_PREPROCESSOR_CMD); if ( #if defined (__DJGPP__) || defined (__CYGWIN__) || defined (_WIN32) @@ -409,13 +409,13 @@ look_for_default (char *cmd, const char *prefix, int end_prefix, if (filename_need_quotes (cmd)) { - char *cmd_copy = xmalloc (strlen (cmd)); - strcpy (cmd_copy, cmd); - sprintf (cmd, "\"%s\"", cmd_copy); - free (cmd_copy); + memmove (cmd + 1, cmd, out - cmd); + cmd[0] = '"'; + out++; + *out++ = '"'; } - sprintf (cmd + strlen (cmd), " %s %s %s%s%s", + sprintf (out, " %s %s %s%s%s", DEFAULT_PREPROCESSOR_ARGS, preprocargs, fnquotes, filename, fnquotes); 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]; - sprintf (buffer, (radix == decimal ? "%" PRIu64 - : radix == octal ? "0%" PRIo64 : "0x%" PRIx64), - (uint64_t) num); - - return strlen (buffer); + return sprintf (buffer, (radix == decimal ? "%" PRIu64 + : radix == octal ? "0%" PRIo64 : "0x%" PRIx64), + (uint64_t) num); } 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_handle *info, 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_handle *info, volatile_prefix = volatilep ? "V" : ""; if (len == 0) - sprintf (buf, "__%s%s", const_prefix, volatile_prefix); + buf_len = sprintf (buf, "__%s%s", const_prefix, volatile_prefix); else if (tagname != NULL && strchr (tagname, '<') != NULL) { /* Template methods are fully mangled. */ - sprintf (buf, "__%s%s", const_prefix, volatile_prefix); + buf_len = sprintf (buf, "__%s%s", const_prefix, volatile_prefix); tagname = NULL; len = 0; } else - sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len); + buf_len = sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len); mangled_name_len = ((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 = convert_unicode_to_ACP (n->lang->sval); /* Prepare filename for binary output. */ - filename = xmalloc (strlen (nd) + 4 + 1 + strlen (mcset_mc_basename) + 1 + strlen (mcset_rc_dir)); - strcpy (filename, mcset_rc_dir); + filename = xmalloc (strlen (nd) + 4 + 1 + strlen (mcset_mc_basename) + + 1 + strlen (mcset_rc_dir)); + char *out = filename; + out = stpcpy (out, mcset_rc_dir); if (mcset_prefix_bin) - sprintf (filename + strlen (filename), "%s_", mcset_mc_basename); - strcat (filename, nd); - strcat (filename, ".bin"); + out += sprintf (out, "%s_", mcset_mc_basename); + out = stpcpy (out, nd); + out = stpcpy (out, ".bin"); /* 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 unsignedp) cache[size - 1] = tindex; - sprintf (buf, "%ld=r%ld;", tindex, tindex); + int len = sprintf (buf, "%ld=r%ld;", tindex, tindex); if (unsignedp) { - strcat (buf, "0;"); + strcpy (buf + len, "0;"); + len += 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 == sizeof (long)) - strcat (buf, "-1;"); + strcpy (buf + len, "-1;"); else if (size == 8) - strcat (buf, "01777777777777777777777;"); + strcpy (buf + len, "01777777777777777777777;"); else abort (); } else { if (size <= 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 == 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 += strlen (*pn) + 22; buf = xmalloc (len); - + char *out = buf; if (tag == NULL) - strcpy (buf, "e"); + out = stpcpy (out, "e"); else { tindex = info->type_index; ++info->type_index; - sprintf (buf, "%s:T%ld=e", tag, tindex); + out += sprintf (out, "%s:T%ld=e", tag, tindex); } for (pn = names, pv = vals; *pn != NULL; pn++, pv++) - sprintf (buf + strlen (buf), "%s:%ld,", *pn, (long) *pv); - strcat (buf, ";"); + out += sprintf (out, "%s:%ld,", *pn, (long) *pv); + strcpy (out, ";"); if (tag == NULL) { @@ -1031,12 +1033,9 @@ stab_array_type (void *p, bfd_signed_vma low, bfd_signed_vma high, element = stab_pop_type (info); buf = xmalloc (strlen (range) + strlen (element) + 70); - + char *out = buf; if (! stringp) - { - tindex = 0; - *buf = '\0'; - } + tindex = 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_signed_vma high, tindex = info->type_index; ++info->type_index; definition = true; - sprintf (buf, "%ld=@S;", tindex); + out += sprintf (out, "%ld=@S;", tindex); } - 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) s = stab_pop_type (info); buf = xmalloc (strlen (s) + 26); - + char *out = buf; if (! bitstringp) - { - *buf = '\0'; - tindex = 0; - } + tindex = 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 = info->type_index; ++info->type_index; definition = true; - sprintf (buf, "%ld=@S;", tindex); + out += sprintf (out, "%ld=@S;", tindex); } - sprintf (buf + strlen (buf), "S%s", s); + sprintf (out, "S%s", s); free (s); return stab_push_string (info, buf, tindex, definition, 0); @@ -1304,11 +1300,11 @@ stab_start_struct_type (void *p, const char *tag, unsigned int id, long tindex; bool definition; char buf[40]; + char *out = buf; if (id == 0) { tindex = 0; - *buf = '\0'; definition = false; } else @@ -1317,11 +1313,11 @@ stab_start_struct_type (void *p, const char *tag, unsigned int id, &size); if (tindex < 0) return false; - sprintf (buf, "%ld=", tindex); + out += sprintf (out, "%ld=", tindex); definition = true; } - sprintf (buf + strlen (buf), "%c%u", + sprintf (out, "%c%u", structp ? 's' : 'u', size); @@ -1699,19 +1695,21 @@ stab_class_method_var (struct stab_write_handle *info, const char *physname, else typec = '*'; + size_t cur_len = strlen (info->type_stack->methods); info->type_stack->methods = - 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 = info->type_stack->methods + cur_len; + out += sprintf (out, "%s:%s;%c%c%c", type, physname, visc, qualc, typec); free (type); if (contextp) { - sprintf (info->type_stack->methods + strlen (info->type_stack->methods), - "%ld;%s;", (long) voffset, context); + sprintf (out, "%ld;%s;", (long) voffset, context); free (context); } @@ -1800,36 +1798,36 @@ stab_end_class_type (void *p) buf = xmalloc (len); - strcpy (buf, info->type_stack->string); + char *out = stpcpy (buf, info->type_stack->string); if (info->type_stack->baseclasses != NULL) { - sprintf (buf + strlen (buf), "!%u,", i); + out += sprintf (out, "!%u,", i); for (i = 0; info->type_stack->baseclasses[i] != NULL; i++) { - strcat (buf, info->type_stack->baseclasses[i]); + out = stpcpy (out, info->type_stack->baseclasses[i]); free (info->type_stack->baseclasses[i]); } free (info->type_stack->baseclasses); info->type_stack->baseclasses = NULL; } - strcat (buf, info->type_stack->fields); + out = stpcpy (out, info->type_stack->fields); free (info->type_stack->fields); info->type_stack->fields = NULL; if (info->type_stack->methods != NULL) { - strcat (buf, info->type_stack->methods); + out = stpcpy (out, info->type_stack->methods); free (info->type_stack->methods); info->type_stack->methods = NULL; } - strcat (buf, ";"); + out = stpcpy (out, ";"); if (info->type_stack->vtable != NULL) { - strcat (buf, info->type_stack->vtable); + out = stpcpy (out, info->type_stack->vtable); free (info->type_stack->vtable); info->type_stack->vtable = NULL; }