Commit: Use memmove in place of strcpy

Nick Clifton nickc@redhat.com
Tue Mar 16 14:02:31 GMT 2021


Hi Guys,

  The static analyzer has thrown up another potential error:
  
    pe-dll.c:3035: overlapping_buffer: The source buffer
    "lname + 1" potentially overlaps with the destination
    buffer "lname", which results in undefined behavior
    for "strcpy". 

  So I am applying the patch below to fix the problem.

Cheers
  Nick

ld/ChangeLog
2021-03-16  Nick Clifton  <nickc@redhat.com>

	* pe-dll.c (pe_find_cdecl_alias_match): Use memmove to overwrite
	lname string.

diff --git a/ld/pe-dll.c b/ld/pe-dll.c
index eaecb951ef..7aba09cd35 100644
--- a/ld/pe-dll.c
+++ b/ld/pe-dll.c
@@ -3039,7 +3039,9 @@ pe_find_cdecl_alias_match (struct bfd_link_info *linfo, char *name)
          if (pe_details->underscored)
            lname[0] = '_';
          else
-           strcpy (lname, lname + 1);
+           /* Use memmove rather than strcpy as that
+              can handle overlapping buffers.  */
+           memmove (lname, lname + 1, strlen (lname));
          key.key = lname;
          kv = bsearch (&key, udef_table, undef_count,
                        sizeof (struct key_value), undef_sort_cmp);



More information about the Binutils mailing list