Received: from eggs.gnu.org ([2001:4830:134:3::10]:47756) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZgUW1-0006PY-Sa for qemu-devel@nongnu.org; Mon, 28 Sep 2015 05:14:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZgUVw-000488-Na for qemu-devel@nongnu.org; Mon, 28 Sep 2015 05:14:33 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38673) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZgUVw-00047u-Fy for qemu-devel@nongnu.org; Mon, 28 Sep 2015 05:14:28 -0400 From: Markus Armbruster References: <1442872682-6523-1-git-send-email-eblake@redhat.com> <1442872682-6523-8-git-send-email-eblake@redhat.com> <87r3lneuet.fsf@blackfin.pond.sub.org> <5604216D.2010509@redhat.com> <56070895.1@redhat.com> Date: Mon, 28 Sep 2015 11:14:25 +0200 In-Reply-To: <56070895.1@redhat.com> (Eric Blake's message of "Sat, 26 Sep 2015 15:05:25 -0600") Message-ID: <8737xylxce.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Qemu-devel] [PATCH v5 07/46] qapi: Don't pass pre-existing error to later call List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake Cc: marcandre.lureau@redhat.com, DirtY.iCE.hu@gmail.com, Michael Roth , ehabkost@redhat.com, qemu-devel@nongnu.org Eric Blake writes: > On 09/24/2015 10:14 AM, Eric Blake wrote: > >>>> >>>> visit_start_implicit_struct(m, (void **)obj, sizeof(FOO), &err); >>>> if (!err) { >>>> visit_type_FOO_fields(m, obj, &err); >>>> visit_end_implicit_struct(m, err ? NULL : &err); >>>> } >>>> error_propagate(errp, err); >>> >>> Hmmmmm... not sure we do this anywhere else, yet. The ternary isn't >>> exactly pretty, but the intent to ignore additional error is clear >>> enough, I think. >>> >>> If we elect to adopt this new error handling pattern, we should perhaps >>> document it in error.h. >>> >>> Third option: drop visit_end_implicit_struct()'s errp parameter. If we >>> find a compelling use for it, we'll put it back and solve the problem. >>> >> >> Ooh, interesting idea. It changes the contract - but since the contract >> isn't (yet) documented, and happens to work with existing uses without a >> contract, it could indeed be nicer. It would have knock-on effects to >> 24/46 where I first try documenting the contract. > > Oh well. We do have a compelling use: qmp-input-visitor.c can set errp > during visit_end_struct() when in strict mode (basically, the mode which > warns if the input QDict has leftover key:value pairs that were not > consumed by visit_type_FOO() between the start/end call). I don't know > if visit_end_list() or visit_end_implicit_struct() care; but then we > have the argument that it is worth keeping them consistent with > visit_end_struct() which can indeed raise an error. > > One other potential alternative: What if we split visit_end_struct() > into two visitor functions, one that checks for success, and the other > that is called unconditionally to clean up resources. That is, go from: > > visit_start_struct(m, (void **)obj, "foo", name, sizeof(FOO), &err); > if (!err) { > if (*obj) { > visit_type_FOO_fields(m, obj, errp); > } > visit_end_struct(m, &err); > } > error_propagate(errp, err); > > to a form where the check for leftover key/value pairs is only done on > success with a new visit_check_struct(): > > visit_start_struct(m, (void **)obj, "foo", name, sizeof(FOO), &err); > if (!err) { > if (*obj) { > visit_type_FOO_fields(m, obj, &err); > } > if (!err) { > visit_check_struct(m, &err); > } > visit_end_struct(m); > } > error_propagate(errp, err); I think this split could help with writing safe code: in visit_check_struct() you can rely on "no error so far", as usual. In visit_end_struct(), you can't, but it should be a pure cleanup function, where that's quite normal. Looks like we're getting drawn into visitor contract territory again.