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

nick clifton nickc@redhat.com
Sat Feb 11 14:45:00 GMT 2012
Hi Jan,

> error: call to ‘__warn_memset_zero_len

> But in some way it may be more GCC bug.  OK to check it in or not?

Yes and no.  Yes a fix is OK, but no, the one you propose does not quite
work:

>     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.  May I suggest this instead:

>     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);

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);

Cheers
  Nick



More information about the Binutils mailing list