diff mbox

[20/39] netfilter: ipset: Use kmalloc() in comment extension helper

Message ID 1479075933-4491-21-git-send-email-pablo@netfilter.org
State Accepted, archived
Delegated to: David Miller
Headers show

Commit Message

Pablo Neira Ayuso Nov. 13, 2016, 10:25 p.m. UTC
From: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>

Allocate memory with kmalloc() rather than kzalloc(): the string
is immediately initialized so it is unnecessary to zero out
the allocated memory area.

Ported from a patch proposed by Sergey Popovich <popovich_sergei@mail.ua>.

Suggested-by: Sergey Popovich <popovich_sergei@mail.ua>
Signed-off-by: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
---
 include/linux/netfilter/ipset/ip_set_comment.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

David Laight Nov. 15, 2016, 10:48 a.m. UTC | #1
From: Pablo Neira Ayuso
> Sent: 13 November 2016 22:25
> Allocate memory with kmalloc() rather than kzalloc(): the string
> is immediately initialized so it is unnecessary to zero out
> the allocated memory area.
> 
> Ported from a patch proposed by Sergey Popovich <popovich_sergei@mail.ua>.
> 
> Suggested-by: Sergey Popovich <popovich_sergei@mail.ua>
> Signed-off-by: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
> ---
>  include/linux/netfilter/ipset/ip_set_comment.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/include/linux/netfilter/ipset/ip_set_comment.h
> b/include/linux/netfilter/ipset/ip_set_comment.h
> index bae5c7609be2..5444b1bbe656 100644
> --- a/include/linux/netfilter/ipset/ip_set_comment.h
> +++ b/include/linux/netfilter/ipset/ip_set_comment.h
> @@ -34,7 +34,7 @@ ip_set_init_comment(struct ip_set_comment *comment,
>  		return;
>  	if (unlikely(len > IPSET_MAX_COMMENT_SIZE))
>  		len = IPSET_MAX_COMMENT_SIZE;
> -	c = kzalloc(sizeof(*c) + len + 1, GFP_ATOMIC);
> +	c = kmalloc(sizeof(*c) + len + 1, GFP_ATOMIC);
>  	if (unlikely(!c))
>  		return;
>  	strlcpy(c->str, ext->comment, len + 1);

I think I'd do a memcpy() and zero the last byte.

Note that any other parts of 'c->' are no longer zeroed by this code fragment.

	David
diff mbox

Patch

diff --git a/include/linux/netfilter/ipset/ip_set_comment.h b/include/linux/netfilter/ipset/ip_set_comment.h
index bae5c7609be2..5444b1bbe656 100644
--- a/include/linux/netfilter/ipset/ip_set_comment.h
+++ b/include/linux/netfilter/ipset/ip_set_comment.h
@@ -34,7 +34,7 @@  ip_set_init_comment(struct ip_set_comment *comment,
 		return;
 	if (unlikely(len > IPSET_MAX_COMMENT_SIZE))
 		len = IPSET_MAX_COMMENT_SIZE;
-	c = kzalloc(sizeof(*c) + len + 1, GFP_ATOMIC);
+	c = kmalloc(sizeof(*c) + len + 1, GFP_ATOMIC);
 	if (unlikely(!c))
 		return;
 	strlcpy(c->str, ext->comment, len + 1);