In set_drive_helper(), we call visit_type_str() to get the value
being set as a string. This is an allocated string, and we're
supposed to free it, but not all paths out of this function do so.
In particular, the "block backend already exists, change the attached
node" code path does not free the string. This shows up as a memory
leak under the newly added virtio-scsi-tests/replaced-node-size
qtest:
$
UBSAN_OPTIONS=halt_on_error=1:abort_on_error=1:print_summary=1:print_stacktrace=1
\
QTEST_QEMU_BINARY=./qemu-system-i386 \
ASAN_OPTIONS=halt_on_error=1:abort_on_error=1:print_summary=1:print_stacktrace=1
\
./tests/qtest/qos-test --tap -k \
-p
/i386/pc/i440FX-pcihost/pci-bus-pc/pci-bus/virtio-scsi-pci/virtio-scsi/virtio-scsi-tests/replaced-node-size
[...]
Direct leak of 4 byte(s) in 1 object(s) allocated from:
#0 0x55f7744155e3 in malloc
(/home/pm215/qemu/build/x86-tgt-san-dbg/qemu-system-i386+0x20115e3) (BuildId:
271ffb42d2e322de7813813d8c6cc85351e48a4f)
#1 0x713dbd345b79 in g_malloc
/usr/src/glib2.0-2.80.0-6ubuntu3.10/debian/build/deb/../../../glib/gmem.c:100:13
#2 0x713dbd35b988 in g_strdup
/usr/src/glib2.0-2.80.0-6ubuntu3.10/debian/build/deb/../../../glib/gstrfuncs.c:323:17
#3 0x55f777208021 in g_strdup_inline
/usr/include/glib-2.0/glib/gstrfuncs.h:321:10
#4 0x55f777208021 in qobject_input_type_str
/home/pm215/qemu/build/x86-tgt-san-dbg/../../qapi/qobject-input-visitor.c:542:12
#5 0x55f777204d1c in visit_type_str
/home/pm215/qemu/build/x86-tgt-san-dbg/../../qapi/qapi-visit-core.c:349:10
#6 0x55f774df80d1 in set_drive_helper
/home/pm215/qemu/build/x86-tgt-san-dbg/../../hw/core/qdev-properties-system.c:129:10
#7 0x55f774dee9fa in set_drive_iothread
/home/pm215/qemu/build/x86-tgt-san-dbg/../../hw/core/qdev-properties-system.c:224:5
#8 0x55f7767cc5c8 in field_prop_set
/home/pm215/qemu/build/x86-tgt-san-dbg/../../hw/core/qdev-properties.c:89:12
#9 0x55f7767ec12b in object_property_set
/home/pm215/qemu/build/x86-tgt-san-dbg/../../qom/object.c:1562:5
Switch to using g_autofree so that the string is properly freed
all all exit paths.
Cc: [email protected]
Fixes: d1a58c176a11b ("qdev: allow setting drive property for realized device")
Signed-off-by: Peter Maydell <[email protected]>
---
Sorry for forgetting qemu-devel on first send...
hw/core/qdev-properties-system.c | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/hw/core/qdev-properties-system.c b/hw/core/qdev-properties-system.c
index 4d605c20561..d5b227dbecb 100644
--- a/hw/core/qdev-properties-system.c
+++ b/hw/core/qdev-properties-system.c
@@ -120,7 +120,7 @@ static void set_drive_helper(Object *obj, Visitor *v, const
char *name,
DeviceState *dev = DEVICE(obj);
const Property *prop = opaque;
void **ptr = object_field_prop_ptr(obj, prop);
- char *str;
+ g_autofree char *str = NULL;
BlockBackend *blk;
bool blk_created = false;
int ret;
@@ -155,7 +155,6 @@ static void set_drive_helper(Object *obj, Visitor *v, const
char *name,
}
if (!*str) {
- g_free(str);
*ptr = NULL;
return;
}
@@ -209,8 +208,6 @@ fail:
/* If we need to keep a reference, blk_attach_dev() took it */
blk_unref(blk);
}
-
- g_free(str);
}
static void set_drive(Object *obj, Visitor *v, const char *name, void *opaque,
--
2.43.0