From patchwork Thu Jun 27 06:07:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 13713856 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D8DD3C3064D for ; Thu, 27 Jun 2024 06:10:07 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sMiJk-0005Mu-Rb; Thu, 27 Jun 2024 02:09:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sMiJi-0005Hi-LF for qemu-devel@nongnu.org; Thu, 27 Jun 2024 02:09:10 -0400 Received: from mail-oi1-x22e.google.com ([2607:f8b0:4864:20::22e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sMiJg-0003hP-Vi for qemu-devel@nongnu.org; Thu, 27 Jun 2024 02:09:10 -0400 Received: by mail-oi1-x22e.google.com with SMTP id 5614622812f47-3c9d70d93dbso5058639b6e.3 for ; Wed, 26 Jun 2024 23:09:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1719468548; x=1720073348; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=647sLL8s6f3U/utr7BzHhYO7TP6S1dzLUbAhiUGJmUg=; b=tldX6TO57ms5ItpVv9EPvUwIqLQaSJ+NyLKDu/CKwyBmHD15UpKlOMXLBOQqQbH0uq I4lGjv9y3L8goMQnFmi1QEGO3pfrtJW3epMd50MQn/sc93Pq3+W3Q14HIxwUYYu25t9w x5wGogFEtmL00hKHKwgkQiOcE8/DzCwVcNExnWE+imrmdGuXkFcWSIQJs2jq1VyhNmk+ wIczfkjrf6qUQ5NZu9iFkZbhr/CYoiHy9dj3bEYDplNPRnK3doS7IkaZQmyIhfdnQEWL zvDNqrQU6jig4lBeKO3zK/p79Sahsconq2MTfLLn4o9kLm4qDsRDytLGb0FCdvaAV6+0 WISQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719468548; x=1720073348; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=647sLL8s6f3U/utr7BzHhYO7TP6S1dzLUbAhiUGJmUg=; b=EGRMPVtawl4bd8G4VcaQMyfSkflUJwkCuAgvwZOTElyLBCzK35/GBmqKJ08Ifg1sce 9dmoWXaMOlBiD6YIAXv9ACghftjjW6Mv5ZmxVNJF7sklhdBbNTgIkQQXUA7Iv6Av7dhS hsmAbVvCsTCcAmRnEGnasfAmyNuy8qhVK1znejCQA3rhLbxJ7qnreYd0pHbp7k8c72yG vZQGPnK7s7VJ++AWtiazzLX9svqGhp++b3fFoMW5cBvsTMAHMw7vS/gBV11sp9zv8lI6 JHlNOQllJ/LhRORoTA/iy6PF9364qchdh0twQ5g+9nbs2CRko6aYLUGrw5/3iGl9lHtI voYA== X-Gm-Message-State: AOJu0Yw7G+ytUGw/Noz2JHm+qcSX5ooyjc2DKEPYSlTCSYFZaHu9TQeG QKg09j0F0+EDd6PRS9kmdgq59uYxXB+O8TrFtbTdsyFQ1f6aAcy5PTvn/jw8neQ= X-Google-Smtp-Source: AGHT+IEGTvrjk0Mo65rf6F8l0hoP4fF2PvJcc6PKC3nybOcGEEFZ61Po8hK+R9Idjqoi4y3jcfAKlw== X-Received: by 2002:a05:6808:1389:b0:3d2:17c2:8301 with SMTP id 5614622812f47-3d5459cf707mr15578076b6e.30.1719468547887; Wed, 26 Jun 2024 23:09:07 -0700 (PDT) Received: from localhost ([157.82.204.135]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-706b48d09fesm518292b3a.19.2024.06.26.23.09.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 26 Jun 2024 23:09:07 -0700 (PDT) From: Akihiko Odaki Date: Thu, 27 Jun 2024 15:07:59 +0900 Subject: [PATCH v10 10/12] hw/pci: Replace -1 with UINT32_MAX for romsize MIME-Version: 1.0 Message-Id: <20240627-reuse-v10-10-7ca0b8ed3d9f@daynix.com> References: <20240627-reuse-v10-0-7ca0b8ed3d9f@daynix.com> In-Reply-To: <20240627-reuse-v10-0-7ca0b8ed3d9f@daynix.com> To: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , "Michael S. Tsirkin" , Marcel Apfelbaum , Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_Goa?= =?utf-8?q?ter?= , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Sriram Yagnaraman , Jason Wang , Keith Busch , Klaus Jensen , Markus Armbruster Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org, Akihiko Odaki X-Mailer: b4 0.14-dev-fd6e3 Received-SPF: none client-ip=2607:f8b0:4864:20::22e; envelope-from=akihiko.odaki@daynix.com; helo=mail-oi1-x22e.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org romsize is an uint32_t variable. Specifying -1 as an uint32_t value is obscure way to denote UINT32_MAX. Worse, if int is wider than 32-bit, it will change the behavior of a construct like the following: romsize = -1; if (romsize != -1) { ... } When -1 is assigned to romsize, -1 will be implicitly casted into uint32_t, resulting in UINT32_MAX. On contrary, when evaluating romsize != -1, romsize will be casted into int, and it will be a comparison of UINT32_MAX and -1, and result in false. Replace -1 with UINT32_MAX for statements involving the variable to clarify the intent and prevent potential breakage. Signed-off-by: Akihiko Odaki Reviewed-by: Markus Armbruster --- hw/pci/pci.c | 8 ++++---- hw/xen/xen_pt_load_rom.c | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/hw/pci/pci.c b/hw/pci/pci.c index af1c743611af..1eb6abf534ca 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -70,7 +70,7 @@ static bool pcie_has_upstream_port(PCIDevice *dev); static Property pci_props[] = { DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1), DEFINE_PROP_STRING("romfile", PCIDevice, romfile), - DEFINE_PROP_UINT32("romsize", PCIDevice, romsize, -1), + DEFINE_PROP_UINT32("romsize", PCIDevice, romsize, UINT32_MAX), DEFINE_PROP_UINT32("rombar", PCIDevice, rom_bar, 1), DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present, QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false), @@ -2073,7 +2073,7 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp) g_cmp_uint32, NULL); } - if (pci_dev->romsize != -1 && !is_power_of_2(pci_dev->romsize)) { + if (pci_dev->romsize != UINT32_MAX && !is_power_of_2(pci_dev->romsize)) { error_setg(errp, "ROM size %u is not a power of two", pci_dev->romsize); return; } @@ -2359,7 +2359,7 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom, return; } - if (load_file || pdev->romsize == -1) { + if (load_file || pdev->romsize == UINT32_MAX) { path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile); if (path == NULL) { path = g_strdup(pdev->romfile); @@ -2378,7 +2378,7 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom, pdev->romfile); return; } - if (pdev->romsize != -1) { + if (pdev->romsize != UINT_MAX) { if (size > pdev->romsize) { error_setg(errp, "romfile \"%s\" (%u bytes) " "is too large for ROM size %u", diff --git a/hw/xen/xen_pt_load_rom.c b/hw/xen/xen_pt_load_rom.c index 03422a8a7148..6bc64acd3352 100644 --- a/hw/xen/xen_pt_load_rom.c +++ b/hw/xen/xen_pt_load_rom.c @@ -53,7 +53,7 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev, } fseek(fp, 0, SEEK_SET); - if (dev->romsize != -1) { + if (dev->romsize != UINT_MAX) { if (st.st_size > dev->romsize) { error_report("ROM BAR \"%s\" (%ld bytes) is too large for ROM size %u", rom_file, (long) st.st_size, dev->romsize);