Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp1166305imj; Thu, 7 Feb 2019 19:11:46 -0800 (PST) X-Google-Smtp-Source: AHgI3IbrAckQTuwlAbdzfUYE+TTr7u7ayiFp5ESuB/nGGJZFo4m7Ste1qANupbOkVugd4ZEL4APb X-Received: by 2002:a62:2702:: with SMTP id n2mr20191652pfn.29.1549595505941; Thu, 07 Feb 2019 19:11:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549595505; cv=none; d=google.com; s=arc-20160816; b=vlQRBiCh+XXKNMFt9bA/liGlF6DHs0kPzfVj2Bwe0loFDQvBm/ZuaTRJ2rF23KjjNV M8RuB1Np3r9ATMJmcLYgT+b5+cTkQ1n+2QxccX+Mfd/QLZV11SRD5HZ7Hn8t0bLJfQsy IchoiNvfllRIhQ4LGiRa0AFOjUIj9loK6I6/MPHAxSfUBWmwcRkN4u85HTdN3tRXTHod UJhX1Oeqx9Ia2S2+pPRceYEsrB5TYRW4i34aNd6t00RX7xL9r+pJ3Iwulc1Wcra/G/6y IhOhQSpyNLycQqF68ezxqHTjIXXk6cZtS8jCEDIlFA0Katz/MQvImqUi+gkGlUa0YwNj eE7A== 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=k4QrkkY+LUv5ohX1jSmMAT2V3+m+8cT/Vn1Mm6QazT4=; b=hz1Wc8LAFHctFSfafleA3VZTgWoMVZywnl9FD7T/I5T2qQL3g2I9Mjrq+cf+vrYRmm bb9e6MGSyiJJ3xt923Ee0Gu5GJuSARLwt9fJRSg6+cY8KP9q5/dXH332xeU0U0YVZhAu Rod7KDZRNFYYW2/8c0pFo3Y4VmjRiGxlN472lpIeTb6IQphPcUBtEQXp3tm+uaJRr4G0 VsWSQZ7o8H3tXLhFacTFoXkdIwPc/Ei0dogmMcZaHli5CtRypmwpoI0+QcYxDoAwyotl Wzb1/xXBiYx3DArudsmRvmBmoUyez+h67hbhB9mrdvRzYHT0gm6oAEYvj/4AXxrrTBY+ F6gw== 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 p31si916379pgb.440.2019.02.07.19.11.29; Thu, 07 Feb 2019 19:11:45 -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 S1727123AbfBHDKi (ORCPT + 99 others); Thu, 7 Feb 2019 22:10:38 -0500 Received: from gateway22.websitewelcome.com ([192.185.47.228]:48328 "EHLO gateway22.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726801AbfBHDKh (ORCPT ); Thu, 7 Feb 2019 22:10:37 -0500 Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway22.websitewelcome.com (Postfix) with ESMTP id CC8ED1D444 for ; Thu, 7 Feb 2019 21:10:36 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id rwYmg6NJ32qH7rwYmgfOeA; Thu, 07 Feb 2019 21:10:36 -0600 X-Authority-Reason: nr=8 Received: from [189.250.49.191] (port=49080 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1grwYm-001Q7r-3i; Thu, 07 Feb 2019 21:10:36 -0600 Date: Thu, 7 Feb 2019 21:10:35 -0600 From: "Gustavo A. R. Silva" To: Wolfgang Grandegger , Marc Kleine-Budde , "David S. Miller" Cc: linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH net-next] can: kvaser_usb: Use struct_size() in alloc_candev() Message-ID: <20190208031035.GA2665@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: 1grwYm-001Q7r-3i X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.49.191]:49080 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 6 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; void *entry[]; }; instance = alloc(sizeof(struct foo) + count * sizeof(void *)); Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper: instance = alloc(struct_size(instance, entry, count)); This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva --- drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c b/drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c index c89c7d4900d7..0f1d3e807d63 100644 --- a/drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c +++ b/drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c @@ -643,8 +643,7 @@ static int kvaser_usb_init_one(struct kvaser_usb *dev, return err; } - netdev = alloc_candev(sizeof(*priv) + - dev->max_tx_urbs * sizeof(*priv->tx_contexts), + netdev = alloc_candev(struct_size(priv, tx_contexts, dev->max_tx_urbs), dev->max_tx_urbs); if (!netdev) { dev_err(&dev->intf->dev, "Cannot alloc candev\n"); -- 2.20.1