From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59241) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8obR-0005fe-1W for qemu-devel@nongnu.org; Fri, 03 Jun 2016 08:53:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b8obL-0004y1-S1 for qemu-devel@nongnu.org; Fri, 03 Jun 2016 08:53:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:59238) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8obL-0004xx-JO for qemu-devel@nongnu.org; Fri, 03 Jun 2016 08:53:23 -0400 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> From: Eric Blake Message-ID: <57517DC1.2010702@redhat.com> Date: Fri, 3 Jun 2016 06:53:21 -0600 MIME-Version: 1.0 In-Reply-To: <8760tq7mbv.fsf@dusky.pond.sub.org> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="L7HbGakelk2HNgDQ9nRTAK2NmGbM1ARX8" 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: Markus Armbruster Cc: qemu-devel@nongnu.org, Michael Roth This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --L7HbGakelk2HNgDQ9nRTAK2NmGbM1ARX8 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 06/03/2016 01:39 AM, Markus Armbruster wrote: > Eric Blake writes: >=20 >> 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 =3D to_jov(v); >> + >> + assert(!jov->depth); >> + assert(qstring_get_length(jov->str)); >> + assert(jov->result =3D=3D result); >> + *jov->result =3D qstring_consume_str(jov->str); >> + jov->str =3D NULL; >> + jov->result =3D NULL; >> +} >=20 > Related: discussion of complete() semantics in review of PATCH 12. > Non-idempotent semantics save us a copy, like in > string_output_complete(). >=20 >> + >> +static void json_output_free(Visitor *v) >> +{ >> + JsonOutputVisitor *jov =3D to_jov(v); >> + >> + QDECREF(jov->str); >> + g_free(jov); >=20 > 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. >=20 > string_output_free() appears to have the same leak. Same non-bug. >=20 >> +} >> + >> +Visitor *json_output_visitor_new(char **result) >> +{ >> + JsonOutputVisitor *v; >> + >> + v =3D g_malloc0(sizeof(*v)); >> + v->result =3D result; >> + *result =3D NULL; >> + v->str =3D qstring_new(); >> + --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --L7HbGakelk2HNgDQ9nRTAK2NmGbM1ARX8 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEbBAEBCAAGBQJXUX3BAAoJEKeha0olJ0Nqfq0H93HRJqfK1H68V8fvHQ8H0VGm zHbXlvAGY7ey/TVa65Pc4rcdEmovkl15j6r6eYFBlRasSJviUixEh9KN8e3hdBOx aMCGBNtXvg/KGlatgO2S1yl9KbP6XxvkRPmCdHgPgA8xVEpWCTCz6L52NQrWjPae XkYoiq8OyVgDFhdvf3yH9QGHYPKhx+w3vE4Pffvy+IM1gxIuL6FcN9mSliqd3W0B YTYh+6mUkgVFZljNsVdp9CETjuYCgV1Bx4Uf3Zgexqd3g+/4Hizm1ZRBvr1svEQ8 d4kJ/pG4x9XZQKIIDytg4t23U8WR3W8UejnrmNWBcDcTpALlUXlUHsV90cHRXg== =AvZb -----END PGP SIGNATURE----- --L7HbGakelk2HNgDQ9nRTAK2NmGbM1ARX8--