All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: "Carlo Marcelo Arenas Belón" <carenas@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH] remote: avoid -Wunused-but-set-variable in gcc with -DNDEBUG
Date: Thu, 2 Sep 2021 04:52:53 -0400	[thread overview]
Message-ID: <YTCQ5aJXPMTUpKs8@coredump.intra.peff.net> (raw)
In-Reply-To: <YTCO56kbtQbODDeK@coredump.intra.peff.net>

[dropped Patrick from cc; that address isn't valid anymore, and he's not
active in Git development these days]

On Thu, Sep 02, 2021 at 04:44:23AM -0400, Jeff King wrote:

> > this breaks the build with -DNDEBUG because the assert gets compiled
> > out and therefore the variable used to check is never used
> 
> Right, this is the real point of the patch. Compiling with NDEBUG will
> result in a warning.

Taking all of my suggestions together yields something like:

-- >8 --
From: Carlo Marcelo Arenas Belón <carenas@gmail.com>
Subject: [PATCH] remote: avoid -Wunused-but-set-variable in gcc with -DNDEBUG

In make_remote(), we store the return value of hashmap_put() and check
it using assert(), but don't otherwise use it. If Git is compiled with
NDEBUG, then the assert() becomes a noop, and nobody looks at the
variable at all. This causes some compilers to produce warnings.

Let's switch it instead to a BUG(). This accomplishes the same thing,
but is always compiled in (and we don't have to worry about the cost;
the check is cheap, and this is not a hot code path).

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
---
 remote.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/remote.c b/remote.c
index dfb863d808..40e785da38 100644
--- a/remote.c
+++ b/remote.c
@@ -135,7 +135,7 @@ static inline void init_remotes_hash(void)
 
 static struct remote *make_remote(const char *name, int len)
 {
-	struct remote *ret, *replaced;
+	struct remote *ret;
 	struct remotes_hash_key lookup;
 	struct hashmap_entry lookup_entry, *e;
 
@@ -162,8 +162,8 @@ static struct remote *make_remote(const char *name, int len)
 	remotes[remotes_nr++] = ret;
 
 	hashmap_entry_init(&ret->ent, lookup_entry.hash);
-	replaced = hashmap_put_entry(&remotes_hash, ret, ent);
-	assert(replaced == NULL);  /* no previous entry overwritten */
+	if (hashmap_put_entry(&remotes_hash, ret, ent))
+		BUG("hashmap_put overwrote entry after hashmap_get returned NULL");
 	return ret;
 }
 
-- 
2.33.0.446.g793367f49a


  reply	other threads:[~2021-09-02  8:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-02  7:36 [PATCH] remote: avoid -Wunused-but-set-variable in gcc with -DNDEBUG Carlo Marcelo Arenas Belón
2021-09-02  8:44 ` Jeff King
2021-09-02  8:52   ` Jeff King [this message]
2021-09-02  9:10     ` Carlo Arenas
2021-09-02 20:13       ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YTCQ5aJXPMTUpKs8@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=carenas@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.