From patchwork Wed Jun 19 19:28:33 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: 11005081 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 D6468924 for ; Wed, 19 Jun 2019 19:28:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C511C1FF2D for ; Wed, 19 Jun 2019 19:28:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B907620564; Wed, 19 Jun 2019 19:28:38 +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=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5DA0F286EC for ; Wed, 19 Jun 2019 19:28:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730405AbfFST2h (ORCPT ); Wed, 19 Jun 2019 15:28:37 -0400 Received: from gateway33.websitewelcome.com ([192.185.146.97]:36898 "EHLO gateway33.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726482AbfFST2h (ORCPT ); Wed, 19 Jun 2019 15:28:37 -0400 Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway33.websitewelcome.com (Postfix) with ESMTP id 943891D7D18 for ; Wed, 19 Jun 2019 14:28:36 -0500 (CDT) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id dgG4hm9rP2qH7dgG4hw3N4; 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-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: 8 X-Source-Cap: Z3V6aWRpbmU7Z3V6aWRpbmU7Z2F0b3I0MTY2Lmhvc3RnYXRvci5jb20= X-Local-Domain: yes Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org 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 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 Reviewed-by: Stefan Hajnoczi --- 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;