Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp1052677imj; Thu, 7 Feb 2019 16:47:24 -0800 (PST) X-Google-Smtp-Source: AHgI3Ib5ZyZZofu3QZcQ94YPnBgjCZ+3DaKpraJeAg/UUHwbonB1yCB09UmSdC7x2077753p0a3A X-Received: by 2002:a62:6f49:: with SMTP id k70mr19011233pfc.7.1549586844384; Thu, 07 Feb 2019 16:47:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549586844; cv=none; d=google.com; s=arc-20160816; b=MWJiKQPBpyQ034r9vdbgDgylOUCOLisDf2Bxtnz+fE/TJNxhtj5YmuRh5CdKEXhk/y za1lsD5pLVnuDaIHPJV0HY+WWWHdo0oyvRbb8wjw08Uzgb7Z24ntPgzTANNec7xDDl/c 0MfbAEFNqaf/OniHzNxkVWwV0a+oFnaPQNuxaubQCToHmjcrWpXY+WXPCwWN6VpvPxN8 nCfYnSU7GfznqEpp3FrRSlqWgGAR+o/soGy9HWIeH+ToJ/1XMX+0ISNNzlPC5QsFZJOP c5XNmtfhSgGWHU/3lqt41+qY1aoJajsJCh4/BTGN2D3It/f0/UQROwDh850ugzDJPdFU ttHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date; bh=rR8UkuZnd9tMURp1sZXBykJrcAuKM6+JXkDmG9XXGlU=; b=WmZxJAOWORFqJrzuklXhrZ/xcpnfm9obGc7mTBscWAsxYUyACcTeY2WqMk+Lzd6Neu dwKcs/NV3ZCJHxcmZjKTic86mrhwyHcRwGFNQuZq6nUAZrHoxEZwVhSIu/wsvoVNLTBC 6/XBAPQm6j8o4jNnmp/dT+g5zCL67N49xNjNUTm8sOwusdpCl1rX83IvGzgQv1A2X6ed lMzyRmzOlzMcb0IanVgYI9uy5IziJ4IvBTzMvHi/PqV512TuITBQW8u/yy3P8fRdk25r j+0RtxVfkwGpfPq0nsB6IxPLSTVxs+Fuyryiptn1ijtMaEGxNXPCbDxSHbOpCPtrKaRW g3nA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f1si479932pgq.553.2019.02.07.16.47.08; Thu, 07 Feb 2019 16:47:24 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727068AbfBHApB (ORCPT + 99 others); Thu, 7 Feb 2019 19:45:01 -0500 Received: from gateway20.websitewelcome.com ([192.185.47.18]:20556 "EHLO gateway20.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727018AbfBHAo6 (ORCPT ); Thu, 7 Feb 2019 19:44:58 -0500 Received: from cm10.websitewelcome.com (cm10.websitewelcome.com [100.42.49.4]) by gateway20.websitewelcome.com (Postfix) with ESMTP id C4289400CC7CD for ; Thu, 7 Feb 2019 18:44:57 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id ruHpgURCb2PzOruHpg0YG2; Thu, 07 Feb 2019 18:44:57 -0600 X-Authority-Reason: nr=8 Received: from [189.250.49.191] (port=46362 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1gruHo-0044CX-O0; Thu, 07 Feb 2019 18:44:56 -0600 Date: Thu, 7 Feb 2019 18:44:56 -0600 From: "Gustavo A. R. Silva" To: Wensong Zhang , Simon Horman , Julian Anastasov , Pablo Neira Ayuso , Jozsef Kadlecsik , Florian Westphal , "David S. Miller" Cc: netdev@vger.kernel.org, lvs-devel@vger.kernel.org, netfilter-devel@vger.kernel.org, coreteam@netfilter.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH net-next] ipvs: Use struct_size() helper Message-ID: <20190208004456.GA15845@embeddedor> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator4166.hostgator.com X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - embeddedor.com X-BWhitelist: no X-Source-IP: 189.250.49.191 X-Source-L: No X-Exim-ID: 1gruHo-0044CX-O0 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.49.191]:46362 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 24 X-Source-Cap: Z3V6aWRpbmU7Z3V6aWRpbmU7Z2F0b3I0MTY2Lmhvc3RnYXRvci5jb20= X-Local-Domain: yes Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 7d6318664eb2..bcd9112f47d9 100644 --- a/net/netfilter/ipvs/ip_vs_ctl.c +++ b/net/netfilter/ipvs/ip_vs_ctl.c @@ -2734,8 +2734,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; @@ -2776,8 +2775,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.20.1