This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
Re: strcpy -> memcpy
- From: Elias Athanasopoulos <eathan at otenet dot gr>
- To: Alan Modra <amodra at bigpond dot net dot au>
- Cc: binutils at sources dot redhat dot com
- Date: Tue, 25 Jun 2002 19:51:16 +0300
- Subject: Re: strcpy -> memcpy
- References: <20020625093130.GH22093@bubble.sa.bigpond.net.au>
Hi Alan,
On Tue, Jun 25, 2002 at 07:01:30PM +0930, Alan Modra wrote:
> Many places in bfd, we do something like
>
> p = malloc (strlen (s) + 1);
> if (!p)
> return false;
> strcpy (p, s);
>
> which is a little inefficient since strcpy will need to check for the
> terminating 0 or call strlen itself. This patch changes the above to
>
> len = strlen (s) + 1;
> p = malloc (len);
> if (!p)
> return false;
> memcpy (p, s, len);
I fixed some similar cases in gas. You might want to include them.
Elias
ChangeLog/gas
2002-06-25 Elias Athanasopoulos <eathan@otenet.gr>
* ecoff.c (get_tag): Replace strcpy with memcpy.
(ecoff_directive_def): Likewise.
(ecoff_directive_tag): Likewise.
* listing.c (file_info): Likewise.
* hash.c (what): Likewise.
--- ecoff.c.orig Tue Jun 25 19:17:30 2002
+++ ecoff.c Tue Jun 25 19:39:29 2002
@@ -2036,9 +2036,13 @@
if (hash_ptr == (shash_t *) NULL)
{
char *perm;
+ size_t len;
- perm = xmalloc ((unsigned long) (strlen (tag) + 1));
- strcpy (perm, tag);
+ len = strlen (tag) + 1;
+ perm = xmalloc (len);
+ if (!perm)
+ return false;
+ memcpy (perm, tag, len);
hash_ptr = allocate_shash ();
err = hash_insert (tag_hash, perm, (char *) hash_ptr);
if (err)
@@ -2541,12 +2545,19 @@
as_warn (_("empty symbol name in .def; ignored"));
else
{
+ size_t len;
+
if (coff_sym_name != (char *) NULL)
free (coff_sym_name);
if (coff_tag != (char *) NULL)
free (coff_tag);
- coff_sym_name = (char *) xmalloc ((unsigned long) (strlen (name) + 1));
- strcpy (coff_sym_name, name);
+
+ len = strlen(name) + 1;
+ coff_sym_name = xmalloc (len);
+ if (!coff_sym_name)
+ return false;
+ memcpy (coff_sym_name, name, len);
+
coff_type = type_info_init;
coff_storage_class = sc_Nil;
coff_symbol_typ = st_Nil;
@@ -2767,6 +2778,7 @@
{
char *name;
char name_end;
+ size_t len;
if (coff_sym_name == (char *) NULL)
{
@@ -2778,8 +2790,11 @@
name = input_line_pointer;
name_end = get_symbol_end ();
- coff_tag = (char *) xmalloc ((unsigned long) (strlen (name) + 1));
- strcpy (coff_tag, name);
+ len = strlen (name) + 1;
+ coff_tag = xmalloc (len);
+ if (!coff_tag)
+ return false;
+ memcpy (coff_tag, name, len);
*input_line_pointer = name_end;
--- listing.c.orig Tue Jun 25 19:28:29 2002
+++ listing.c Tue Jun 25 19:28:32 2002
@@ -254,6 +254,7 @@
file_info (file_name)
const char *file_name;
{
+ size_t len;
/* Find an entry with this file name. */
file_info_type *p = file_info_head;
@@ -269,8 +270,11 @@
p = (file_info_type *) xmalloc (sizeof (file_info_type));
p->next = file_info_head;
file_info_head = p;
- p->filename = xmalloc ((unsigned long) strlen (file_name) + 1);
- strcpy (p->filename, file_name);
+ len = strlen (file_name) + 1;
+ p->filename = xmalloc (len);
+ if (!p->filename)
+ return false;
+ memcpy (p->filename, file_name, len);
p->pos = 0;
p->linenum = 0;
p->at_end = 0;
--- hash.c.orig Tue Jun 25 19:35:20 2002
+++ hash.c Tue Jun 25 19:35:23 2002
@@ -547,17 +547,18 @@
char *description;
{
char *retval;
- char *malloc ();
+ size_t len;
printf (" %s : ", description);
gets (answer);
/* Will one day clean up answer here. */
- retval = malloc (strlen (answer) + 1);
+ len = strlen (answer) + 1;
+ retval = xmalloc (len);
if (!retval)
{
error ("room");
}
- (void) strcpy (retval, answer);
+ memcpy (retval, answer, len);
return (retval);
}