[commit] [patch] Fix gcc-4.7 memset zero-length warning

Jan Kratochvil jan.kratochvil@redhat.com
Sat Feb 11 15:13:00 GMT 2012


Hi Nick,

On Sat, 11 Feb 2012 15:43:40 +0100, nick clifton wrote:
> >     if (length>  (size_t) sizeof (r.module_name))
> >       length = sizeof (r.module_name);
> > +  else
> > +    (void) memset (r.module_name + length, ' ',
> > +		   sizeof (r.module_name) - length);
> > 
> >     (void) memcpy (r.module_name, abfd->filename, length);
> > -  (void) memset (r.module_name + length, ' ', sizeof (r.module_name) - length);
> 
> If length == sizeof (r.module_name) then you still have a call to memset
> with a length of zero.

I agree; still memset (p, x, 0) is valid and GCC did not complain - which was
the goal.


> May I suggest this instead:

Used this, checked it in.


> You could also just bypass all of this code and just use sprintf
> instead.  Eg:
> 
>   size_t namelen = sizeof (r.module_name);
> 
>   sprintf ((char *) r.module_name, "%*.*s", - namelen, length > namelen
> ? length : namelen, abfd->filename);

It writes out a whole packet, not just the string; I did not see there the
shortcut now, sorry.


Thanks,
Jan


http://sourceware.org/ml/binutils-cvs/2012-02/msg00045.html

--- src/bfd/ChangeLog	2012/02/10 11:24:44	1.5605
+++ src/bfd/ChangeLog	2012/02/11 15:10:10	1.5606
@@ -1,3 +1,9 @@
+2012-02-11  Jan Kratochvil  <jan.kratochvil@redhat.com>
+	    Nick Clifton  <nickc@redhat.com>
+
+	* oasys.c (oasys_write_header): Fix compilation warning on zero-sized
+	memset.
+
 2012-02-10  Iain Sandoe  <idsandoe@googlemail.com>
 
 	* mach-o.c (bfd_mach_o_build_seg_command): Count zerofill section
--- src/bfd/oasys.c	2011/07/11 15:03:07	1.47
+++ src/bfd/oasys.c	2012/02/11 15:10:12	1.48
@@ -906,9 +906,11 @@
 
   if (length > (size_t) sizeof (r.module_name))
     length = sizeof (r.module_name);
+  else if (length < (size_t) sizeof (r.module_name))
+    (void) memset (r.module_name + length, ' ',
+		   sizeof (r.module_name) - length);
 
   (void) memcpy (r.module_name, abfd->filename, length);
-  (void) memset (r.module_name + length, ' ', sizeof (r.module_name) - length);
 
   r.version_number = OASYS_VERSION_NUMBER;
   r.rev_number = OASYS_REV_NUMBER;



More information about the Binutils mailing list