Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp8551779ybi; Thu, 6 Jun 2019 14:33:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqxtOfUASOA58mYKToYAeuBxAXAhs1Fja4ildXEewyYX6howltovJsgNXRf0/qZyukKqyeHH X-Received: by 2002:a17:902:bd46:: with SMTP id b6mr52026340plx.173.1559856793403; Thu, 06 Jun 2019 14:33:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559856793; cv=none; d=google.com; s=arc-20160816; b=J9uYmN5O9mMEl1NhfSD2r+1BSJmnUJLIoQKGgCDvoJXKHr4KVg/cVu0hH8zt4q24hE EdCtCVVQzpSyWgyUseRSsB474ZJTDveUltq+eyvIaz3GRhVv9IDEjnzT/qr4SZZEa/Hq JJ+SiZwpy59m1/pdSf+oBk54z4ieWXMEqAp8A13g6aYt2UHUl0zcdzLJG8PD9F1T05zK RrkJ8aE8MaAuHfxS0oUov9coEpyjDsHzYPfdWLwsHAyzDVPEq6cYC6Gf5d5YPpxtTQUl sXfbn2siq71iGNcg0GrqaXNvh32aSq5UNuLXcSyHWi4BrLxCZRiZlFnuPOlo2Q7AamLf II9A== 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=1b+eMRwYWzCKvOgA99dEeJmneVWpsG6ZaRrCqZ06Qxc=; b=RicB0ZX0NcYL2oOmwyxlXp25PRIKC6Fdb04D2M7GvgthDBK+4d3nXGoonOvHyaNizU kP64C189u47zLyMeAJBqKQoDbdSRUtXMhuWUjmn3e5ZjEOagxOghPOln2K1aUOtvpCs1 TNIuhAKhbR36UgRiliS2HWMhLhTFWM5g/i0isRIvoUMumWqguNSm8HTT+RCtGPSayaec Knug9hl8uWqE5BTX5GMsMcvDWiZVmQGT4u0Pm+3Vm29ObkM0vvqzpVzbin4OA5A5X1cK umLIB38eRunM5+H8Bhe88d5YBfWfOIBSlBJ1ig5pEiKZk63Yub9GaZDcCW3os8atCEna Z5Jw== 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 h9si160298pgs.397.2019.06.06.14.32.56; Thu, 06 Jun 2019 14:33:13 -0700 (PDT) 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 S1730569AbfFFSXW (ORCPT + 99 others); Thu, 6 Jun 2019 14:23:22 -0400 Received: from gateway32.websitewelcome.com ([192.185.145.178]:20075 "EHLO gateway32.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729519AbfFFSXW (ORCPT ); Thu, 6 Jun 2019 14:23:22 -0400 Received: from cm13.websitewelcome.com (cm13.websitewelcome.com [100.42.49.6]) by gateway32.websitewelcome.com (Postfix) with ESMTP id D043088AA9 for ; Thu, 6 Jun 2019 13:23:21 -0500 (CDT) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id Yx2nhYj6CYTGMYx2nhWeWH; Thu, 06 Jun 2019 13:23:21 -0500 X-Authority-Reason: nr=8 Received: from [189.250.127.120] (port=48372 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.92) (envelope-from ) id 1hYwyU-002JXr-Mg; Thu, 06 Jun 2019 13:23:21 -0500 Date: Thu, 6 Jun 2019 13:18:53 -0500 From: "Gustavo A. R. Silva" To: Jessica Yu Cc: linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH v2] kernel: module: Use struct_size() helper Message-ID: <20190606181853.GA29960@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.127.120 X-Source-L: No X-Exim-ID: 1hYwyU-002JXr-Mg X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.127.120]:48372 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 2 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 module_sect_attrs { ... struct module_sect_attr attrs[0]; }; Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes. So, replace the following form: sizeof(*sect_attrs) + nloaded * sizeof(sect_attrs->attrs[0] with: struct_size(sect_attrs, attrs, nloaded) This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva --- Changes in v2: - Update changelog text by adding a better description for the motivation and usage of the struct_size() helper. kernel/module.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kernel/module.c b/kernel/module.c index 80c7c09584cf..3f3bb090fbf4 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -1492,8 +1492,7 @@ static void add_sect_attrs(struct module *mod, const struct load_info *info) for (i = 0; i < info->hdr->e_shnum; i++) if (!sect_empty(&info->sechdrs[i])) nloaded++; - size[0] = ALIGN(sizeof(*sect_attrs) - + nloaded * sizeof(sect_attrs->attrs[0]), + size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded), sizeof(sect_attrs->grp.attrs[0])); size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]); sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL); -- 2.21.0