Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1048952ybi; Wed, 19 Jun 2019 12:30:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqyftM0QYW2qlBccLX+TaFn9id1zbIxmHpwtGV/pxUkTfAzzcNykCMSbRxJYLUzCimx9SkTK X-Received: by 2002:a17:90a:2525:: with SMTP id j34mr13235833pje.11.1560972615994; Wed, 19 Jun 2019 12:30:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560972615; cv=none; d=google.com; s=arc-20160816; b=lk18m+IIxVHfiffmlcLvCZjg6pDJ/8vKL9FISxfdyXRyQIAf1n4DDHpG6isltr7SZY iyihTwU7VCUYkhr2m5Qegh3T9jCZY1LlnWh0H6NPlrV0wb+SoFgXsvwe1fptlD02328f D/cTSm0E/QnN1/uilNK0ilhyqWXX7Mh80a8A7UrtL0K5jYWSBygTKLpchuVwZTe/GMpe HB6lwoGnFHk9RWRO21d2uqduyxnd8wMEuE3sSIyKvp4WDyKosxhtkylbBG4oT7OV6wA5 hgmWTLUfLRx8IYXft8DdLNpAsR616WJ/z/2C3L3SieV/jKlnuU1sdb1gdkbgshQExyHk qijw== 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=kY9lFHRiW/CTKPjRrp9kLVxwoONTo2akSzMBKqWVoo8=; b=vIQw3boffqpJNfaozxI4Llv64Xyb/E9Do9bpCjKvfB+Y7tpbwa04RszEWmyZn7gqpR ZRCHXsK4l5n92JcjlFbyAgo1DRvXkPXSWdhvG3/gUCg07CnDlZiXLj5mXQUTxM/z8YvR BkshtJRS5hlj2K3XRROekWshYPiGwfgsePfKv55+FaVgBSB9gEomcvDsjxsqCvZEuygs LPz0xmGuip/cutbWEvxyVNRazay1sVNGTbH48KF5bVyOHap89y4XyoQbhMLWAt1RWmPT NrAjULMR3pciyAui+k0gICWd9VrTZyEpwNiD0UiLb7LFL9EHNEAazVyJON71odZ5GddU +BsA== 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 j59si16640341plb.176.2019.06.19.12.29.59; Wed, 19 Jun 2019 12:30:15 -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 S1730479AbfFST2i (ORCPT + 99 others); Wed, 19 Jun 2019 15:28:38 -0400 Received: from gateway34.websitewelcome.com ([192.185.148.222]:15191 "EHLO gateway34.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729988AbfFST2h (ORCPT ); Wed, 19 Jun 2019 15:28:37 -0400 Received: from cm13.websitewelcome.com (cm13.websitewelcome.com [100.42.49.6]) by gateway34.websitewelcome.com (Postfix) with ESMTP id 93E98BB875 for ; Wed, 19 Jun 2019 14:28:36 -0500 (CDT) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id dgG4h0KjyYTGMdgG4hvu9z; Wed, 19 Jun 2019 14:28:36 -0500 X-Authority-Reason: nr=8 Received: from cablelink-187-160-61-213.pcs.intercable.net ([187.160.61.213]:34582 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.92) (envelope-from ) id 1hdgG3-000c4E-GC; Wed, 19 Jun 2019 14:28:35 -0500 Date: Wed, 19 Jun 2019 14:28:33 -0500 From: "Gustavo A. R. Silva" To: "Michael S. Tsirkin" , Jason Wang , Paolo Bonzini , Stefan Hajnoczi , "James E.J. Bottomley" , "Martin K. Petersen" Cc: virtualization@lists.linux-foundation.org, linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH] scsi: virtio_scsi: Use struct_size() helper Message-ID: <20190619192833.GA825@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: 187.160.61.213 X-Source-L: No X-Exim-ID: 1hdgG3-000c4E-GC X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: cablelink-187-160-61-213.pcs.intercable.net (embeddedor) [187.160.61.213]:34582 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 virtio_scsi { ... struct virtio_scsi_vq req_vqs[]; }; 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(*vscsi) + sizeof(vscsi->req_vqs[0]) * num_queues with: struct_size(vscsi, req_vqs, num_queues) This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva --- drivers/scsi/virtio_scsi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/scsi/virtio_scsi.c b/drivers/scsi/virtio_scsi.c index 13f1b3b9923a..ed4f79bffc73 100644 --- a/drivers/scsi/virtio_scsi.c +++ b/drivers/scsi/virtio_scsi.c @@ -795,7 +795,7 @@ static int virtscsi_probe(struct virtio_device *vdev) num_targets = virtscsi_config_get(vdev, max_target) + 1; shost = scsi_host_alloc(&virtscsi_host_template, - sizeof(*vscsi) + sizeof(vscsi->req_vqs[0]) * num_queues); + struct_size(vscsi, req_vqs, num_queues)); if (!shost) return -ENOMEM; -- 2.21.0