From patchwork Tue Aug 4 09:18:04 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 503431 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 D52421402C8 for ; Tue, 4 Aug 2015 19:21:36 +1000 (AEST) Received: from localhost ([::1]:34175 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZMYPe-00017A-Vs for incoming@patchwork.ozlabs.org; Tue, 04 Aug 2015 05:21:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59561) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZMYMf-00042C-Ds for qemu-devel@nongnu.org; Tue, 04 Aug 2015 05:18:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZMYMb-0007as-PO for qemu-devel@nongnu.org; Tue, 04 Aug 2015 05:18:29 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58558) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZMYMb-0007Y7-Am for qemu-devel@nongnu.org; Tue, 04 Aug 2015 05:18:25 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (Postfix) with ESMTPS id 6F762A10CF; Tue, 4 Aug 2015 09:18:21 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-31.ams2.redhat.com [10.36.116.31]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t749IJtG002724 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Tue, 4 Aug 2015 05:18:20 -0400 Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 21E5F3011FA3; Tue, 4 Aug 2015 11:18:17 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Tue, 4 Aug 2015 11:18:04 +0200 Message-Id: <1438679896-5077-15-git-send-email-armbru@redhat.com> In-Reply-To: <1438679896-5077-1-git-send-email-armbru@redhat.com> References: <1438679896-5077-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: mdroth@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH 14/26] qapi: Document that input visitor semantics are prone to leaks 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 From: Eric Blake Most functions that can return a pointer or set an Error ** value are decent enough to guarantee a NULL return when reporting an error. Not so with our generated qapi visitor functions. If the caller is not careful to clean up partially-allocated objects on error, then the caller suffers a memory leak. Properly fixing it is probably complex enough to save for a later day, so merely document it for now. Signed-off-by: Eric Blake Message-Id: <1438295587-19069-1-git-send-email-eblake@redhat.com> Signed-off-by: Markus Armbruster --- scripts/qapi-visit.py | 4 ++++ tests/test-qmp-input-visitor.c | 2 ++ 2 files changed, 6 insertions(+) diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index 73f136f..eec5f1f 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -115,6 +115,10 @@ out: def generate_visit_struct_body(name): + # FIXME: if *obj is NULL on entry, and visit_start_struct() assigns to + # *obj, but then visit_type_FOO_fields() fails, we should clean up *obj + # rather than leaving it non-NULL. As currently written, the caller must + # call qapi_free_FOO() to avoid a memory leak of the partial FOO. ret = mcgen(''' Error *err = NULL; diff --git a/tests/test-qmp-input-visitor.c b/tests/test-qmp-input-visitor.c index b7a87ee..a5cfefa 100644 --- a/tests/test-qmp-input-visitor.c +++ b/tests/test-qmp-input-visitor.c @@ -636,6 +636,8 @@ static void test_visitor_in_errors(TestInputVisitorData *data, visit_type_TestStruct(v, &p, NULL, &err); g_assert(err); + /* FIXME - a failed parse should not leave a partially-allocated p + * for us to clean up; this could cause callers to leak memory. */ g_assert(p->string == NULL); error_free(err);