@samitouri / QOSamiQemu / commits / 4f64d7e336

hw/riscv/virt: free flash devices and OEM strings on finalization

Add instance_finalize to free the two pflash devices (when unrealized) and the OEM ID strings allocated during instance_init. Fixes leaks found by ASan. Fixes: 71eb522c4063 ("riscv/virt: Add the PFlash CFI01 device") Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>

Marc-André Lureau committed Apr 27, 2026 at 16:39 UTC 4f64d7e336391e2886d06fa6e125c5a323b7d579
1 file changed +14
hw/riscv/virt.c
+14
@@ -1745,6 +1745,19 @@ static void virt_machine_init(MachineState *machine)
1745 qemu_add_machine_init_done_notifier(&s->machine_done);
1746 }
1747
1748 +static void virt_machine_instance_finalize(Object *obj)
1749 +{
1750 + RISCVVirtState *s = RISCV_VIRT_MACHINE(obj);
1751 +
1752 + for (int i = 0; i < ARRAY_SIZE(s->flash); i++) {
1753 + if (s->flash[i] && !qdev_is_realized(DEVICE(s->flash[i]))) {
1754 + object_unref(OBJECT(s->flash[i]));
1755 + }
1756 + }
1757 + g_free(s->oem_id);
1758 + g_free(s->oem_table_id);
1759 +}
1760 +
1761 static void virt_machine_instance_init(Object *obj)
1762 {
1763 RISCVVirtState *s = RISCV_VIRT_MACHINE(obj);
@@ -1984,6 +1997,7 @@ static const TypeInfo virt_machine_typeinfo = {
1997 .parent = TYPE_MACHINE,
1998 .class_init = virt_machine_class_init,
1999 .instance_init = virt_machine_instance_init,
2000 + .instance_finalize = virt_machine_instance_finalize,
2001 .instance_size = sizeof(RISCVVirtState),
2002 .interfaces = (const InterfaceInfo[]) {
2003 { TYPE_HOTPLUG_HANDLER },