diff mbox series

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

Message ID 20200902080801.160652-3-philmd@redhat.com
State New
Headers show
Series misc: Reduce assert side-effects | expand

Commit Message

Philippe Mathieu-Daudé Sept. 2, 2020, 8:07 a.m. UTC
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(-)

Comments

Greg Kurz Sept. 2, 2020, 8:59 a.m. UTC | #1
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);
>  }
>
diff mbox series

Patch

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);
 }