Skip to content

Commit f07f64c

Browse files
pm215Michael Tokarev
authored andcommitted
hw: Make qdev_get_printable_name() consistently return freeable string
The current implementation of qdev_get_printable_name() sometimes returns a string that must not be freed (vdev->id or the fixed fallback string "<unknown device>" and sometimes returns a string that must be freed (the return value of qdev_get_dev_path()). This forces callers to leak the string in the "must be freed" case. Make the function consistent that it always returns a string that the caller must free, and make the three callsites free it. This fixes leaks like this that show up when running "make check" with the address sanitizer enabled: Direct leak of 13 byte(s) in 1 object(s) allocated from: #0 0x5561de21f293 in malloc (/home/pm215/qemu/build/san/qemu-system-i386+0x1a2d293) (BuildId: 6d6fad7130fd5c8dbbc03401df554f68b8034936) #1 0x767ad7a82ac9 in g_malloc (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x62ac9) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3) #2 0x5561deaf34f2 in pcibus_get_dev_path /home/pm215/qemu/build/san/../../hw/pci/pci.c:2792:12 #3 0x5561df9d8830 in qdev_get_printable_name /home/pm215/qemu/build/san/../../hw/core/qdev.c:431:24 #4 0x5561deebdca2 in virtio_init_region_cache /home/pm215/qemu/build/san/../../hw/virtio/virtio.c:298:17 #5 0x5561df05f842 in memory_region_write_accessor /home/pm215/qemu/build/san/../../system/memory.c:491:5 #6 0x5561df05ed1b in access_with_adjusted_size /home/pm215/qemu/build/san/../../system/memory.c:567:18 #7 0x5561df05e3fa in memory_region_dispatch_write /home/pm215/qemu/build/san/../../system/memory.c #8 0x5561df0aa805 in address_space_stm_internal /home/pm215/qemu/build/san/../../system/memory_ldst.c.inc:85:13 #9 0x5561df0bcad3 in qtest_process_command /home/pm215/qemu/build/san/../../system/qtest.c:480:13 Cc: qemu-stable@nongnu.org Fixes: e209d4d ("virtio: improve virtqueue mapping error messages") Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-ID: <20260307155046.3940197-3-peter.maydell@linaro.org> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> (cherry picked from commit 1e3e1d5) Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
1 parent a69152d commit f07f64c

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

hw/core/qdev.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ const char *qdev_get_printable_name(DeviceState *vdev)
420420
* names.
421421
*/
422422
if (vdev->id) {
423-
return vdev->id;
423+
return g_strdup(vdev->id);
424424
}
425425
/*
426426
* Fall back to the canonical QOM device path (eg. ID for PCI
@@ -437,7 +437,7 @@ const char *qdev_get_printable_name(DeviceState *vdev)
437437
* Final fallback: if all else fails, return a placeholder string.
438438
* This ensures the error message always contains a valid string.
439439
*/
440-
return "<unknown device>";
440+
return g_strdup("<unknown device>");
441441
}
442442

443443
void qdev_add_unplug_blocker(DeviceState *dev, Error *reason)

hw/virtio/virtio.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,32 +258,38 @@ void virtio_init_region_cache(VirtIODevice *vdev, int n)
258258
len = address_space_cache_init(&new->desc, vdev->dma_as,
259259
addr, size, packed);
260260
if (len < size) {
261+
g_autofree const char *devname = qdev_get_printable_name(DEVICE(vdev));
262+
261263
virtio_error(vdev,
262264
"Failed to map descriptor ring for device %s: "
263265
"invalid guest physical address or corrupted queue setup",
264-
qdev_get_printable_name(DEVICE(vdev)));
266+
devname);
265267
goto err_desc;
266268
}
267269

268270
size = virtio_queue_get_used_size(vdev, n);
269271
len = address_space_cache_init(&new->used, vdev->dma_as,
270272
vq->vring.used, size, true);
271273
if (len < size) {
274+
g_autofree const char *devname = qdev_get_printable_name(DEVICE(vdev));
275+
272276
virtio_error(vdev,
273277
"Failed to map used ring for device %s: "
274278
"possible guest misconfiguration or insufficient memory",
275-
qdev_get_printable_name(DEVICE(vdev)));
279+
devname);
276280
goto err_used;
277281
}
278282

279283
size = virtio_queue_get_avail_size(vdev, n);
280284
len = address_space_cache_init(&new->avail, vdev->dma_as,
281285
vq->vring.avail, size, false);
282286
if (len < size) {
287+
g_autofree const char *devname = qdev_get_printable_name(DEVICE(vdev));
288+
283289
virtio_error(vdev,
284290
"Failed to map avalaible ring for device %s: "
285291
"possible queue misconfiguration or overlapping memory region",
286-
qdev_get_printable_name(DEVICE(vdev)));
292+
devname);
287293
goto err_avail;
288294
}
289295

include/hw/qdev-core.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,22 @@ bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp);
10651065
extern bool qdev_hot_removed;
10661066

10671067
char *qdev_get_dev_path(DeviceState *dev);
1068+
1069+
/**
1070+
* qdev_get_printable_name: Return human readable name for device
1071+
* @dev: Device to get name of
1072+
*
1073+
* Returns: A newly allocated string containing some human
1074+
* readable name for the device, suitable for printing in
1075+
* user-facing error messages. The function will never return NULL,
1076+
* so the name can be used without further checking or fallbacks.
1077+
*
1078+
* If the device has an explicitly set ID (e.g. by the user on the
1079+
* command line via "-device thisdev,id=myid") this is preferred.
1080+
* Otherwise we try the canonical QOM device path (which will be
1081+
* the PCI ID for PCI devices, for example). If all else fails
1082+
* we will return the placeholder "<unknown device">.
1083+
*/
10681084
const char *qdev_get_printable_name(DeviceState *dev);
10691085

10701086
void qbus_set_hotplug_handler(BusState *bus, Object *handler);

0 commit comments

Comments
 (0)