hw/arm/xlnx-zynqmp.c: fix some error-handling code
cover hw/arm/xlnx-zynqmp.c: fix some error-handling code
This series fixes some minor bugs in error-handling paths in the xlnx-zynqmp device. The first is a memory leak spotted by Coverity; the second is some missing error-propagation code that I noticed while writing the first patch.
thanks
-- PMM
Peter Maydell (2):
hw/arm/xlnx-zynqmp.c: Avoid memory leak in error-return path hw/arm/xlnx-zynqmp.c: Add missing error-propagation code
hw/arm/xlnx-zynqmp.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-)
Signature · 1 line
2.20.1
PATCH 1/2 hw/arm/xlnx-zynqmp.c: Avoid memory leak in error-return path
In xlnx_zynqmp_realize() if the attempt to realize the SD controller object fails then the error-return path will leak the 'bus_name' string. Fix this by deferring the allocation until after the realize has succeeded.
--- hw/arm/xlnx-zynqmp.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/arm/xlnx-zynqmp.c +2 -1
@@ -520,7 +520,7 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)sysbus_connect_irq (SYS_BUS_DEVICE (&s->sata),0 , gic_spi[SATA_INTR ]);for (i =0 ; i <XLNX_ZYNQMP_NUM_SDHCI ; i++) { -char *bus_name =g_strdup_printf ("sd-bus%d" , i); +char *bus_name;SysBusDevice *sbd =SYS_BUS_DEVICE (&s->sdhci[i]);Object *sdhci =OBJECT (&s->sdhci[i]);
@@ -541,6 +541,7 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)sysbus_connect_irq (sbd,0 , gic_spi[sdhci_intr[i]]);/* Alias controller SD bus to the SoC itself */ + bus_name =g_strdup_printf ("sd-bus%d" , i);object_property_add_alias (OBJECT (s), bus_name, sdhci,"sd-bus" , &error_abort);g_free (bus_name);
Signature · 1 line
2.20.1
Re: [PATCH 1/2] hw/arm/xlnx-zynqmp.c: Avoid memory leak in error-return path
Quoted 26 lines · Peter Maydell
In
xlnx_zynqmp_realize()if the attempt to realize the SD controller object fails then the error-return path will leak the 'bus_name' string. Fix this by deferring the allocation until after the realize has succeeded.Fixes Coverity CID 1421911Signed-off-by Peter Maydell <[email protected]>--- hw/arm/xlnx-zynqmp.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)hw/arm/xlnx-zynqmp.c +1
-1@@ -520,7 +520,7 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)sysbus_connect_irq (SYS_BUS_DEVICE (&s->sata),0 , gic_spi[SATA_INTR ]);for (i =0 ; i <XLNX_ZYNQMP_NUM_SDHCI ; i++) { -char *bus_name =g_strdup_printf ("sd-bus%d" , i); +char *bus_name;SysBusDevice *sbd =SYS_BUS_DEVICE (&s->sdhci[i]);Object *sdhci =OBJECT (&s->sdhci[i]);
hw/arm/xlnx-zynqmp.c static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp) · line 547
Quoted 7 lines
hw/arm/xlnx-zynqmp.c +1
-0@@ -541,6 +541,7 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)sysbus_connect_irq (sbd,0 , gic_spi[sdhci_intr[i]]);/* Alias controller SD bus to the SoC itself */ + bus_name =g_strdup_printf ("sd-bus%d" , i);object_property_add_alias (OBJECT (s), bus_name, sdhci,"sd-bus" , &error_abort);g_free (bus_name);
Re: [PATCH 1/2] hw/arm/xlnx-zynqmp.c: Avoid memory leak in error-return path
In reply to Peter MaydellQuoted 8 lines · Peter Maydell
In
xlnx_zynqmp_realize()if the attempt to realize the SD controller object fails then the error-return path will leak the 'bus_name' string. Fix this by deferring the allocation until after the realize has succeeded.Fixes Coverity CID 1421911Signed-off-by Peter Maydell <[email protected]>
Alistair
Quoted 29 lines
--- hw/arm/xlnx-zynqmp.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)hw/arm/xlnx-zynqmp.c +2
-2@@ -520,7 +520,7 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)sysbus_connect_irq (SYS_BUS_DEVICE (&s->sata),0 , gic_spi[SATA_INTR ]);for (i =0 ; i <XLNX_ZYNQMP_NUM_SDHCI ; i++) { -char *bus_name =g_strdup_printf ("sd-bus%d" , i); +char *bus_name;SysBusDevice *sbd =SYS_BUS_DEVICE (&s->sdhci[i]);Object *sdhci =OBJECT (&s->sdhci[i]);@@ -541,6 +541,7 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)sysbus_connect_irq (sbd,0 , gic_spi[sdhci_intr[i]]);/* Alias controller SD bus to the SoC itself */ + bus_name =g_strdup_printf ("sd-bus%d" , i);object_property_add_alias (OBJECT (s), bus_name, sdhci,"sd-bus" , &error_abort);g_free (bus_name); --2.20.1
PATCH 2/2 hw/arm/xlnx-zynqmp.c: Add missing error-propagation code
In reply to Peter MaydellIn some places in xlnx_zynqmp_realize() we were putting an error into our local Error*, but forgetting to check for failure and pass it back to the caller. Add the missing code.
---
Spotted while I was writing the previous patch.
--- hw/arm/xlnx-zynqmp.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
hw/arm/xlnx-zynqmp.c +24 -0
@@ -530,8 +530,20 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)* - eMMC Specification Version 4.51 */ object_property_set_uint (sdhci,3 ,"sd-spec-version" , &err); +if (err) { +error_propagate (errp, err); +return ; + }object_property_set_uint (sdhci,SDHCI_CAPABILITIES ,"capareg" , &err); +if (err) { +error_propagate (errp, err); +return ; + }object_property_set_uint (sdhci,UHS_I ,"uhs" , &err); +if (err) { +error_propagate (errp, err); +return ; + }object_property_set_bool (sdhci, true,"realized" , &err);if (err) {error_propagate (errp, err);
@@ -551,6 +563,10 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)gchar *bus_name;object_property_set_bool (OBJECT (&s->spi[i]), true,"realized" , &err); +if (err) { +error_propagate (errp, err); +return ; + }sysbus_mmio_map (SYS_BUS_DEVICE (&s->spi[i]),0 , spi_addr[i]); sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi[i]), 0,
@@ -565,6 +581,10 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp) }object_property_set_bool (OBJECT (&s->qspi), true,"realized" , &err); +if (err) { +error_propagate (errp, err); +return ; + }sysbus_mmio_map (SYS_BUS_DEVICE (&s->qspi),0 ,QSPI_ADDR );sysbus_mmio_map (SYS_BUS_DEVICE (&s->qspi),1 ,LQSPI_ADDR );sysbus_connect_irq (SYS_BUS_DEVICE (&s->qspi),0 , gic_spi[QSPI_IRQ ]);
@@ -619,6 +639,10 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)for (i =0 ; i <XLNX_ZYNQMP_NUM_GDMA_CH ; i++) {object_property_set_uint (OBJECT (&s->gdma[i]),128 ,"bus-width" , &err); +if (err) { +error_propagate (errp, err); +return ; + }object_property_set_bool (OBJECT (&s->gdma[i]), true,"realized" , &err);if (err) {error_propagate (errp, err);
Signature · 1 line
2.20.1
Re: [PATCH 2/2] hw/arm/xlnx-zynqmp.c: Add missing error-propagation code
Quoted 3 lines · Peter Maydell
In some places in
xlnx_zynqmp_realize()we were putting an error into our local Error*, but forgetting to check for failure and pass it back to the caller. Add the missing code.
This (incorrect) pattern is used in many places, so it might be easier to fix it once with a Coccinelle script...
i.e. a quick "git grep -3 object_property_set_uint":
hw/arm/bcm2835_peripherals.c:290hw/arm/fsl-imx25.c:272hw/arm/fsl-imx6.c:349hw/i386/x86.c:110hw/microblaze/xlnx-zynqmp-pmu.c:88target/i386/cpu.c:5158
Quoted 67 lines
Signed-off-by Peter Maydell <[email protected]>---
Spotted while I was writing the previous patch.--- hw/arm/xlnx-zynqmp.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)hw/arm/xlnx-zynqmp.c +24
-0@@ -530,8 +530,20 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)* - eMMC Specification Version 4.51 */ object_property_set_uint (sdhci,3 ,"sd-spec-version" , &err); +if (err) { +error_propagate (errp, err); +return ; + }object_property_set_uint (sdhci,SDHCI_CAPABILITIES ,"capareg" , &err); +if (err) { +error_propagate (errp, err); +return ; + }object_property_set_uint (sdhci,UHS_I ,"uhs" , &err); +if (err) { +error_propagate (errp, err); +return ; + }object_property_set_bool (sdhci, true,"realized" , &err);if (err) {error_propagate (errp, err);@@ -551,6 +563,10 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)gchar *bus_name;object_property_set_bool (OBJECT (&s->spi[i]), true,"realized" , &err); +if (err) { +error_propagate (errp, err); +return ; + }sysbus_mmio_map (SYS_BUS_DEVICE (&s->spi[i]),0 , spi_addr[i]); sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi[i]), 0,@@ -565,6 +581,10 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp) }object_property_set_bool (OBJECT (&s->qspi), true,"realized" , &err); +if (err) { +error_propagate (errp, err); +return ; + }sysbus_mmio_map (SYS_BUS_DEVICE (&s->qspi),0 ,QSPI_ADDR );sysbus_mmio_map (SYS_BUS_DEVICE (&s->qspi),1 ,LQSPI_ADDR );sysbus_connect_irq (SYS_BUS_DEVICE (&s->qspi),0 , gic_spi[QSPI_IRQ ]);@@ -619,6 +639,10 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)for (i =0 ; i <XLNX_ZYNQMP_NUM_GDMA_CH ; i++) {object_property_set_uint (OBJECT (&s->gdma[i]),128 ,"bus-width" , &err); +if (err) { +error_propagate (errp, err); +return ; + }object_property_set_bool (OBJECT (&s->gdma[i]), true,"realized" , &err);if (err) {error_propagate (errp, err);
Re: [PATCH 2/2] hw/arm/xlnx-zynqmp.c: Add missing error-propagation code
In reply to Peter MaydellQuoted 6 lines · Peter Maydell
In some places in
xlnx_zynqmp_realize()we were putting an error into our local Error*, but forgetting to check for failure and pass it back to the caller. Add the missing code.Signed-off-by Peter Maydell <[email protected]>
Alistair
Quoted 68 lines
---
Spotted while I was writing the previous patch.--- hw/arm/xlnx-zynqmp.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)hw/arm/xlnx-zynqmp.c +24
-1@@ -530,8 +530,20 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)* - eMMC Specification Version 4.51 */ object_property_set_uint (sdhci,3 ,"sd-spec-version" , &err); +if (err) { +error_propagate (errp, err); +return ; + }object_property_set_uint (sdhci,SDHCI_CAPABILITIES ,"capareg" , &err); +if (err) { +error_propagate (errp, err); +return ; + }object_property_set_uint (sdhci,UHS_I ,"uhs" , &err); +if (err) { +error_propagate (errp, err); +return ; + }object_property_set_bool (sdhci, true,"realized" , &err);if (err) {error_propagate (errp, err);@@ -551,6 +563,10 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)gchar *bus_name;object_property_set_bool (OBJECT (&s->spi[i]), true,"realized" , &err); +if (err) { +error_propagate (errp, err); +return ; + }sysbus_mmio_map (SYS_BUS_DEVICE (&s->spi[i]),0 , spi_addr[i]); sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi[i]), 0,@@ -565,6 +581,10 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp) }object_property_set_bool (OBJECT (&s->qspi), true,"realized" , &err); +if (err) { +error_propagate (errp, err); +return ; + }sysbus_mmio_map (SYS_BUS_DEVICE (&s->qspi),0 ,QSPI_ADDR );sysbus_mmio_map (SYS_BUS_DEVICE (&s->qspi),1 ,LQSPI_ADDR );sysbus_connect_irq (SYS_BUS_DEVICE (&s->qspi),0 , gic_spi[QSPI_IRQ ]);@@ -619,6 +639,10 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)for (i =0 ; i <XLNX_ZYNQMP_NUM_GDMA_CH ; i++) {object_property_set_uint (OBJECT (&s->gdma[i]),128 ,"bus-width" , &err); +if (err) { +error_propagate (errp, err); +return ; + }object_property_set_bool (OBJECT (&s->gdma[i]), true,"realized" , &err);if (err) {error_propagate (errp, err); --2.20.1
Quoted 5 lines · Peter Maydell
This series fixes some minor bugs in error-handling paths in the xlnx-zynqmp device. The first is a memory leak spotted by Coverity; the second is some missing error-propagation code that I noticed while writing the first patch.
Quoted 15 lines
thanks
-- PMMPeter Maydell (2):
hw/arm/xlnx-zynqmp.c: Avoid memory leak in error-return pathhw/arm/xlnx-zynqmp.c: Add missing error-propagation codehw/arm/xlnx-zynqmp.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-)--
2.20.1