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

[PATCH] oidmap: ensure map is initialized

From
BWBrandon Williams <bmwill@google.com>
Date
Dec 22, 2017, 23:27 UTC
Message-ID
<20171222232729.253936-1-bmwill@google.com>
In-Reply-To
<xmqqlghur08f.fsf@gitster.mtv.corp.google.com>

Ensure that an oidmap is initialized before attempting to add, remove, or retrieve an entry by simply performing the initialization step before accessing the underlying hashmap.

Signed-off-by: Brandon Williams <bmwill@google.com>
---
 oidmap.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
diff --git a/oidmap.c b/oidmap.c
index 6db4fffcd..d9fb19ba6 100644
--- a/oidmap.c
+++ b/oidmap.c
@@ -33,12 +33,19 @@ void oidmap_free(struct oidmap *map, int free_entries)
 
 void *oidmap_get(const struct oidmap *map, const struct object_id *key)
 {
+	if (!map->map.cmpfn)
+		return NULL;
+
 	return hashmap_get_from_hash(&map->map, hash(key), key);
 }
 
 void *oidmap_remove(struct oidmap *map, const struct object_id *key)
 {
 	struct hashmap_entry entry;
+
+	if (!map->map.cmpfn)
+		oidmap_init(map, 0);
+
 	hashmap_entry_init(&entry, hash(key));
 	return hashmap_remove(&map->map, &entry, key);
 }
@@ -46,6 +53,10 @@ void *oidmap_remove(struct oidmap *map, const struct object_id *key)
 void *oidmap_put(struct oidmap *map, void *entry)
 {
 	struct oidmap_entry *to_put = entry;
+
+	if (!map->map.cmpfn)
+		oidmap_init(map, 0);
+
 	hashmap_entry_init(&to_put->internal_entry, hash(&to_put->oid));
 	return hashmap_put(&map->map, to_put);
 }
-- 
2.15.1.620.gb9897f4670-goog
Previous: Junio C HamanoNext: Johannes Schindelin
Message 4 of 8 in “oidmap.h: strongly discourage using OIDMAP_INIT directly”
  1. oidmap.h: strongly discourage using OIDMAP_INIT directlyJohannes Schindelin, Dec 22, 2017
  2. Brandon WilliamsDec 22, 2017
  3. Junio C HamanoDec 22, 2017
  4. oidmap: ensure map is initializedBrandon Williams, Dec 22, 2017
  5. Johannes SchindelinDec 23, 2017
  6. Junio C HamanoDec 27, 2017
  7. Johannes SchindelinDec 23, 2017
  8. Jeff HostetlerJan 2, 2018

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.