Received: from eggs.gnu.org ([2001:4830:134:3::10]:50543) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z59O5-0005wC-5i for qemu-devel@nongnu.org; Wed, 17 Jun 2015 05:12:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z59O1-0000VK-2S for qemu-devel@nongnu.org; Wed, 17 Jun 2015 05:12:01 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40556) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z59O0-0000VG-Tz for qemu-devel@nongnu.org; Wed, 17 Jun 2015 05:11:57 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id C58012CD838 for ; Wed, 17 Jun 2015 09:11:55 +0000 (UTC) Date: Wed, 17 Jun 2015 11:11:53 +0200 From: Kevin Wolf Message-ID: <20150617091153.GC4202@noname.str.redhat.com> References: <1434458200-23440-1-git-send-email-mst@redhat.com> <1434458200-23440-2-git-send-email-mst@redhat.com> <55803AD0.9050708@redhat.com> <20150617085351-mutt-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20150617085351-mutt-send-email-mst@redhat.com> Subject: Re: [Qemu-devel] [PATCH RFC 1/3] error: don't rely on pointer comparisons List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Michael S. Tsirkin" Cc: qemu-devel@nongnu.org, dgilbert@redhat.com, armbru@redhat.com Am 17.06.2015 um 08:57 hat Michael S. Tsirkin geschrieben: > On Tue, Jun 16, 2015 at 09:03:44AM -0600, Eric Blake wrote: > > On 06/16/2015 06:53 AM, Michael S. Tsirkin wrote: > > > makes it possible to copy error_abort pointers, > > > not just pass them on directly. > > > > > > Signed-off-by: Michael S. Tsirkin > > > --- > > > util/error.c | 16 +++++++++++----- > > > 1 file changed, 11 insertions(+), 5 deletions(-) > > > > > > diff --git a/util/error.c b/util/error.c > > > index 14f4351..ccf29ea 100644 > > > --- a/util/error.c > > > +++ b/util/error.c > > > @@ -20,7 +20,13 @@ struct Error > > > ErrorClass err_class; > > > }; > > > > > > -Error *error_abort; > > > +static Error error_abort_st = { .err_class = ERROR_CLASS_MAX }; > > > +Error *error_abort = &error_abort_st; > > > > Looking at this a bit further, I still wonder if we can do a slightly > > better job of coming up with something that will SIGSEGV (or SIGBUS) if > > we (accidentally) try to dereference the pointer (similar to how SIG_IGN > > is (sighandler_t)1) - because we know that the abort object should never > > be dereferenced. Something like: > > > > Error *error_abort = (Error *)1; > > > > with no need for error_abort_st. (Might have to spell it as Error > > *error_abort = (void*)(intptr_t)1; > > to shut up compiler warnings) > > > > > + > > > +static bool error_is_abort(Error **errp) > > > +{ > > > + return errp && *errp && (*errp)->err_class == ERROR_CLASS_MAX; > > > +} > > > > and this would be: > > > > return errp && *errp == error_abort; > > > > The rest of this patch is still good. Then in patch 2, you'd have: > > > > Error *error_init_local(Error **errp) > > { > > return error_is_abort(errp) ? error_abort : NULL; > > } > > > > That is, you still use pointer equality, but at one less level of > > indirection (equality at the Error* level, not the Error** level). > > It's a clever trick, it'd work. But why do tricks? This is never > performance-critical, is it? E.g. debugging is easier if pointers > actually point to things. > > Let's see what do others say. This isn't about performance, but about failing for invalid use. If we say that dereferencing error_abort is wrong, then letting it fail fast (with a segfault) actually makes debugging easier than silently accessing garbage and trying to figure out why some code misbehaves only later. Kevin