From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35878) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ws9kb-0003KI-QJ for qemu-devel@nongnu.org; Wed, 04 Jun 2014 07:53:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ws9kS-0002jB-P8 for qemu-devel@nongnu.org; Wed, 04 Jun 2014 07:53:01 -0400 Received: from mail-we0-x22a.google.com ([2a00:1450:400c:c03::22a]:61497) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ws9kS-0002j2-Ij for qemu-devel@nongnu.org; Wed, 04 Jun 2014 07:52:52 -0400 Received: by mail-we0-f170.google.com with SMTP id u57so8437088wes.29 for ; Wed, 04 Jun 2014 04:52:51 -0700 (PDT) Date: Wed, 4 Jun 2014 13:52:46 +0200 From: Stefan Hajnoczi Message-ID: <20140604115246.GB11073@stefanha-thinkpad.redhat.com> References: <1401561792-13410-1-git-send-email-mreitz@redhat.com> <1401561792-13410-2-git-send-email-mreitz@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1401561792-13410-2-git-send-email-mreitz@redhat.com> Subject: Re: [Qemu-devel] [RFC 1/5] nbd: Correct name comparison for export_set_name() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Max Reitz Cc: Kevin Wolf , Paolo Bonzini , Fam Zheng , qemu-devel@nongnu.org, Stefan Hajnoczi On Sat, May 31, 2014 at 08:43:08PM +0200, Max Reitz wrote: > exp->name == name is certainly true if both strings are equal and will > work for both of them being NULL (which is important to check here); > however, the strings may also be equal without having the same address, > in which case there is no need to replace the export's name either. > Therefore, add a check for this case. > > Signed-off-by: Max Reitz > --- > nbd.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/nbd.c b/nbd.c > index e5084b6..0787cba 100644 > --- a/nbd.c > +++ b/nbd.c > @@ -832,7 +832,7 @@ NBDExport *nbd_export_find(const char *name) > > void nbd_export_set_name(NBDExport *exp, const char *name) > { > - if (exp->name == name) { > + if (exp->name == name || (exp->name && name && !strcmp(exp->name, name))) { > return; > } It's not clear to me why we even bother. The function is idempotent and there are only 2 call sites in QEMU. This is not a performance-critical function where it helps to bail early. Can we just drop the if statement completely? void nbd_export_set_name(NBDExport *exp, const char *name) { if (exp->name == name) { return; } nbd_export_get(exp); if (exp->name != NULL) { g_free(exp->name); exp->name = NULL; QTAILQ_REMOVE(&exports, exp, next); nbd_export_put(exp); } if (name != NULL) { nbd_export_get(exp); exp->name = g_strdup(name); QTAILQ_INSERT_TAIL(&exports, exp, next); } nbd_export_put(exp); }