[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 07/31] qemu/queue.h: add QLIST_SAFE_REMOVE()
From: |
Stefan Hajnoczi |
Subject: |
[PULL 07/31] qemu/queue.h: add QLIST_SAFE_REMOVE() |
Date: |
Sat, 22 Feb 2020 08:50:06 +0000 |
QLIST_REMOVE() assumes the element is in a list. It also leaves the
element's linked list pointers dangling.
Introduce a safe version of QLIST_REMOVE() and convert open-coded
instances of this pattern.
Signed-off-by: Stefan Hajnoczi <address@hidden>
Reviewed-by: Sergio Lopez <address@hidden>
Message-id: address@hidden
Signed-off-by: Stefan Hajnoczi <address@hidden>
---
block.c | 5 +----
chardev/spice.c | 4 +---
include/qemu/queue.h | 14 ++++++++++++++
3 files changed, 16 insertions(+), 7 deletions(-)
diff --git a/block.c b/block.c
index 308a91c96b..1bdb9c679d 100644
--- a/block.c
+++ b/block.c
@@ -2636,10 +2636,7 @@ BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
static void bdrv_detach_child(BdrvChild *child)
{
- if (child->next.le_prev) {
- QLIST_REMOVE(child, next);
- child->next.le_prev = NULL;
- }
+ QLIST_SAFE_REMOVE(child, next);
bdrv_replace_child(child, NULL);
diff --git a/chardev/spice.c b/chardev/spice.c
index 241e2b7770..bf7ea1e294 100644
--- a/chardev/spice.c
+++ b/chardev/spice.c
@@ -216,9 +216,7 @@ static void char_spice_finalize(Object *obj)
vmc_unregister_interface(s);
- if (s->next.le_prev) {
- QLIST_REMOVE(s, next);
- }
+ QLIST_SAFE_REMOVE(s, next);
g_free((char *)s->sin.subtype);
g_free((char *)s->sin.portname);
diff --git a/include/qemu/queue.h b/include/qemu/queue.h
index fcecb70228..60e794a4e3 100644
--- a/include/qemu/queue.h
+++ b/include/qemu/queue.h
@@ -144,6 +144,20 @@ struct {
\
*(elm)->field.le_prev = (elm)->field.le_next; \
} while (/*CONSTCOND*/0)
+/*
+ * Like QLIST_REMOVE() but safe to call when elm is not in a list
+ */
+#define QLIST_SAFE_REMOVE(elm, field) do { \
+ if ((elm)->field.le_prev != NULL) { \
+ if ((elm)->field.le_next != NULL) \
+ (elm)->field.le_next->field.le_prev = \
+ (elm)->field.le_prev; \
+ *(elm)->field.le_prev = (elm)->field.le_next; \
+ (elm)->field.le_next = NULL; \
+ (elm)->field.le_prev = NULL; \
+ } \
+} while (/*CONSTCOND*/0)
+
#define QLIST_FOREACH(var, head, field) \
for ((var) = ((head)->lh_first); \
(var); \
--
2.24.1
- [PULL 00/31] Block patches, Stefan Hajnoczi, 2020/02/22
- [PULL 01/31] virtio: increase virtqueue size for virtio-scsi and virtio-blk, Stefan Hajnoczi, 2020/02/22
- [PULL 02/31] aio-posix: avoid reacquiring rcu_read_lock() when polling, Stefan Hajnoczi, 2020/02/22
- [PULL 03/31] rcu_queue: add QSLIST functions, Stefan Hajnoczi, 2020/02/22
- [PULL 04/31] util/async: make bh_aio_poll() O(1), Stefan Hajnoczi, 2020/02/22
- [PULL 05/31] aio-posix: fix use after leaving scope in aio_poll(), Stefan Hajnoczi, 2020/02/22
- [PULL 06/31] aio-posix: don't pass ns timeout to epoll_wait(), Stefan Hajnoczi, 2020/02/22
- [PULL 07/31] qemu/queue.h: add QLIST_SAFE_REMOVE(),
Stefan Hajnoczi <=
- [PULL 08/31] aio-posix: make AioHandler deletion O(1), Stefan Hajnoczi, 2020/02/22
- [PULL 09/31] aio-posix: make AioHandler dispatch O(1) with epoll, Stefan Hajnoczi, 2020/02/22
- [PULL 10/31] softmmu: move vl.c to softmmu/, Stefan Hajnoczi, 2020/02/22
- [PULL 11/31] softmmu: split off vl.c:main() into main.c, Stefan Hajnoczi, 2020/02/22
- [PULL 12/31] module: check module wasn't already initialized, Stefan Hajnoczi, 2020/02/22
- [PULL 13/31] fuzz: add FUZZ_TARGET module type, Stefan Hajnoczi, 2020/02/22
- [PULL 14/31] qtest: add qtest_server_send abstraction, Stefan Hajnoczi, 2020/02/22
- [PULL 15/31] libqtest: add a layer of abstraction to send/recv, Stefan Hajnoczi, 2020/02/22
- [PULL 16/31] libqtest: make bufwrite rely on the TransportOps, Stefan Hajnoczi, 2020/02/22
- [PULL 17/31] qtest: add in-process incoming command handler, Stefan Hajnoczi, 2020/02/22