git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/6] fast-export: simplify initialization of anonymized hashmaps

From
Jeff King <peff@peff.net>
Date
Mar 22, 2023, 17:38 UTC
Message-ID
<20230322173804.GB5795@coredump.intra.peff.net>
In-Reply-To
<20230322173636.GA5643@coredump.intra.peff.net>

We take pains to avoid doing a lookup on a hashmap which has not been initialized with hashmap_init(). That was necessary back when this code was written. But hashmap_get() became safer in b7879b0ba6e (hashmap: allow re-use after hashmap_free(), 2020-11-02). Since then it's OK to call functions on a zero-initialized table; it will just correctly return NULL, since there is no match.

This simplifies the code a little, and also lets us keep the initialization line closer to when we add an entry (which is when the hashmap really does need to be totally initialized). That will help later refactoring.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/fast-export.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
diff --git a/builtin/fast-export.c b/builtin/fast-export.c
index f422819c82a..ba9ab3a97e5 100644
--- a/builtin/fast-export.c
+++ b/builtin/fast-export.c
@@ -152,25 +152,22 @@ static const char *anonymize_str(struct hashmap *map,
 	struct anonymized_entry_key key;
 	struct anonymized_entry *ret;
 
-	if (!map->cmpfn)
-		hashmap_init(map, anonymized_entry_cmp, NULL, 0);
-
 	hashmap_entry_init(&key.hash, memhash(orig, len));
 	key.orig = orig;
 	key.orig_len = len;
 
 	/* First check if it's a token the user configured manually... */
-	if (anonymized_seeds.cmpfn)
-		ret = hashmap_get_entry(&anonymized_seeds, &key, hash, &key);
-	else
-		ret = NULL;
+	ret = hashmap_get_entry(&anonymized_seeds, &key, hash, &key);
 
 	/* ...otherwise check if we've already seen it in this context... */
 	if (!ret)
 		ret = hashmap_get_entry(map, &key, hash, &key);
 
 	/* ...and finally generate a new mapping if necessary */
 	if (!ret) {
+		if (!map->cmpfn)
+			hashmap_init(map, anonymized_entry_cmp, NULL, 0);
+
 		FLEX_ALLOC_MEM(ret, orig, orig, len);
 		hashmap_entry_init(&ret->hash, key.hash.hash);
 		ret->anon = generate(data);
-- 
2.40.0.595.g9b96b494d8c
Previous: Jeff KingNext: Jeff King
Message 3 of 9 in “minor cleanups to fast-export --anonymize”
  1. 0/6 minor cleanups to fast-export --anonymizeJeff King, Mar 22, 2023
  2. 1/6 fast-export: drop const when storing anonymized valuesJeff King, Mar 22, 2023
  3. 2/6 fast-export: simplify initialization of anonymized hashmapsJeff King, Mar 22, 2023
  4. 3/6 fast-export: factor out anonymized_entry creationJeff King, Mar 22, 2023
  5. 4/6 fast-export: de-obfuscate --anonymize-map handlingJeff King, Mar 22, 2023
  6. 5/6 fast-export: drop data parameter from anonymous generatorsJeff King, Mar 22, 2023
  7. 6/6 fast-export: drop unused parameter from anonymize_commit_message()Jeff King, Mar 22, 2023
  8. Derrick StoleeMar 24, 2023
  9. Junio C HamanoMar 24, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.