From nobody Thu Dec 18 22:23:43 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582361932; cv=none; d=zohomail.com; s=zohoarc; b=CL2DlqklB604Ylh3zK6n0jkYLWQsGhXOV75n5qN/WGKClAmNeZRNHOXCqdrNmPIm4ZXsMMH3tXrqdMMrvDoJLSDqUno6Kh8dNqr+qgkM8fpYzJ/vFBOIcTNmxglwa3Ob0ujC6kW6RkeGZwA1XucJGa9NNUi4tPaYWcYKhHoSoN8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582361932; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=HUKq3/ARCCpuk5Uj5F14E13z54NDwFfEVciYGtHeEdc=; b=CrNikYFzwNcjvp8h9CY5rC1JMT9Pf2ILt2tXHTb95+Ruc4fQco1Sfb8gCYn2tC0XKi37GzpIHP/jjyI5r8Hf/rfv2RdxsuJWb1XvTF9WhphyosuwJ2H7A76B1a24wyFBMyXBPXv+uNOKKJjzV+vO/gYuWMySyDvNZXwmSZbOPkE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582361932086459.8146537758273; Sat, 22 Feb 2020 00:58:52 -0800 (PST) Received: from localhost ([::1]:40502 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j5Qcd-0002QE-33 for importer@patchew.org; Sat, 22 Feb 2020 03:58:51 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:38239) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j5QVt-00068N-MO for qemu-devel@nongnu.org; Sat, 22 Feb 2020 03:51:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j5QVs-0000H7-EJ for qemu-devel@nongnu.org; Sat, 22 Feb 2020 03:51:53 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:34546 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j5QVs-0000Fp-9l for qemu-devel@nongnu.org; Sat, 22 Feb 2020 03:51:52 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-430-DQmcNYr8P0qO1WGxS-ZJng-1; Sat, 22 Feb 2020 03:51:50 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BBAC2800D50; Sat, 22 Feb 2020 08:51:48 +0000 (UTC) Received: from localhost (ovpn-116-74.ams2.redhat.com [10.36.116.74]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4A50B5D9CD; Sat, 22 Feb 2020 08:51:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582361511; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HUKq3/ARCCpuk5Uj5F14E13z54NDwFfEVciYGtHeEdc=; b=dC5hxCWLj6MtOYUBQEwatbxPZEXvtoNcVyXD7mtiTVmEQiK6aQCJpe/EhIhsCTN3Xnirsy qLJ3OR5ryIZeiuniwuEgjSbyR6ROTey0xqxxHCgA985bwECZK+SCDWfjOZwAhcMdQ3hIm2 yhi2jJuFjQ0Yzde5S7AnAC3VWbBGAGg= X-MC-Unique: DQmcNYr8P0qO1WGxS-ZJng-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PULL 07/31] qemu/queue.h: add QLIST_SAFE_REMOVE() Date: Sat, 22 Feb 2020 08:50:06 +0000 Message-Id: <20200222085030.1760640-8-stefanha@redhat.com> In-Reply-To: <20200222085030.1760640-1-stefanha@redhat.com> References: <20200222085030.1760640-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Peter Maydell , Thomas Huth , Sergio Lopez , Eduardo Habkost , qemu-block@nongnu.org, "Michael S. Tsirkin" , Laurent Vivier , Max Reitz , Alexander Bulekov , Bandan Das , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , Fam Zheng , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Sergio Lopez Message-id: 20200214171712.541358-4-stefanha@redhat.com Signed-off-by: Stefan Hajnoczi --- 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 *paren= t_bs, =20 static void bdrv_detach_child(BdrvChild *child) { - if (child->next.le_prev) { - QLIST_REMOVE(child, next); - child->next.le_prev =3D NULL; - } + QLIST_SAFE_REMOVE(child, next); =20 bdrv_replace_child(child, NULL); =20 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) =20 vmc_unregister_interface(s); =20 - if (s->next.le_prev) { - QLIST_REMOVE(s, next); - } + QLIST_SAFE_REMOVE(s, next); =20 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 =3D (elm)->field.le_next; \ } while (/*CONSTCOND*/0) =20 +/* + * 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 !=3D NULL) { \ + if ((elm)->field.le_next !=3D NULL) \ + (elm)->field.le_next->field.le_prev =3D \ + (elm)->field.le_prev; \ + *(elm)->field.le_prev =3D (elm)->field.le_next; \ + (elm)->field.le_next =3D NULL; \ + (elm)->field.le_prev =3D NULL; \ + } \ +} while (/*CONSTCOND*/0) + #define QLIST_FOREACH(var, head, field) \ for ((var) =3D ((head)->lh_first); \ (var); \ --=20 2.24.1