misc: Reduce assert side-effects
cover misc: Reduce assert side-effects
Fix a Coverity warning in qtest,
do not declare local variable only used for assertions (use them directly).
Philippe Mathieu-Daudé (5):
tests/qtest/ipmi-kcs: Fix assert side-effect hw/pci-bridge: Do not declare local variable only used for assertion hw/ppc/spapr: Do not declare local variable only used for assertion tcg/tcg: Do not declare local variable only used for assertion util/qsp: Do not declare local variable only used for assertion
hw/pci-bridge/pci_bridge_dev.c | 4 +--- hw/ppc/spapr.c | 3 +-- tcg/tcg.c | 3 +-- tests/qtest/ipmi-kcs-test.c | 3 ++- util/qsp.c | 4 +--- 5 files changed, 6 insertions(+), 11 deletions(-)
Signature · 1 line
2.26.2
PATCH 1/5 tests/qtest/ipmi-kcs: Fix assert side-effect
Fix assert side-effect reported by Coverity:
/qemu/tests/qtest/ipmi-kcs-test.c: 84 in kcs_wait_obf()
83 while (IPMI_KCS_CMDREG_GET_OBF() == 0) {
>>> CID 1432368: Incorrect expression (ASSERT_SIDE_EFFECT)
>>> Argument "--count" of g_assert() has a side effect. The containing function might work differently in a non-debug build.
84 g_assert(--count != 0);--- tests/qtest/ipmi-kcs-test.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tests/qtest/ipmi-kcs-test.c +2 -1
@@ -81,7 +81,8 @@ static void kcs_wait_obf(void) {unsigned int count =1000 ;while (IPMI_KCS_CMDREG_GET_OBF () ==0 ) { -g_assert (--count !=0 ); + --count; +g_assert (count !=0 ); } }
Signature · 1 line
2.26.2
Re: [PATCH 1/5] tests/qtest/ipmi-kcs: Fix assert side-effect
Quoted 11 lines · On Wed, 2 Sep 2020 10:07:57 +0200 Philippe Mathieu-Daudé <[email protected]>…
Fix assert side-effect reported by Coverity:
/qemu/tests/qtest/ipmi-kcs-test.c: 84 in kcs_wait_obf() 83 while (IPMI_KCS_CMDREG_GET_OBF() == 0) { >>> CID 1432368: Incorrect expression (ASSERT_SIDE_EFFECT) >>> Argument "--count" of g_assert() has a side effect. The containing function might work differently in a non-debug build. 84 g_assert(--count != 0);Reported-by Coverity (CID 1432368)Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
Quoted 17 lines
tests/qtest/ipmi-kcs-test.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)tests/qtest/ipmi-kcs-test.c +2
-1@@ -81,7 +81,8 @@ static void kcs_wait_obf(void) {unsigned int count =1000 ;while (IPMI_KCS_CMDREG_GET_OBF () ==0 ) { -g_assert (--count !=0 ); + --count; +g_assert (count !=0 ); } }
Re: [PATCH 1/5] tests/qtest/ipmi-kcs: Fix assert side-effect
In reply to Philippe Mathieu-DaudéQuoted 20 lines · Philippe Mathieu-Daudé
Fix assert side-effect reported by Coverity:
/qemu/tests/qtest/ipmi-kcs-test.c: 84 in kcs_wait_obf() 83 while (IPMI_KCS_CMDREG_GET_OBF() == 0) { >>> CID 1432368: Incorrect expression (ASSERT_SIDE_EFFECT) >>> Argument "--count" of g_assert() has a side effect. The containing function might work differently in a non-debug build. 84 g_assert(--count != 0);Reported-by Coverity (CID 1432368)Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- tests/qtest/ipmi-kcs-test.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)tests/qtest/ipmi-kcs-test.c +0
-0
tests/qtest/ipmi-kcs-test.c static void kcs_wait_obf(void) · line 88
Quoted 9 lines
tests/qtest/ipmi-kcs-test.c +2
-1@@ -81,7 +81,8 @@ static void kcs_wait_obf(void) {unsigned int count =1000 ;while (IPMI_KCS_CMDREG_GET_OBF () ==0 ) { -g_assert (--count !=0 ); + --count; +g_assert (count !=0 ); } }
You could also use g_assert_true() instead. Anyway:
PATCH 2/5 hw/pci-bridge: Do not declare local variable only used for assertion
In reply to Philippe Mathieu-Daudé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.
--- hw/pci-bridge/pci_bridge_dev.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
hw/pci-bridge/pci_bridge_dev.c +1 -3
@@ -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); }
Signature · 1 line
2.26.2
Re: [PATCH 2/5] hw/pci-bridge: Do not declare local variable only used for assertion
Quoted 10 lines · On Wed, 2 Sep 2020 10:07:58 +0200 Philippe Mathieu-Daudé <[email protected]>…
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...
Quoted 10 lines
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/pci-bridge/pci_bridge_dev.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)hw/pci-bridge/pci_bridge_dev.c +0
-0
hw/pci-bridge/pci_bridge_dev.c void pci_bridge_dev_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev, · line 227
Quoted 7 lines
hw/pci-bridge/pci_bridge_dev.c +1
-3@@ -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.
Quoted 3 lines
hw/pci-bridge/pci_bridge_dev.c +0
-0shpc_device_unplug_cb (hotplug_dev, dev, errp); }
PATCH 3/5 hw/ppc/spapr: Do not declare local variable only used for assertion
In reply to Philippe Mathieu-Daudé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.
--- hw/ppc/spapr.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
hw/ppc/spapr.c +1 -2
@@ -628,7 +628,6 @@ static int spapr_dt_dynamic_reconfiguration_memory(SpaprMachineState *spapr,static int spapr_dt_memory (SpaprMachineState *spapr,void *fdt) {MachineState *machine =MACHINE (spapr); -SpaprMachineClass *smc =SPAPR_MACHINE_GET_CLASS (spapr);hwaddr mem_start, node_size;int i, nb_nodes = machine->numa_state->num_nodes;NodeInfo *nodes = machine->numa_state->nodes;
@@ -670,7 +669,7 @@ static int spapr_dt_memory(SpaprMachineState *spapr, void *fdt)if (spapr_ovec_test (spapr->ov5_cas,OV5_DRCONF_MEMORY )) {int ret; -g_assert (smc->dr_lmb_enabled); +g_assert (SPAPR_MACHINE_GET_CLASS (spapr)->dr_lmb_enabled); ret =spapr_dt_dynamic_reconfiguration_memory (spapr, fdt);if (ret) {return ret;
Signature · 1 line
2.26.2
Re: [PATCH 3/5] hw/ppc/spapr: Do not declare local variable only used for assertion
Quoted 5 lines · On Wed, 2 Sep 2020 10:07:59 +0200 Philippe Mathieu-Daudé <[email protected]>…
This variable is used once in an assertion. Remove single declaration and access directly in the
assert().See in "
qemu/osdep.h":
Quoted 7 lines
* [...] 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é <[email protected]>---
Same remarks as patch 2/5.
Quoted 24 lines
hw/ppc/spapr.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)hw/ppc/spapr.c +1
-2@@ -628,7 +628,6 @@ static int spapr_dt_dynamic_reconfiguration_memory(SpaprMachineState *spapr,static int spapr_dt_memory (SpaprMachineState *spapr,void *fdt) {MachineState *machine =MACHINE (spapr); -SpaprMachineClass *smc =SPAPR_MACHINE_GET_CLASS (spapr);hwaddr mem_start, node_size;int i, nb_nodes = machine->numa_state->num_nodes;NodeInfo *nodes = machine->numa_state->nodes;@@ -670,7 +669,7 @@ static int spapr_dt_memory(SpaprMachineState *spapr, void *fdt)if (spapr_ovec_test (spapr->ov5_cas,OV5_DRCONF_MEMORY )) {int ret; -g_assert (smc->dr_lmb_enabled); +g_assert (SPAPR_MACHINE_GET_CLASS (spapr)->dr_lmb_enabled); ret =spapr_dt_dynamic_reconfiguration_memory (spapr, fdt);if (ret) {return ret;
PATCH 4/5 tcg/tcg: Do not declare local variable only used for assertion
In reply to Philippe Mathieu-Daudé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.
--- tcg/tcg.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
tcg/tcg.c +1 -2
@@ -777,7 +777,6 @@ void tcg_register_thread(void)#else void tcg_register_thread (void ) { -MachineState *ms =MACHINE (qdev_get_machine ());TCGContext *s =g_malloc (sizeof (*s));unsigned int i, n;bool err;
@@ -795,7 +794,7 @@ void tcg_register_thread(void)/* Claim an entry in tcg_ctxs */ n =atomic_fetch_inc (&n_tcg_ctxs); -g_assert (n < ms->smp.max_cpus); +g_assert (n <MACHINE (qdev_get_machine ())->smp.max_cpus);atomic_set (&tcg_ctxs[n], s);if (n >0 ) {
Signature · 1 line
2.26.2
Re: [PATCH 4/5] tcg/tcg: Do not declare local variable only used for assertion
Quoted 5 lines · On Wed, 2 Sep 2020 10:08:00 +0200 Philippe Mathieu-Daudé <[email protected]>…
This variable is used once in an assertion. Remove single declaration and access directly in the
assert().See in "
qemu/osdep.h":
Quoted 7 lines
* [...] 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é <[email protected]>---
Ditto.
Quoted 24 lines
tcg/tcg.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)tcg/tcg.c +1
-2@@ -777,7 +777,6 @@ void tcg_register_thread(void)#else void tcg_register_thread (void ) { -MachineState *ms =MACHINE (qdev_get_machine ());TCGContext *s =g_malloc (sizeof (*s));unsigned int i, n;bool err;@@ -795,7 +794,7 @@ void tcg_register_thread(void)/* Claim an entry in tcg_ctxs */ n =atomic_fetch_inc (&n_tcg_ctxs); -g_assert (n < ms->smp.max_cpus); +g_assert (n <MACHINE (qdev_get_machine ())->smp.max_cpus);atomic_set (&tcg_ctxs[n], s);if (n >0 ) {
PATCH 5/5 util/qsp: Do not declare local variable only used for assertion
In reply to Philippe Mathieu-Daudé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.
--- util/qsp.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
util/qsp.c +1 -3
@@ -560,9 +560,7 @@ static void qsp_iter_diff(void *p, uint32_t hash, void *htp)/* No point in reporting an empty entry */ if (new->n_acqs ==0 && new->ns ==0 ) { -bool removed =qht_remove (ht, new, hash); - -g_assert (removed); +g_assert (qht_remove (ht, new, hash));g_free (new); } }
Signature · 1 line
2.26.2
Re: [PATCH 5/5] util/qsp: Do not declare local variable only used for assertion
Quoted 20 lines · On Wed, 2 Sep 2020 10:08:01 +0200 Philippe Mathieu-Daudé <[email protected]>…
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é <[email protected]>--- util/qsp.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)util/qsp.c +0
-0
util/qsp.c static void qsp_iter_diff(void *p, uint32_t hash, void *htp) · line 563
Quoted 7 lines
util/qsp.c +1
-3@@ -560,9 +560,7 @@ static void qsp_iter_diff(void *p, uint32_t hash, void *htp)/* No point in reporting an empty entry */ if (new->n_acqs ==0 && new->ns ==0 ) { -bool removed =qht_remove (ht, new, hash); - -g_assert (removed); +g_assert (qht_remove (ht, new, hash));
Urgh... this is doing exactly the opposite of the "qemu/osdep.h" recommandations above. We still want to remove new from the hash table even if QEMU was built without assertions.
Quoted 3 lines
util/qsp.c +0
-0g_free (new); } }
Re: [PATCH 5/5] util/qsp: Do not declare local variable only used for assertion
Quoted 24 lines · Greg Kurz
Quoted 21 lines · On Wed, 2 Sep 2020 10:08:01 +0200 Philippe Mathieu-Daudé <[email protected]>…
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é <[email protected]>--- util/qsp.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)util/qsp.c +0
-0@@ -560,9 +560,7 @@ static void qsp_iter_diff(void *p, uint32_t hash, void *htp)
Quoted 10 lines
util/qsp.c static void qsp_iter_diff(void *p, uint32_t hash, void *htp) · line 563
Quoted 6 lines
util/qsp.c +1
-3/* No point in reporting an empty entry */ if (new->n_acqs ==0 && new->ns ==0 ) { -bool removed =qht_remove (ht, new, hash); - -g_assert (removed); +g_assert (qht_remove (ht, new, hash));Urgh... this is doing exactly the opposite of the "
qemu/osdep.h" recommandations above. We still want to remove new from the hash table even if QEMU was built without assertions.
Oops you are right :/
Quoted 5 lines
Quoted 4 lines
util/qsp.c +0
-0g_free (new); } }