Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp6561758ybi; Wed, 29 May 2019 09:32:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqyN2HVWA1CRqz9hLO3uofUPpCUj2wv/XJ4kJmYNEYc+FfF75I1PxyJNIef+Uwojr1dQDKhU X-Received: by 2002:aa7:8dcd:: with SMTP id j13mr48225609pfr.107.1559147558872; Wed, 29 May 2019 09:32:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559147558; cv=none; d=google.com; s=arc-20160816; b=00gUqKY0TyIZ0902px98e8bhwpSWn2mW5kd1k0LWVy2m0CfmT+d0LBxBSDDkHB/Yim FpupUyE9WZQXf4bCP9r7sh2XzXzUiOMpojfwJUMMcjjNZK6dK47g8B+yBlLuKjm8VaTz LCTPhTVNCLmI1ZfqceI11SS3XgFLQM1FTOoFgGn3Lc1j9bxUqnrQB7sluRUepQ3TByvR SZwwLz4xnvCLX07Jj/PlIaHRsVnpvSGjcbi4CLQtWopsVE1dV7diwB6Lg9yOQOThVthR pRoNLbJMgR8IaAGp2noyaEELE/g97TG4MPzuF8sCZOKyALp5TiAyJ36PDppmKgpyHU07 o7+w== 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=4QpIwsE18peMvYoHCAAclRYcdZLYMoayTaDPst/9TDc=; b=0D494b50SU4joXqP4tFAndKfvwzCCq8pOW89OApHo+IOtmQ6uOAYgBZEYCAg0h26SK os9b9dUodJ7V70VbhAIX5Utf0hGVMd9UaEgIZ2+d8l13TacNrXWXVgiabo3V1Mlr8Jsy qmTv3V/yw6cOyc5NBx1XoRNNiKQdw+UBBOe0OEFuhH9sMkMkuVdjatEZ8lG2ULobQM84 JgItMveq7B5Su/NtoI361JtiVE8WY4Ddz84lOV8XuPyIe4C0ZWZ/JJukMte0rk8N1HoE k4X/aKKtnrm2TzZWt0SjP3hArrgCo3oyM4pK6PROT6FtW5gmEBcjrhyv0m7yUEdnyEO9 4jjw== 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 v38si19637plg.277.2019.05.29.09.32.21; Wed, 29 May 2019 09:32:38 -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 S1726699AbfE2Qaz (ORCPT + 99 others); Wed, 29 May 2019 12:30:55 -0400 Received: from gateway23.websitewelcome.com ([192.185.50.119]:37445 "EHLO gateway23.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726062AbfE2Qay (ORCPT ); Wed, 29 May 2019 12:30:54 -0400 Received: from cm17.websitewelcome.com (cm17.websitewelcome.com [100.42.49.20]) by gateway23.websitewelcome.com (Postfix) with ESMTP id EB8174D19 for ; Wed, 29 May 2019 11:30:53 -0500 (CDT) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id W1TZh8V2M90onW1TZhBX7b; Wed, 29 May 2019 11:30:53 -0500 X-Authority-Reason: nr=8 Received: from [189.250.47.159] (port=50828 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1hW1TY-001z7k-O5; Wed, 29 May 2019 11:30:52 -0500 Date: Wed, 29 May 2019 11:30:52 -0500 From: "Gustavo A. R. Silva" To: Bartosz Golaszewski , Arnd Bergmann , Greg Kroah-Hartman Cc: linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH] eeprom: at24: use struct_size() in devm_kzalloc() Message-ID: <20190529163052.GA29158@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.47.159 X-Source-L: No X-Exim-ID: 1hW1TY-001z7k-O5 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.47.159]:50828 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 9 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 = devm_kzalloc(dev, size, GFP_KERNEL); Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper: instance = devm_kzalloc(dev, struct_size(instance, entry, count), GFP_KERNEL); Notice that, in this case, variable at24_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/misc/eeprom/at24.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/drivers/misc/eeprom/at24.c b/drivers/misc/eeprom/at24.c index ba8e73812644..78ba6b1917a8 100644 --- a/drivers/misc/eeprom/at24.c +++ b/drivers/misc/eeprom/at24.c @@ -568,7 +568,6 @@ static int at24_probe(struct i2c_client *client) unsigned int i, num_addresses; struct at24_data *at24; struct regmap *regmap; - size_t at24_size; bool writable; u8 test_byte; int err; @@ -652,8 +651,8 @@ static int at24_probe(struct i2c_client *client) if (IS_ERR(regmap)) return PTR_ERR(regmap); - at24_size = sizeof(*at24) + num_addresses * sizeof(struct at24_client); - at24 = devm_kzalloc(dev, at24_size, GFP_KERNEL); + at24 = devm_kzalloc(dev, struct_size(at24, client, num_addresses), + GFP_KERNEL); if (!at24) return -ENOMEM; -- 2.21.0