From patchwork Sat Feb 24 09:24:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 1903770 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=daynix-com.20230601.gappssmtp.com header.i=@daynix-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=CR1T+f84; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4ThhMr6s6pz23q3 for ; Sat, 24 Feb 2024 20:26:32 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rdoHz-0007aX-UD; Sat, 24 Feb 2024 04:25:47 -0500 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 1rdoHx-0007S4-RW for qemu-devel@nongnu.org; Sat, 24 Feb 2024 04:25:45 -0500 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rdoHv-0002xu-QN for qemu-devel@nongnu.org; Sat, 24 Feb 2024 04:25:45 -0500 Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-6e3ffafa708so1708559b3a.1 for ; Sat, 24 Feb 2024 01:25:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1708766742; x=1709371542; 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=to/Jrs3zcoDLKOpkcBrN6NN3gvz3JR19dFR5YKO4O7w=; b=CR1T+f84Z/hb9Pn0mrkzak/Ms3LL8yaw2NXqShKvYxVKsbs60oYKnXBgZcuP1qfqeI CyQO7ps6AO9VWczs+AMHJCbAsJ/QDqDyRjhJtziWejx88HZDBQZ15iJogWH3PMgf6L5i YV3I8DKw8qo20NaGuR7zgLbZoEAXBfErsbr9He3ChOAJkT6W8ZlYDp6N6COlSm0f3SLT P2n/pkAnCoQYp6grEd0uK78bXgw3cON0nJh7VC2JB35u2AX4MNyX2w6zLmdDnCcm05fR c4MONh9IyI787m1qI51WkpbkvDbWc+YyksrLOVRM+/Y2hUAdrBEa4ZSrHNafftACq3rq cP5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708766742; x=1709371542; 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=to/Jrs3zcoDLKOpkcBrN6NN3gvz3JR19dFR5YKO4O7w=; b=hLJyj+IUiQc6VJK5uVJEusPHW1ajMoUzAvA2eLKA73dPC0g03zcymTxht4Fl93y2pF MghquRWTagelRZ9DuthB5/YIQZJQjnxduIMlFX4HtJrU1U21mA/pApLHs/TSbu3ajLwi o2YULNRQn2Jg+y0K1jtWW6UW34sm0mkcpyNiPXJtTTpIgh8fkcL1LswlHmD2ZHKHTJ52 yF3kXiAAUgoud3GggsQHTzBUDyccTJWaR9I3q44DsHrf3SYa5v3wDOpjfehsKJbYOZMN NhBS5MkU3z7SY5XALRthH3166E7D9m4R7eOiWZjaDIRrTPS28AmKNPXsnDgeXW9eETq3 q+Lw== X-Gm-Message-State: AOJu0YyRWCL4XrxaPhX1z4RCUyG/BfqXad3Gr6lbmKXB5bDa8S8Lbq55 g2b1lpI0z373piZs7S6buKiPc3MfyfczkPLbbnoTKaep4t7OY4Xtyq06yDOsoFMtVWnoEEGGdgW t X-Google-Smtp-Source: AGHT+IEvAKRNsen9U6MFbhEmv8hOFUJXcaxXE5wYROKuyN7F8tT01/aDL/oyCe8q/RUqfeWR7C33Vw== X-Received: by 2002:aa7:8b84:0:b0:6e4:8df7:d52e with SMTP id r4-20020aa78b84000000b006e48df7d52emr2023122pfd.18.1708766742105; Sat, 24 Feb 2024 01:25:42 -0800 (PST) Received: from localhost ([157.82.203.206]) by smtp.gmail.com with UTF8SMTPSA id z20-20020aa785d4000000b006e45dce37basm746214pfn.220.2024.02.24.01.25.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 24 Feb 2024 01:25:41 -0800 (PST) From: Akihiko Odaki Date: Sat, 24 Feb 2024 18:24:44 +0900 Subject: [PATCH v7 12/16] hw/pci: Replace -1 with UINT32_MAX for romsize MIME-Version: 1.0 Message-Id: <20240224-reuse-v7-12-29c14bcb952e@daynix.com> References: <20240224-reuse-v7-0-29c14bcb952e@daynix.com> In-Reply-To: <20240224-reuse-v7-0-29c14bcb952e@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 Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org, Akihiko Odaki X-Mailer: b4 0.12.3 Received-SPF: none client-ip=2607:f8b0:4864:20::433; envelope-from=akihiko.odaki@daynix.com; helo=mail-pf1-x433.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, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.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. Fix these issues by replacing -1 with UINT32_MAX for statements involving the variable. 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 54b375da2d26..84df07a2789b 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);