-Wstringop-truncation warnings

Alan Modra amodra@gmail.com
Mon May 7 02:23:00 GMT 2018


On Sun, May 06, 2018 at 05:15:22PM -0700, H.J. Lu wrote:
> On Fri, May 4, 2018 at 2:32 AM, Alan Modra <amodra@gmail.com> wrote:
> > index fb1696afff..6ca547ce5b 100644
> > --- a/bfd/cofflink.c
> > +++ b/bfd/cofflink.c
> > @@ -1839,7 +1839,7 @@ _bfd_coff_link_input_bfd (struct coff_final_link_info *flaginfo, bfd *input_bfd)
> >
> >             case C_FCN:
> >               if (obj_pe (input_bfd)
> > -                 && strcmp (isym.n_name, ".bf") != 0
> > +                 && strncmp (isym.n_name, ".bf", sizeof isym.n_name) != 0
> >                   && isym.n_scnum > 0)
> >                 {
> >                   /* For PE, .lf and .ef get their value left alone,
> 
> If this is truly non-string, can memcmp be used instead?

Yes, good idea.  That would avoid avoid
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85623 for these strncpy
calls.  I'll commit the following after running my usual tests, and
fix the missing ChangeLog entry for cofflink.c.

	* cofflink.c (_bfd_coff_link_input_bfd): Use memcmp rather than
	strncmp when checking for ".bf" special symbol.
	* prXXigen.c (_bfd_XXi_swap_scnhdr_out): Make pe_required_section_flags
	section_name a char array, remove sentinal known_sections entry,
	and adjust loop over known_sections to suit.  Use memcmp rather
	than strncmp.

diff --git a/bfd/cofflink.c b/bfd/cofflink.c
index 6ca547ce5b..2f73f72e31 100644
--- a/bfd/cofflink.c
+++ b/bfd/cofflink.c
@@ -1839,7 +1839,7 @@ _bfd_coff_link_input_bfd (struct coff_final_link_info *flaginfo, bfd *input_bfd)
 
 	    case C_FCN:
 	      if (obj_pe (input_bfd)
-		  && strncmp (isym.n_name, ".bf", sizeof isym.n_name) != 0
+		  && memcmp (isym.n_name, ".bf", sizeof ".bf") != 0
 		  && isym.n_scnum > 0)
 		{
 		  /* For PE, .lf and .ef get their value left alone,
diff --git a/bfd/peXXigen.c b/bfd/peXXigen.c
index a62d126aff..5e0acc4571 100644
--- a/bfd/peXXigen.c
+++ b/bfd/peXXigen.c
@@ -1018,7 +1018,7 @@ _bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out)
 
     typedef struct
     {
-      const char *	section_name;
+      char section_name[SCNNMLEN];
       unsigned long	must_have;
     }
     pe_required_section_flags;
@@ -1037,7 +1037,6 @@ _bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out)
 	{ ".text" , IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE },
 	{ ".tls",   IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
 	{ ".xdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
-	{ NULL, 0}
       };
 
     pe_required_section_flags * p;
@@ -1050,11 +1049,12 @@ _bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out)
        by ld --enable-auto-import (if auto-import is actually needed),
        by ld --omagic, or by obcopy --writable-text.  */
 
-    for (p = known_sections; p->section_name; p++)
-      if (strncmp (scnhdr_int->s_name, p->section_name,
-		   sizeof scnhdr_int->s_name) == 0)
+    for (p = known_sections;
+	 p < known_sections + ARRAY_SIZE (known_sections);
+	 p++)
+      if (memcmp (scnhdr_int->s_name, p->section_name, SCNNMLEN) == 0)
 	{
-	  if (strncmp (scnhdr_int->s_name, ".text", sizeof scnhdr_int->s_name)
+	  if (memcmp (scnhdr_int->s_name, ".text", sizeof ".text")
 	      || (bfd_get_file_flags (abfd) & WP_TEXT))
 	    scnhdr_int->s_flags &= ~IMAGE_SCN_MEM_WRITE;
 	  scnhdr_int->s_flags |= p->must_have;
@@ -1067,7 +1067,7 @@ _bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out)
   if (coff_data (abfd)->link_info
       && ! bfd_link_relocatable (coff_data (abfd)->link_info)
       && ! bfd_link_pic (coff_data (abfd)->link_info)
-      && strncmp (scnhdr_int->s_name, ".text", sizeof scnhdr_int->s_name) == 0)
+      && memcmp (scnhdr_int->s_name, ".text", sizeof ".text") == 0)
     {
       /* By inference from looking at MS output, the 32 bit field
 	 which is the combination of the number_of_relocs and



More information about the Binutils mailing list