From patchwork Fri Dec 13 16:17:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 11291187 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0D1266C1 for ; Fri, 13 Dec 2019 21:01:54 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D1BB32465E for ; Fri, 13 Dec 2019 21:01:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Nfy7zHBJ" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D1BB32465E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1ifnfg-0000j4-1a; Fri, 13 Dec 2019 16:20:04 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1ifnfe-0000aw-O5 for xen-devel@lists.xenproject.org; Fri, 13 Dec 2019 16:20:02 +0000 X-Inumbo-ID: 6568569a-1dc4-11ea-a914-bc764e2007e4 Received: from us-smtp-1.mimecast.com (unknown [205.139.110.61]) by us1-rack-iad1.inumbo.com (Halon) with ESMTP id 6568569a-1dc4-11ea-a914-bc764e2007e4; Fri, 13 Dec 2019 16:19:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1576253993; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=psjmWWDdVhLhKwDb1Wq8yYWFEUJUVcwEEB0iH9Xv/kA=; b=Nfy7zHBJEdilzAJnjb4XMffrbB907kyCRzm0FbdKOlR9dknJ7U9w/dq8+qrD2eW03mMf+5 A+75QNNc22NM6Y9BC0QCgjlLTALzM2slucuOp1RXkvmrMvOtKjmKG36snfFCOamCt740X6 ceYM0N4R9ij/Qmy9CdXPaXLevg92IM8= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-296-XiV1IY8PM82PJ_OyDAlfBg-1; Fri, 13 Dec 2019 11:19:50 -0500 X-MC-Unique: XiV1IY8PM82PJ_OyDAlfBg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AE9211856A65; Fri, 13 Dec 2019 16:19:48 +0000 (UTC) Received: from x1w.redhat.com (ovpn-205-147.brq.redhat.com [10.40.205.147]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 070AE19C4F; Fri, 13 Dec 2019 16:19:37 +0000 (UTC) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Date: Fri, 13 Dec 2019 17:17:49 +0100 Message-Id: <20191213161753.8051-9-philmd@redhat.com> In-Reply-To: <20191213161753.8051-1-philmd@redhat.com> References: <20191213161753.8051-1-philmd@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Subject: [Xen-devel] [PATCH 08/12] hw/ide/piix: Use ARRAY_SIZE() instead of magic numbers X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Sergio Lopez , Eduardo Habkost , kvm@vger.kernel.org, Paul Durrant , =?utf-8?q?Philippe_Mathie?= =?utf-8?q?u-Daud=C3=A9?= , "Michael S. Tsirkin" , Paolo Bonzini , Marcel Apfelbaum , xen-devel@lists.xenproject.org, Anthony Perard , Igor Mammedov , qemu-block@nongnu.org, John Snow , Richard Henderson Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Using magic numbers is dangerous because the structures PCIIDEState might be modified and this source file consuming the "ide/pci.h" header would be out of sync, eventually accessing out of bound array members. Use the ARRAY_SIZE() to keep the source file sync. Signed-off-by: Philippe Mathieu-Daudé --- hw/ide/piix.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/hw/ide/piix.c b/hw/ide/piix.c index ffeff4e095..ab23613a44 100644 --- a/hw/ide/piix.c +++ b/hw/ide/piix.c @@ -87,10 +87,9 @@ static const MemoryRegionOps piix_bmdma_ops = { static void bmdma_setup_bar(PCIIDEState *d) { - int i; - memory_region_init(&d->bmdma_bar, OBJECT(d), "piix-bmdma-container", 16); - for(i = 0;i < 2; i++) { + + for (size_t i = 0; i < ARRAY_SIZE(d->bmdma); i++) { BMDMAState *bm = &d->bmdma[i]; memory_region_init_io(&bm->extra_io, OBJECT(d), &piix_bmdma_ops, bm, @@ -107,9 +106,8 @@ static void piix_ide_reset(DeviceState *dev) PCIIDEState *d = PCI_IDE(dev); PCIDevice *pd = PCI_DEVICE(d); uint8_t *pci_conf = pd->config; - int i; - for (i = 0; i < 2; i++) { + for (size_t i = 0; i < ARRAY_SIZE(d->bus); i++) { ide_bus_reset(&d->bus[i]); } @@ -132,10 +130,10 @@ static void pci_piix_init_ports(PCIIDEState *d) { {0x1f0, 0x3f6, 14}, {0x170, 0x376, 15}, }; - int i; - for (i = 0; i < 2; i++) { - ide_bus_new(&d->bus[i], sizeof(d->bus[i]), DEVICE(d), i, 2); + for (size_t i = 0; i < ARRAY_SIZE(d->bus); i++) { + ide_bus_new(&d->bus[i], sizeof(d->bus[i]), DEVICE(d), i, + ARRAY_SIZE(d->bus[0].ifs)); ide_init_ioport(&d->bus[i], NULL, port_info[i].iobase, port_info[i].iobase2); ide_init2(&d->bus[i], isa_get_irq(NULL, port_info[i].isairq)); @@ -163,14 +161,13 @@ static void pci_piix_ide_realize(PCIDevice *dev, Error **errp) int pci_piix3_xen_ide_unplug(DeviceState *dev, bool aux) { - PCIIDEState *pci_ide; + PCIIDEState *pci_ide = PCI_IDE(dev); DriveInfo *di; - int i; IDEDevice *idedev; + const size_t idedev_max = ARRAY_SIZE(pci_ide->bus) + * ARRAY_SIZE(pci_ide->bus[0].ifs); - pci_ide = PCI_IDE(dev); - - for (i = aux ? 1 : 0; i < 4; i++) { + for (size_t i = aux ? 1 : 0; i < idedev_max; i++) { di = drive_get_by_index(IF_IDE, i); if (di != NULL && !di->media_cd) { BlockBackend *blk = blk_by_legacy_dinfo(di); @@ -210,9 +207,8 @@ PCIDevice *pci_piix3_xen_ide_init(PCIBus *bus, DriveInfo **hd_table, int devfn) static void pci_piix_ide_exitfn(PCIDevice *dev) { PCIIDEState *d = PCI_IDE(dev); - unsigned i; - for (i = 0; i < 2; ++i) { + for (size_t i = 0; i < ARRAY_SIZE(d->bmdma); ++i) { memory_region_del_subregion(&d->bmdma_bar, &d->bmdma[i].extra_io); memory_region_del_subregion(&d->bmdma_bar, &d->bmdma[i].addr_ioport); }