From patchwork Wed Aug 7 16:13:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gustavo A. R. Silva" X-Patchwork-Id: 11082471 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DCDE2746 for ; Wed, 7 Aug 2019 17:01:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C9FDE2873C for ; Wed, 7 Aug 2019 17:01:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BDE9728A14; Wed, 7 Aug 2019 17:01:43 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 6D35F2873C for ; Wed, 7 Aug 2019 17:01:43 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 92BCB6E73D; Wed, 7 Aug 2019 17:01:42 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org X-Greylist: delayed 1500 seconds by postgrey-1.36 at gabe; Wed, 07 Aug 2019 17:01:41 UTC Received: from gateway20.websitewelcome.com (gateway20.websitewelcome.com [192.185.50.28]) by gabe.freedesktop.org (Postfix) with ESMTPS id DE0FB6E73D for ; Wed, 7 Aug 2019 17:01:41 +0000 (UTC) Received: from cm10.websitewelcome.com (cm10.websitewelcome.com [100.42.49.4]) by gateway20.websitewelcome.com (Postfix) with ESMTP id A70C2400DCC19 for ; Wed, 7 Aug 2019 10:09:20 -0500 (CDT) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id vOYuh94jt2PzOvOYuhCX6v; Wed, 07 Aug 2019 11:13:16 -0500 X-Authority-Reason: nr=8 Received: from 187-162-252-62.static.axtel.net ([187.162.252.62]:45386 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.92) (envelope-from ) id 1hvOYt-000bPc-21; Wed, 07 Aug 2019 11:13:15 -0500 Date: Wed, 7 Aug 2019 11:13:12 -0500 From: "Gustavo A. R. Silva" To: Bartlomiej Zolnierkiewicz Subject: [PATCH] video: fbdev/mmp/core: Use struct_size() in kzalloc() Message-ID: <20190807161312.GA26835@embeddedor> MIME-Version: 1.0 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 - lists.freedesktop.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - embeddedor.com X-BWhitelist: no X-Source-IP: 187.162.252.62 X-Source-L: No X-Exim-ID: 1hvOYt-000bPc-21 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 187-162-252-62.static.axtel.net (embeddedor) [187.162.252.62]:45386 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 2 X-Source-Cap: Z3V6aWRpbmU7Z3V6aWRpbmU7Z2F0b3I0MTY2Lmhvc3RnYXRvci5jb20= X-Local-Domain: yes X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=embeddedor.com; s=default; h=Content-Type:MIME-Version:Message-ID:Subject: Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=RDLih2xLWqp+jClGe6quPxx2j3NmBQT7gXxnFLBcjPI=; b=IhyWC51nLhTrjc3Vu16cl6sJ2G s9ltt4Pt+NqZlV9CIrKWPURUTLN8cPdvJ0HvpO5m1CsjLff9dgV/lP9thSiNTZJbBUslbCrJHHYhc p4nMhqr/IOSY5a/guns9P49UhBLpexJrh3kDoShOrDRw4LczwgjN3lOp+tj340g3v+o9TZ8iw5k+f NusgwMbGUySHojHyu+VDpevlDA4y77iu9KgCcmkINyyprQrScRVY7n/PsaapB74U/dQbW1TFc26nm QBzuWEHaA+dNdwxXH6Jt2y1zIsXvZ5txKv7D34NGHHrkWYiLiq29TdiywNlRiSqQlwAENCxm3E+Xz /OMfnK4Q==; X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-fbdev@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, "Gustavo A. R. Silva" Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 mmp_path { ... struct mmp_overlay overlays[0]; }; size = sizeof(struct mmp_path) + count * sizeof(struct mmp_overlay); instance = kzalloc(size, GFP_KERNEL) Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper: instance = kzalloc(struct_size(instance, overlays, count), GFP_KERNEL) Notice that, in this case, variable size is not necessary, hence it is removed. This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva --- drivers/video/fbdev/mmp/core.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/drivers/video/fbdev/mmp/core.c b/drivers/video/fbdev/mmp/core.c index 0ffc1b7b7052..154127256a2c 100644 --- a/drivers/video/fbdev/mmp/core.c +++ b/drivers/video/fbdev/mmp/core.c @@ -153,13 +153,11 @@ EXPORT_SYMBOL_GPL(mmp_get_path); struct mmp_path *mmp_register_path(struct mmp_path_info *info) { int i; - size_t size; struct mmp_path *path = NULL; struct mmp_panel *panel; - size = sizeof(struct mmp_path) - + sizeof(struct mmp_overlay) * info->overlay_num; - path = kzalloc(size, GFP_KERNEL); + path = kzalloc(struct_size(path, overlays, info->overlay_num), + GFP_KERNEL); if (!path) return NULL;