Received: from eggs.gnu.org ([2001:4830:134:3::10]:48561) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8pme-00036V-WE for qemu-devel@nongnu.org; Fri, 03 Jun 2016 10:09:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b8pmb-0007Xz-Nh for qemu-devel@nongnu.org; Fri, 03 Jun 2016 10:09:08 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60501) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8pmb-0007Xu-Hn for qemu-devel@nongnu.org; Fri, 03 Jun 2016 10:09:05 -0400 From: Markus Armbruster References: <1463632874-28559-1-git-send-email-eblake@redhat.com> <1463632874-28559-25-git-send-email-eblake@redhat.com> <8760tq7mbv.fsf@dusky.pond.sub.org> <57517DC1.2010702@redhat.com> Date: Fri, 03 Jun 2016 16:09:03 +0200 In-Reply-To: <57517DC1.2010702@redhat.com> (Eric Blake's message of "Fri, 3 Jun 2016 06:53:21 -0600") Message-ID: <8760tquzy8.fsf@dusky.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Qemu-devel] [PATCH v4 24/28] qapi: Add JSON output visitor List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake Cc: qemu-devel@nongnu.org, Michael Roth Eric Blake writes: > On 06/03/2016 01:39 AM, Markus Armbruster wrote: >> Eric Blake writes: >> >>> We have several places that want to go from qapi to JSON; right now, >>> they have to create an intermediate QObject to do the work. That >>> also has the drawback that the JSON formatting of a QDict will >>> rearrange keys (according to a deterministic, but unpredictable, >>> hash), when humans have an easier time if dicts are produced in >>> the same order as the qapi type. >>> > >>> +struct JsonOutputVisitor { >>> + Visitor visitor; >>> + QString *str; >>> + bool comma; >>> + unsigned int depth; >>> + char **result; >>> +}; >>> + > >>> +static void json_output_complete(Visitor *v, void *result) >>> +{ >>> + JsonOutputVisitor *jov = to_jov(v); >>> + >>> + assert(!jov->depth); >>> + assert(qstring_get_length(jov->str)); >>> + assert(jov->result == result); >>> + *jov->result = qstring_consume_str(jov->str); >>> + jov->str = NULL; >>> + jov->result = NULL; >>> +} >> >> Related: discussion of complete() semantics in review of PATCH 12. >> Non-idempotent semantics save us a copy, like in >> string_output_complete(). >> >>> + >>> +static void json_output_free(Visitor *v) >>> +{ >>> + JsonOutputVisitor *jov = to_jov(v); >>> + >>> + QDECREF(jov->str); >>> + g_free(jov); >> >> I'm afraid this leaks jov->result when the visitor is destroyed without >> calling its complete() method. > > Not true. jov->result is owned by the caller, and not something we > allocate locally. We set jov->result to NULL to make sure complete() is > not called twice, but we are not responsible for freeing it, since we > didn't allocate it. > >> >> string_output_free() appears to have the same leak. > > Same non-bug. You're right.