From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.7 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16AFDC3A5AB for ; Wed, 4 Sep 2019 18:20:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EC8EA208E4 for ; Wed, 4 Sep 2019 18:20:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389804AbfIDSUI (ORCPT ); Wed, 4 Sep 2019 14:20:08 -0400 Received: from coyote.holtmann.net ([212.227.132.17]:45829 "EHLO mail.holtmann.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388749AbfIDSId (ORCPT ); Wed, 4 Sep 2019 14:08:33 -0400 Received: from marcel-macbook.fritz.box (p4FEFC197.dip0.t-ipconnect.de [79.239.193.151]) by mail.holtmann.org (Postfix) with ESMTPSA id 9C832CECC4; Wed, 4 Sep 2019 20:17:19 +0200 (CEST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: [PATCH][next] Bluetooth: mgmt: Use struct_size() helper From: Marcel Holtmann In-Reply-To: <20190830011211.GA26531@embeddedor> Date: Wed, 4 Sep 2019 20:08:31 +0200 Cc: Johan Hedberg , "David S. Miller" , linux-bluetooth@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: 7bit Message-Id: <4213EC85-4152-4851-9636-7069F9E2272A@holtmann.org> References: <20190830011211.GA26531@embeddedor> To: "Gustavo A. R. Silva" X-Mailer: Apple Mail (2.3445.104.11) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Gustavo, > 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 mgmt_rp_get_connections { > ... > struct mgmt_addr_info addr[0]; > } __packed; > > 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(*rp) + (i * sizeof(struct mgmt_addr_info)); > > with: > > struct_size(rp, addr, i) > > Also, notice that, in this case, variable rp_len is not necessary, > hence it is removed. > > This code was detected with the help of Coccinelle. > > Signed-off-by: Gustavo A. R. Silva > --- > net/bluetooth/mgmt.c | 8 ++------ > 1 file changed, 2 insertions(+), 6 deletions(-) patch has been applied to bluetooth-next tree. Regards Marcel