[LEDE-DEV] [PATCH] umdns: Replace unnecessary memset calls with {}.

Matthias Schiffer mschiffer at universe-factory.net
Wed Nov 8 02:13:13 PST 2017


On 11/08/2017 10:57 AM, Zefir Kurtisi wrote:
> On 11/07/2017 09:24 PM, Rosen Penev wrote:
>> Less verbose
>>
>> Signed-off-by: Rosen Penev <rosenp at gmail.com>
>> ---
>>  interface.c | 22 ++++++++--------------
>>  1 file changed, 8 insertions(+), 14 deletions(-)
>>
>> diff --git a/interface.c b/interface.c
>> index 7f814d2..18dee52 100644
>> --- a/interface.c
>> +++ b/interface.c
>> @@ -44,7 +44,7 @@
>>  static int
>>  interface_send_packet4(struct interface *iface, struct sockaddr_in *to, struct iovec *iov, int iov_len)
>>  {
>> -	static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
>> +	static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1] = {};
>>  	static struct sockaddr_in a;
>>  	static struct msghdr m = {
>>  		.msg_name = (struct sockaddr *) &a,
>> @@ -61,7 +61,6 @@ interface_send_packet4(struct interface *iface, struct sockaddr_in *to, struct i
>>  	m.msg_iov = iov;
>>  	m.msg_iovlen = iov_len;
>>  
>> -	memset(cmsg_data, 0, sizeof(cmsg_data));
>>  	cmsg = CMSG_FIRSTHDR(&m);
>>  	cmsg->cmsg_len = m.msg_controllen;
>>  	cmsg->cmsg_level = IPPROTO_IP;
> 
> This one looks like a pitfall to me, since without the memset cmsg_data being
> static is never zeroed again at runtime. Not sure if all cmsg members are
> initialized after the removed memset, otherwise the change is wrong.
> 
> 
> Cheers,
> Zefir

Huh, good catch. But I wonder if there is any reason to make these
variables static at all - stack-allocated should be fine? I see that
re-initialization of the msghdr is avoided, but I don't consider that a
good enough reason, as it makes the code less intuitive. (And if kept
static, it should at least get a comment explaining it.)

Matthias

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <http://lists.infradead.org/pipermail/lede-dev/attachments/20171108/c9ebbf5c/attachment.sig>


More information about the Lede-dev mailing list