Skip to content

Commit

Permalink
qemu/queue.h: add QLIST_SAFE_REMOVE()
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Sergio Lopez <[email protected]>
Message-id: [email protected]
Signed-off-by: Stefan Hajnoczi <[email protected]>
  • Loading branch information
stefanhaRH committed Feb 22, 2020
1 parent ca8c6b2 commit 195ed8c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
5 changes: 1 addition & 4 deletions block.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions chardev/spice.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions include/qemu/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
Expand Down

0 comments on commit 195ed8c

Please sign in to comment.