This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: [patch] Fix gcc-4.7 memset zero-length warning
- From: nick clifton <nickc at redhat dot com>
- To: Jan Kratochvil <jan dot kratochvil at redhat dot com>
- Cc: binutils at sourceware dot org
- Date: Sat, 11 Feb 2012 14:43:40 +0000
- Subject: Re: [patch] Fix gcc-4.7 memset zero-length warning
- References: <20120209171126.GA2678@host2.jankratochvil.net>
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