Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4885456imu; Tue, 8 Jan 2019 07:55:15 -0800 (PST) X-Google-Smtp-Source: ALg8bN6+5UKbgAiLk5lgVqwhit7AnAukqyutdKghEW5Cws4wPCWsJgdEfQ4H+CwrRYJCb+Qz0bSH X-Received: by 2002:a63:20e:: with SMTP id 14mr1966924pgc.161.1546962915880; Tue, 08 Jan 2019 07:55:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546962915; cv=none; d=google.com; s=arc-20160816; b=uOH5Hq9vpx597I1m0txDcKiCfqaEbpPr0R5Dhm1zkMOSpseHgVzJaUg9dDJ+JmiyGU 58W+6IncvNMCTNDDcNTTT7mtd2iYsVMvbavaqO1RBL76H1Fn/6/kBb6FGpl3YRwE2IPI YLM5J4aOPAWuFy9iKmp0jHxhTPahu26V3h5ysZ9WoDtLbYf/dxCQ+8eVwSCOibHEIDJr sC6qq/FPO/c92hG6TGZhSqMwAtQAoYMC3a2xE6Zgk9HqJZZgLJ4cUoXFToKDq1dbROP9 Fx1P3/6ii2aez973JZg0SueVjEEcbNHNS5w4Eu5KPXqI697QJrzIo0j4lFC0vJc0JVfj BGlw== 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=KGI8O14nopUwtSQbKbWCDF7mr1+Tc8lhHLDAV0WEUnE=; b=ACGnDtjmOvq90GUSWzS3dOss1LThwLrKiuQsawNivBJTLpv2km76grfEwj83zKq4O0 Udmct/r69k17PcIqTzg1Q+lFnvQ0Xr5jdcMW59RbXCKH90GdCCH8K3DgSFGnQIVIUgl6 VqXd4kzOk/p3wtzHH+Zqw4bkkazUxZkewDdbWRl06iaYKl4qlxft7i9qXE4Sb/CleaAj teS+s/dL45g5kj/owUOZN/gGiOz1UJsKSjVE1ryD56bEpp5hhQH4pNbICY3zN1fxMb2H ubP9/Lg38osnW9dSSsyvQvhieTWxjs0r9DMxEjE2/WGjYdudybC8jl+yFHrMoz9HlKLs i51A== 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 s191si46853326pfs.53.2019.01.08.07.55.00; Tue, 08 Jan 2019 07:55:15 -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 S1729062AbfAHPws (ORCPT + 99 others); Tue, 8 Jan 2019 10:52:48 -0500 Received: from gateway33.websitewelcome.com ([192.185.146.130]:39680 "EHLO gateway33.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727906AbfAHPws (ORCPT ); Tue, 8 Jan 2019 10:52:48 -0500 Received: from cm17.websitewelcome.com (cm17.websitewelcome.com [100.42.49.20]) by gateway33.websitewelcome.com (Postfix) with ESMTP id 8620324426D for ; Tue, 8 Jan 2019 09:52:47 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id gtgNgUZbG90ongtgNgymsW; Tue, 08 Jan 2019 09:52:47 -0600 X-Authority-Reason: nr=8 Received: from [189.250.130.205] (port=52952 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1ggtgM-001VMM-3X; Tue, 08 Jan 2019 09:52:46 -0600 Date: Tue, 8 Jan 2019 09:52:44 -0600 From: "Gustavo A. R. Silva" To: David Woodhouse , Brian Norris , Boris Brezillon , Marek Vasut , Richard Weinberger Cc: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH] mtd: cfi: cmdset_0001: Use struct_size() in kmalloc() Message-ID: <20190108155244.GA15339@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.130.205 X-Source-L: No X-Exim-ID: 1ggtgM-001VMM-3X X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.130.205]:52952 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 27 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 = kmalloc(sizeof(struct foo) + sizeof(void *) * count, GFP_KERNEL); Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper: instance = kmalloc(struct_size(instance, entry, count), GFP_KERNEL); This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva --- drivers/mtd/chips/cfi_cmdset_0001.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/mtd/chips/cfi_cmdset_0001.c b/drivers/mtd/chips/cfi_cmdset_0001.c index 6e8e7b1bb34b..79a53cb8507b 100644 --- a/drivers/mtd/chips/cfi_cmdset_0001.c +++ b/drivers/mtd/chips/cfi_cmdset_0001.c @@ -756,7 +756,8 @@ static int cfi_intelext_partition_fixup(struct mtd_info *mtd, } numvirtchips = cfi->numchips * numparts; - newcfi = kmalloc(sizeof(struct cfi_private) + numvirtchips * sizeof(struct flchip), GFP_KERNEL); + newcfi = kmalloc(struct_size(newcfi, chips, numvirtchips), + GFP_KERNEL); if (!newcfi) return -ENOMEM; shared = kmalloc_array(cfi->numchips, -- 2.20.1