netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Cc: davem@davemloft.net, netdev@vger.kernel.org
Subject: [PATCH 06/11] ipvs: Use struct_size() helper
Date: Mon, 18 Feb 2019 00:23:52 +0100	[thread overview]
Message-ID: <20190217232357.29858-7-pablo@netfilter.org> (raw)
In-Reply-To: <20190217232357.29858-1-pablo@netfilter.org>

From: "Gustavo A. R. Silva" <gustavo@embeddedor.com>

One of the more common cases of allocation size calculations is finding
the size of a structure that has a zero-sized array at the end, along
with memory for some number of elements for that array. For example:

struct foo {
    int stuff;
    struct boo entry[];
};

size = sizeof(struct foo) + count * sizeof(struct boo);
instance = alloc(size, GFP_KERNEL)

Instead of leaving these open-coded and prone to type mistakes, we can
now use the new struct_size() helper:

size = struct_size(instance, entry, count);

This code was detected with the help of Coccinelle.

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Acked-by: Simon Horman <horms+renesas@verge.net.au>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 net/netfilter/ipvs/ip_vs_ctl.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/net/netfilter/ipvs/ip_vs_ctl.c b/net/netfilter/ipvs/ip_vs_ctl.c
index 432141f04af3..446beeb5e7b2 100644
--- a/net/netfilter/ipvs/ip_vs_ctl.c
+++ b/net/netfilter/ipvs/ip_vs_ctl.c
@@ -2722,8 +2722,7 @@ do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
 		int size;
 
 		get = (struct ip_vs_get_services *)arg;
-		size = sizeof(*get) +
-			sizeof(struct ip_vs_service_entry) * get->num_services;
+		size = struct_size(get, entrytable, get->num_services);
 		if (*len != size) {
 			pr_err("length: %u != %u\n", *len, size);
 			ret = -EINVAL;
@@ -2764,8 +2763,7 @@ do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
 		int size;
 
 		get = (struct ip_vs_get_dests *)arg;
-		size = sizeof(*get) +
-			sizeof(struct ip_vs_dest_entry) * get->num_dests;
+		size = struct_size(get, entrytable, get->num_dests);
 		if (*len != size) {
 			pr_err("length: %u != %u\n", *len, size);
 			ret = -EINVAL;
-- 
2.11.0


  parent reply	other threads:[~2019-02-17 23:24 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-17 23:23 [PATCH 00/11] Netfilter/IPVS updates for net-next Pablo Neira Ayuso
2019-02-17 23:23 ` [PATCH 01/11] netfilter: nf_tables: add NFTA_RULE_POSITION_ID to nla_policy Pablo Neira Ayuso
2019-02-17 23:23 ` [PATCH 02/11] netfilter: nft_tunnel: Add NFTA_TUNNEL_MODE options Pablo Neira Ayuso
2019-02-17 23:23 ` [PATCH 03/11] netfilter: nat: remove module dependency on ipv6 core Pablo Neira Ayuso
2019-02-17 23:23 ` [PATCH 04/11] netfilter: ipv6: avoid indirect calls for IPV6=y case Pablo Neira Ayuso
2019-02-17 23:23 ` [PATCH 05/11] netfilter: conntrack: fix indentation issue Pablo Neira Ayuso
2019-02-17 23:23 ` Pablo Neira Ayuso [this message]
2019-02-17 23:23 ` [PATCH 07/11] netfilter: xt_recent: Use struct_size() in kvzalloc() Pablo Neira Ayuso
2019-02-17 23:23 ` [PATCH 08/11] netfilter: conntrack: fix cloned unconfirmed skb->_nfct race in __nf_conntrack_confirm Pablo Neira Ayuso
2019-02-17 23:23 ` [PATCH 09/11] netfilter: reject: skip csum verification for protocols that don't support it Pablo Neira Ayuso
2019-02-17 23:23 ` [PATCH 10/11] netfilter: ipt_CLUSTERIP: make symbol 'cip_netdev_notifier' static Pablo Neira Ayuso
2019-02-17 23:23 ` [PATCH 11/11] netfilter: nf_conntrack_sip: add sip_external_media logic Pablo Neira Ayuso
2019-02-18 19:59 ` [PATCH 00/11] Netfilter/IPVS updates for net-next David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190217232357.29858-7-pablo@netfilter.org \
    --to=pablo@netfilter.org \
    --cc=davem@davemloft.net \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).