This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] strncpy -Wstringop-truncation in bfd/


Several uses of strncpy in bfd/* do not compile when using -Werror in gcc 8.0.x
because of -Wstringop-truncation.  A typical warning is:

   ../../binutils-gdb/libiberty/dyn-string.c:280:3: warning: ‘strncpy’ output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]

A work-around is to use 'memcpy' instead of 'strncpy'.  This spends
a few more cycles each time; but it compiles, and runs correctly
as long as the character arrays really are strings.  Patch is attached.
diff --git a/bfd/coffcode.h b/bfd/coffcode.h
index 0cb1fc8b4f..841ad15073 100644
--- a/bfd/coffcode.h
+++ b/bfd/coffcode.h
@@ -3737,7 +3737,7 @@ coff_write_object_contents (bfd * abfd)
 
       internal_f.f_nscns++;
 
-      strncpy (section.s_name, current->name, SCNNMLEN);
+      memcpy (section.s_name, current->name, SCNNMLEN);
 
 #ifdef COFF_LONG_SECTION_NAMES
       /* Handle long section names as in PE.  This must be compatible
@@ -3775,7 +3775,7 @@ coff_write_object_contents (bfd * abfd)
 		 buffer, just in case.  */
 	      sprintf (s_name_buf, "/%lu", (unsigned long) string_size);
 	      /* Then strncpy takes care of any padding for us.  */
-	      strncpy (section.s_name, s_name_buf, SCNNMLEN);
+	      memcpy (section.s_name, s_name_buf, SCNNMLEN);
 	      string_size += len + 1;
 	      long_section_names = TRUE;
 	    }
diff --git a/bfd/coffgen.c b/bfd/coffgen.c
index 3d32968022..d9a48e6eca 100644
--- a/bfd/coffgen.c
+++ b/bfd/coffgen.c
@@ -923,7 +923,7 @@ coff_fix_symbol_name (bfd *abfd,
     {
       if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
 	/* This name will fit into the symbol neatly.  */
-	strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
+	memcpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
 
       else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
 	{
diff --git a/bfd/elf-linux-core.h b/bfd/elf-linux-core.h
index 0a5d76fe9d..8aa6b7b9e6 100644
--- a/bfd/elf-linux-core.h
+++ b/bfd/elf-linux-core.h
@@ -69,8 +69,8 @@ swap_linux_prpsinfo32_ugid32_out
   bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
   bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
   bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
-  strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
-  strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
+  memcpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
+  memcpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
 }
 
 /* External 32-bit structure for PRPSINFO.  This structure is
@@ -121,8 +121,8 @@ swap_linux_prpsinfo32_ugid16_out
   bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
   bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
   bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
-  strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
-  strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
+  memcpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
+  memcpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
 }
 
 /* External 64-bit structure for PRPSINFO.  This structure is
@@ -174,8 +174,8 @@ swap_linux_prpsinfo64_ugid32_out
   bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
   bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
   bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
-  strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
-  strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
+  memcpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
+  memcpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
 }
 
 /* External 64-bit structure for PRPSINFO.  This structure is
@@ -227,8 +227,8 @@ swap_linux_prpsinfo64_ugid16_out
   bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
   bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
   bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
-  strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
-  strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
+  memcpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
+  memcpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
 }
 
 #endif
diff --git a/bfd/elf.c b/bfd/elf.c
index 2fb8377274..5f3991484f 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -10524,8 +10524,8 @@ elfcore_write_prpsinfo (bfd  *abfd,
 #endif
 
       memset (&data, 0, sizeof (data));
-      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
-      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
+      memcpy (data.pr_fname, fname, sizeof (data.pr_fname));
+      memcpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
       return elfcore_write_note (abfd, buf, bufsiz,
 				 "CORE", note_type, &data, sizeof (data));
     }
@@ -10541,8 +10541,8 @@ elfcore_write_prpsinfo (bfd  *abfd,
 #endif
 
       memset (&data, 0, sizeof (data));
-      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
-      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
+      memcpy (data.pr_fname, fname, sizeof (data.pr_fname));
+      memcpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
       return elfcore_write_note (abfd, buf, bufsiz,
 				 "CORE", note_type, &data, sizeof (data));
     }
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
index d66d9ba5fc..05c0cc55fc 100644
--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -465,8 +465,8 @@ elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
 	{
 	  prpsinfo32_t data;
 	  memset (&data, 0, sizeof (data));
-	  strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
-	  strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
+	  memcpy (data.pr_fname, fname, sizeof (data.pr_fname));
+	  memcpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
 	  return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
 				     &data, sizeof (data));
 	}
@@ -474,8 +474,8 @@ elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
 	{
 	  prpsinfo64_t data;
 	  memset (&data, 0, sizeof (data));
-	  strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
-	  strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
+	  memcpy (data.pr_fname, fname, sizeof (data.pr_fname));
+	  memcpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
 	  return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
 				     &data, sizeof (data));
 	}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]