[PATCH 2/5] hw/pci-bridge: Do not declare local variable only used for assertion

Philippe Mathieu-Daudé posted 5 patches 5 years, 2 months ago
[PATCH 2/5] hw/pci-bridge: Do not declare local variable only used for assertion
Posted by Philippe Mathieu-Daudé 5 years, 2 months ago
This variable is used once in an assertion. Remove single
declaration and access directly in the assert().

See in "qemu/osdep.h":

 *                                  [...] disable assertion is not
 * supported upstream so the risk is all yours.  Meanwhile, please
 * submit patches to remove any side-effects inside an assertion, or
 * fixing error handling that should use Error instead of assert.

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 hw/pci-bridge/pci_bridge_dev.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/hw/pci-bridge/pci_bridge_dev.c b/hw/pci-bridge/pci_bridge_dev.c
index 4a080b7c7bf..10c521085d7 100644
--- a/hw/pci-bridge/pci_bridge_dev.c
+++ b/hw/pci-bridge/pci_bridge_dev.c
@@ -224,9 +224,7 @@ void pci_bridge_dev_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
 void pci_bridge_dev_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
                               Error **errp)
 {
-    PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);
-
-    g_assert(shpc_present(pci_hotplug_dev));
+    g_assert(shpc_present(PCI_DEVICE(hotplug_dev)));
     shpc_device_unplug_cb(hotplug_dev, dev, errp);
 }
 
-- 
2.26.2


Re: [PATCH 2/5] hw/pci-bridge: Do not declare local variable only used for assertion
Posted by Greg Kurz 5 years, 2 months ago
On Wed,  2 Sep 2020 10:07:58 +0200
Philippe Mathieu-Daudé <philmd@redhat.com> wrote:

> This variable is used once in an assertion. Remove single
> declaration and access directly in the assert().
> 
> See in "qemu/osdep.h":
> 
>  *                                  [...] disable assertion is not
>  * supported upstream so the risk is all yours.  Meanwhile, please
>  * submit patches to remove any side-effects inside an assertion, or
>  * fixing error handling that should use Error instead of assert.
> 

Not sure how this relates to what the patch is doing, ie. get rid
of a local variable with a single user...

> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> ---
>  hw/pci-bridge/pci_bridge_dev.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/hw/pci-bridge/pci_bridge_dev.c b/hw/pci-bridge/pci_bridge_dev.c
> index 4a080b7c7bf..10c521085d7 100644
> --- a/hw/pci-bridge/pci_bridge_dev.c
> +++ b/hw/pci-bridge/pci_bridge_dev.c
> @@ -224,9 +224,7 @@ void pci_bridge_dev_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
>  void pci_bridge_dev_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
>                                Error **errp)
>  {
> -    PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);
> -
> -    g_assert(shpc_present(pci_hotplug_dev));
> +    g_assert(shpc_present(PCI_DEVICE(hotplug_dev)));

I don't think this is correct because PCI_DEVICE() has a side-effect: it
causes QEMU to abort() if dev doesn't point to a PCI device. I guess that
if a non-PCI device reaches this code, something is clearly wrong and we
should abort(), even if QEMU was built without assertions.

>      shpc_device_unplug_cb(hotplug_dev, dev, errp);
>  }
>