From patchwork Wed Oct 26 21:18:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 9398347 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D28E8600BA for ; Wed, 26 Oct 2016 21:22:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9DC6F29E10 for ; Wed, 26 Oct 2016 21:22:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9234729E13; Wed, 26 Oct 2016 21:22:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 0EE0D29E10 for ; Wed, 26 Oct 2016 21:22:08 +0000 (UTC) Received: from localhost ([::1]:37455 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bzVeC-0005sA-03 for patchwork-qemu-devel@patchwork.kernel.org; Wed, 26 Oct 2016 17:22:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49873) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bzVb6-0003xX-FZ for qemu-devel@nongnu.org; Wed, 26 Oct 2016 17:18:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bzVb3-0006qU-8O for qemu-devel@nongnu.org; Wed, 26 Oct 2016 17:18:56 -0400 Received: from relay4-d.mail.gandi.net ([217.70.183.196]:50916) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1bzVb3-0006q0-1G for qemu-devel@nongnu.org; Wed, 26 Oct 2016 17:18:53 -0400 Received: from red.redhat.com (unknown [IPv6:2602:30a:c7d0:7100:7657:7b6:d81b:9143]) (Authenticated sender: eric@blake.one) by relay4-d.mail.gandi.net (Postfix) with ESMTPA id DF0A41720A9; Wed, 26 Oct 2016 23:18:50 +0200 (CEST) From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 26 Oct 2016 16:18:38 -0500 Message-Id: <1477516718-15439-3-git-send-email-eblake@redhat.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1477516358-15039-1-git-send-email-eblake@redhat.com> References: <1477516358-15039-1-git-send-email-eblake@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 217.70.183.196 Subject: [Qemu-devel] [PATCH v2 3/3] tests: Enhance qobject output to cover partial visit X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , armbru@redhat.com, ptoscano@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Add a test that proves (at least when run under valgrind) that we are correctly handling allocated memory even when a visit is aborted in the middle for whatever other reason. See commit f24582d "qapi: fix double free in qmp_output_visitor_cleanup()" for a fix that was lacking testsuite exposure prior to this patch. Signed-off-by: Eric Blake --- v2: rebase (and hence retitle), add list & alternate coverage --- tests/test-qobject-output-visitor.c | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/test-qobject-output-visitor.c b/tests/test-qobject-output-visitor.c index c2e0f43..fdae0d5 100644 --- a/tests/test-qobject-output-visitor.c +++ b/tests/test-qobject-output-visitor.c @@ -254,6 +254,43 @@ static void test_visitor_out_struct_errors(TestOutputVisitorData *data, } +static void test_visitor_out_partial_visit(TestOutputVisitorData *data, + const void *unused) +{ + /* Various checks that a mid-visit abort doesn't leak or double-free. */ + const char *str = "hi"; + Error *err = NULL; + UserDefAlternate uda = { .type = QTYPE_QDICT, + .u.udfu = { .integer = 1, + .string = (char *) "bye", + .enum1 = -1 } }; + UserDefAlternate *obj = &uda; + + /* Abort within a nested object with no data members */ + visit_start_struct(data->ov, NULL, NULL, 0, &error_abort); + visit_start_struct(data->ov, "nested", NULL, 0, &error_abort); + visitor_reset(data); + + /* Abort in the middle of a list of strings */ + visit_start_list(data->ov, "list", NULL, 0, &error_abort); + visit_type_str(data->ov, NULL, (char **)&str, &error_abort); + visit_type_str(data->ov, NULL, (char **)&str, &error_abort); + visitor_reset(data); + + /* Abort in the middle of an alternate. Alternates can't be + * virtually visited, so we get to inline the first half of + * visit_type_UserDefAlternate(). */ + visit_start_alternate(data->ov, NULL, (GenericAlternate **)&obj, + sizeof(uda), false, &error_abort); + visit_start_struct(data->ov, NULL, NULL, 0, &error_abort); + visit_type_UserDefUnionBase_members(data->ov, + (UserDefUnionBase *)&uda.u.udfu, + &err); + error_free_or_abort(&err); + visitor_reset(data); +} + + static void test_visitor_out_list(TestOutputVisitorData *data, const void *unused) { @@ -817,6 +854,8 @@ int main(int argc, char **argv) &out_visitor_data, test_visitor_out_struct_nested); output_visitor_test_add("/visitor/output/struct-errors", &out_visitor_data, test_visitor_out_struct_errors); + output_visitor_test_add("/visitor/output/partial-visit", + &out_visitor_data, test_visitor_out_partial_visit); output_visitor_test_add("/visitor/output/list", &out_visitor_data, test_visitor_out_list); output_visitor_test_add("/visitor/output/any",