Skip to content

Commit ad923de

Browse files
committed
binutils sprintf optimisation
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.
1 parent 75747be commit ad923de

File tree

7 files changed

+68
-70
lines changed

7 files changed

+68
-70
lines changed

binutils/dlltool.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4188,9 +4188,9 @@ look_for_prog (const char *prog_name, const char *prefix, int end_prefix)
41884188
+ strlen (EXECUTABLE_SUFFIX)
41894189
#endif
41904190
+ 10);
4191-
strcpy (cmd, prefix);
4191+
memcpy (cmd, prefix, end_prefix);
41924192

4193-
sprintf (cmd + end_prefix, "%s", prog_name);
4193+
strcpy (cmd + end_prefix, prog_name);
41944194

41954195
if (strchr (cmd, '/') != NULL)
41964196
{

binutils/dllwrap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ look_for_prog (const char *progname, const char *prefix, int end_prefix)
186186
+ strlen (EXECUTABLE_SUFFIX)
187187
#endif
188188
+ 10);
189-
strcpy (cmd, prefix);
189+
memcpy (cmd, prefix, end_prefix);
190190

191-
sprintf (cmd + end_prefix, "%s", progname);
191+
strcpy (cmd + end_prefix, progname);
192192

193193
if (strchr (cmd, '/') != NULL)
194194
{

binutils/resrc.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,9 @@ look_for_default (char *cmd, const char *prefix, int end_prefix,
383383
struct stat s;
384384
const char *fnquotes = (filename_need_quotes (filename) ? "\"" : "");
385385

386-
strcpy (cmd, prefix);
386+
memcpy (cmd, prefix, end_prefix);
387387

388-
sprintf (cmd + end_prefix, "%s", DEFAULT_PREPROCESSOR_CMD);
388+
char *out = stpcpy (cmd + end_prefix, DEFAULT_PREPROCESSOR_CMD);
389389

390390
if (
391391
#if defined (__DJGPP__) || defined (__CYGWIN__) || defined (_WIN32)
@@ -409,13 +409,13 @@ look_for_default (char *cmd, const char *prefix, int end_prefix,
409409

410410
if (filename_need_quotes (cmd))
411411
{
412-
char *cmd_copy = xmalloc (strlen (cmd));
413-
strcpy (cmd_copy, cmd);
414-
sprintf (cmd, "\"%s\"", cmd_copy);
415-
free (cmd_copy);
412+
memmove (cmd + 1, cmd, out - cmd);
413+
cmd[0] = '"';
414+
out++;
415+
*out++ = '"';
416416
}
417417

418-
sprintf (cmd + strlen (cmd), " %s %s %s%s%s",
418+
sprintf (out, " %s %s %s%s%s",
419419
DEFAULT_PREPROCESSOR_ARGS, preprocargs, fnquotes, filename, fnquotes);
420420

421421
if (verbose)

binutils/size.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,9 @@ size_number (bfd_size_type num)
441441
{
442442
char buffer[40];
443443

444-
sprintf (buffer, (radix == decimal ? "%" PRIu64
445-
: radix == octal ? "0%" PRIo64 : "0x%" PRIx64),
446-
(uint64_t) num);
447-
448-
return strlen (buffer);
444+
return sprintf (buffer, (radix == decimal ? "%" PRIu64
445+
: radix == octal ? "0%" PRIo64 : "0x%" PRIx64),
446+
(uint64_t) num);
449447
}
450448

451449
static void

binutils/stabs.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,7 +3030,7 @@ parse_stab_argtypes (void *dhandle, struct stab_handle *info,
30303030

30313031
if (!(is_destructor || is_full_physname_constructor || is_v3))
30323032
{
3033-
unsigned int len;
3033+
unsigned int len, buf_len;
30343034
const char *const_prefix;
30353035
const char *volatile_prefix;
30363036
char buf[20];
@@ -3042,19 +3042,19 @@ parse_stab_argtypes (void *dhandle, struct stab_handle *info,
30423042
volatile_prefix = volatilep ? "V" : "";
30433043

30443044
if (len == 0)
3045-
sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
3045+
buf_len = sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
30463046
else if (tagname != NULL && strchr (tagname, '<') != NULL)
30473047
{
30483048
/* Template methods are fully mangled. */
3049-
sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
3049+
buf_len = sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
30503050
tagname = NULL;
30513051
len = 0;
30523052
}
30533053
else
3054-
sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
3054+
buf_len = sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
30553055

30563056
mangled_name_len = ((is_constructor ? 0 : strlen (fieldname))
3057-
+ strlen (buf)
3057+
+ buf_len
30583058
+ len
30593059
+ strlen (argtypes)
30603060
+ 1);

binutils/windmc.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -746,12 +746,14 @@ write_bin (void)
746746
nd = convert_unicode_to_ACP (n->lang->sval);
747747

748748
/* Prepare filename for binary output. */
749-
filename = xmalloc (strlen (nd) + 4 + 1 + strlen (mcset_mc_basename) + 1 + strlen (mcset_rc_dir));
750-
strcpy (filename, mcset_rc_dir);
749+
filename = xmalloc (strlen (nd) + 4 + 1 + strlen (mcset_mc_basename)
750+
+ 1 + strlen (mcset_rc_dir));
751+
char *out = filename;
752+
out = stpcpy (out, mcset_rc_dir);
751753
if (mcset_prefix_bin)
752-
sprintf (filename + strlen (filename), "%s_", mcset_mc_basename);
753-
strcat (filename, nd);
754-
strcat (filename, ".bin");
754+
out += sprintf (out, "%s_", mcset_mc_basename);
755+
out = stpcpy (out, nd);
756+
out = stpcpy (out, ".bin");
755757

756758
/* Write message file. */
757759
windmc_write_bin (filename, &mc_nodes_lang[i], (c - i));

binutils/wrstabs.c

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -678,27 +678,29 @@ stab_int_type (void *p, unsigned int size, bool unsignedp)
678678

679679
cache[size - 1] = tindex;
680680

681-
sprintf (buf, "%ld=r%ld;", tindex, tindex);
681+
int len = sprintf (buf, "%ld=r%ld;", tindex, tindex);
682682
if (unsignedp)
683683
{
684-
strcat (buf, "0;");
684+
strcpy (buf + len, "0;");
685+
len += 2;
685686
if (size < sizeof (long))
686-
sprintf (buf + strlen (buf), "%ld;", ((long) 1 << (size * 8)) - 1);
687+
sprintf (buf + len, "%ld;", ((long) 1 << (size * 8)) - 1);
687688
else if (size == sizeof (long))
688-
strcat (buf, "-1;");
689+
strcpy (buf + len, "-1;");
689690
else if (size == 8)
690-
strcat (buf, "01777777777777777777777;");
691+
strcpy (buf + len, "01777777777777777777777;");
691692
else
692693
abort ();
693694
}
694695
else
695696
{
696697
if (size <= sizeof (long))
697-
sprintf (buf + strlen (buf), "%ld;%ld;",
698+
sprintf (buf + len, "%ld;%ld;",
698699
(long) - ((unsigned long) 1 << (size * 8 - 1)),
699700
(long) (((unsigned long) 1 << (size * 8 - 1)) - 1));
700701
else if (size == 8)
701-
strcat (buf, "01000000000000000000000;0777777777777777777777;");
702+
strcpy (buf + len,
703+
"01000000000000000000000;0777777777777777777777;");
702704
else
703705
abort ();
704706
}
@@ -828,19 +830,19 @@ stab_enum_type (void *p, const char *tag, const char **names,
828830
len += strlen (*pn) + 22;
829831

830832
buf = xmalloc (len);
831-
833+
char *out = buf;
832834
if (tag == NULL)
833-
strcpy (buf, "e");
835+
out = stpcpy (out, "e");
834836
else
835837
{
836838
tindex = info->type_index;
837839
++info->type_index;
838-
sprintf (buf, "%s:T%ld=e", tag, tindex);
840+
out += sprintf (out, "%s:T%ld=e", tag, tindex);
839841
}
840842

841843
for (pn = names, pv = vals; *pn != NULL; pn++, pv++)
842-
sprintf (buf + strlen (buf), "%s:%ld,", *pn, (long) *pv);
843-
strcat (buf, ";");
844+
out += sprintf (out, "%s:%ld,", *pn, (long) *pv);
845+
strcpy (out, ";");
844846

845847
if (tag == NULL)
846848
{
@@ -1031,23 +1033,20 @@ stab_array_type (void *p, bfd_signed_vma low, bfd_signed_vma high,
10311033
element = stab_pop_type (info);
10321034

10331035
buf = xmalloc (strlen (range) + strlen (element) + 70);
1034-
1036+
char *out = buf;
10351037
if (! stringp)
1036-
{
1037-
tindex = 0;
1038-
*buf = '\0';
1039-
}
1038+
tindex = 0;
10401039
else
10411040
{
10421041
/* We need to define a type in order to include the string
10431042
attribute. */
10441043
tindex = info->type_index;
10451044
++info->type_index;
10461045
definition = true;
1047-
sprintf (buf, "%ld=@S;", tindex);
1046+
out += sprintf (out, "%ld=@S;", tindex);
10481047
}
10491048

1050-
sprintf (buf + strlen (buf), "ar%s;%ld;%ld;%s",
1049+
sprintf (out, "ar%s;%ld;%ld;%s",
10511050
range, (long) low, (long) high, element);
10521051
free (range);
10531052
free (element);
@@ -1073,23 +1072,20 @@ stab_set_type (void *p, bool bitstringp)
10731072

10741073
s = stab_pop_type (info);
10751074
buf = xmalloc (strlen (s) + 26);
1076-
1075+
char *out = buf;
10771076
if (! bitstringp)
1078-
{
1079-
*buf = '\0';
1080-
tindex = 0;
1081-
}
1077+
tindex = 0;
10821078
else
10831079
{
10841080
/* We need to define a type in order to include the string
10851081
attribute. */
10861082
tindex = info->type_index;
10871083
++info->type_index;
10881084
definition = true;
1089-
sprintf (buf, "%ld=@S;", tindex);
1085+
out += sprintf (out, "%ld=@S;", tindex);
10901086
}
10911087

1092-
sprintf (buf + strlen (buf), "S%s", s);
1088+
sprintf (out, "S%s", s);
10931089
free (s);
10941090

10951091
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,
13041300
long tindex;
13051301
bool definition;
13061302
char buf[40];
1303+
char *out = buf;
13071304

13081305
if (id == 0)
13091306
{
13101307
tindex = 0;
1311-
*buf = '\0';
13121308
definition = false;
13131309
}
13141310
else
@@ -1317,11 +1313,11 @@ stab_start_struct_type (void *p, const char *tag, unsigned int id,
13171313
&size);
13181314
if (tindex < 0)
13191315
return false;
1320-
sprintf (buf, "%ld=", tindex);
1316+
out += sprintf (out, "%ld=", tindex);
13211317
definition = true;
13221318
}
13231319

1324-
sprintf (buf + strlen (buf), "%c%u",
1320+
sprintf (out, "%c%u",
13251321
structp ? 's' : 'u',
13261322
size);
13271323

@@ -1699,19 +1695,21 @@ stab_class_method_var (struct stab_write_handle *info, const char *physname,
16991695
else
17001696
typec = '*';
17011697

1698+
size_t cur_len = strlen (info->type_stack->methods);
17021699
info->type_stack->methods =
1703-
xrealloc (info->type_stack->methods,
1704-
(strlen (info->type_stack->methods) + strlen (type)
1705-
+ strlen (physname) + (contextp ? strlen (context) : 0) + 40));
1706-
1707-
sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1708-
"%s:%s;%c%c%c", type, physname, visc, qualc, typec);
1700+
xrealloc (info->type_stack->methods, (cur_len
1701+
+ strlen (type)
1702+
+ strlen (physname)
1703+
+ (contextp ? strlen (context) : 0)
1704+
+ 40));
1705+
1706+
char *out = info->type_stack->methods + cur_len;
1707+
out += sprintf (out, "%s:%s;%c%c%c", type, physname, visc, qualc, typec);
17091708
free (type);
17101709

17111710
if (contextp)
17121711
{
1713-
sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1714-
"%ld;%s;", (long) voffset, context);
1712+
sprintf (out, "%ld;%s;", (long) voffset, context);
17151713
free (context);
17161714
}
17171715

@@ -1800,36 +1798,36 @@ stab_end_class_type (void *p)
18001798

18011799
buf = xmalloc (len);
18021800

1803-
strcpy (buf, info->type_stack->string);
1801+
char *out = stpcpy (buf, info->type_stack->string);
18041802

18051803
if (info->type_stack->baseclasses != NULL)
18061804
{
1807-
sprintf (buf + strlen (buf), "!%u,", i);
1805+
out += sprintf (out, "!%u,", i);
18081806
for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
18091807
{
1810-
strcat (buf, info->type_stack->baseclasses[i]);
1808+
out = stpcpy (out, info->type_stack->baseclasses[i]);
18111809
free (info->type_stack->baseclasses[i]);
18121810
}
18131811
free (info->type_stack->baseclasses);
18141812
info->type_stack->baseclasses = NULL;
18151813
}
18161814

1817-
strcat (buf, info->type_stack->fields);
1815+
out = stpcpy (out, info->type_stack->fields);
18181816
free (info->type_stack->fields);
18191817
info->type_stack->fields = NULL;
18201818

18211819
if (info->type_stack->methods != NULL)
18221820
{
1823-
strcat (buf, info->type_stack->methods);
1821+
out = stpcpy (out, info->type_stack->methods);
18241822
free (info->type_stack->methods);
18251823
info->type_stack->methods = NULL;
18261824
}
18271825

1828-
strcat (buf, ";");
1826+
out = stpcpy (out, ";");
18291827

18301828
if (info->type_stack->vtable != NULL)
18311829
{
1832-
strcat (buf, info->type_stack->vtable);
1830+
out = stpcpy (out, info->type_stack->vtable);
18331831
free (info->type_stack->vtable);
18341832
info->type_stack->vtable = NULL;
18351833
}

0 commit comments

Comments
 (0)