From patchwork Thu Feb 20 12:50:28 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 322160 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 8A5052C00A8 for ; Thu, 20 Feb 2014 23:52:45 +1100 (EST) Received: from localhost ([::1]:38128 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WGT7L-0005C3-GS for incoming@patchwork.ozlabs.org; Thu, 20 Feb 2014 07:52:43 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54120) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WGT5h-00031r-Pu for qemu-devel@nongnu.org; Thu, 20 Feb 2014 07:51:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WGT5Z-0001n2-Kc for qemu-devel@nongnu.org; Thu, 20 Feb 2014 07:51:01 -0500 Received: from mx1.redhat.com ([209.132.183.28]:18342) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WGT5Z-0001mq-3l for qemu-devel@nongnu.org; Thu, 20 Feb 2014 07:50:53 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s1KCoon1020151 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 20 Feb 2014 07:50:50 -0500 Received: from localhost (ovpn-112-44.ams2.redhat.com [10.36.112.44]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s1KConBO017303; Thu, 20 Feb 2014 07:50:50 -0500 From: Stefan Hajnoczi To: Date: Thu, 20 Feb 2014 13:50:28 +0100 Message-Id: <1392900630-17608-5-git-send-email-stefanha@redhat.com> In-Reply-To: <1392900630-17608-1-git-send-email-stefanha@redhat.com> References: <1392900630-17608-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Paolo Bonzini , Fam Zheng , Michael Roth Subject: [Qemu-devel] [PATCH v3 4/6] qdev: add get_pointer_and_free() for temporary strings X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org get_pointer() assumes the string has unspecified lifetime (at least as long as the object is alive). In some cases we can only produce a temporary string that should be freed when get_pointer() is done. Signed-off-by: Stefan Hajnoczi --- hw/core/qdev-properties-system.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/hw/core/qdev-properties-system.c b/hw/core/qdev-properties-system.c index 3f29b49..aaebb87 100644 --- a/hw/core/qdev-properties-system.c +++ b/hw/core/qdev-properties-system.c @@ -31,6 +31,20 @@ static void get_pointer(Object *obj, Visitor *v, Property *prop, visit_type_str(v, &p, name, errp); } +/* Same as get_pointer() but frees heap-allocated print() return value */ +static void get_pointer_and_free(Object *obj, Visitor *v, Property *prop, + char *(*print)(void *ptr), + const char *name, Error **errp) +{ + DeviceState *dev = DEVICE(obj); + void **ptr = qdev_get_prop_ptr(dev, prop); + char *p; + + p = *ptr ? print(*ptr) : g_strdup(""); + visit_type_str(v, &p, name, errp); + g_free(p); +} + static void set_pointer(Object *obj, Visitor *v, Property *prop, int (*parse)(DeviceState *dev, const char *str, void **ptr),