Commit 7fc97eb36dedc4ac3bc4a4a8cb5be3bb15fb53ab

Patrick Steinhardt 2020-01-09T14:21:41

index: fix resizing index map twice on case-insensitive systems Depending on whether the index map is case-sensitive or insensitive, we need to call either `git_idxmap_icase_resize` or `git_idxmap_resize`. There are multiple locations where we thus use the following pattern: if (index->ignore_case && git_idxmap_icase_resize(map, length) < 0) return -1; else if (git_idxmap_resize(map, length) < 0) return -1; The funny thing is: on case-insensitive systems, we will try to resize the map twice in case where `git_idxmap_icase_resize()` doesn't error. While this will still use the correct hashing function as both map types use the same, this bug will at least cause us to resize the map twice in a row. Fix the issue by introducing a new function `index_map_resize` that handles case-sensitivity, similar to how `index_map_set` and `index_map_delete`. Convert all call sites where we were previously resizing the map to use that new function.

diff --git a/src/index.c b/src/index.c
index c3aaa48..e2a8c7b 100644
--- a/src/index.c
+++ b/src/index.c
@@ -141,6 +141,14 @@ GIT_INLINE(int) index_map_delete(git_idxmap *map, git_index_entry *e, bool ignor
 		return git_idxmap_delete(map, e);
 }
 
+GIT_INLINE(int) index_map_resize(git_idxmap *map, size_t count, bool ignore_case)
+{
+	if (ignore_case)
+		return git_idxmap_icase_resize((git_idxmap_icase *) map, count);
+	else
+		return git_idxmap_resize(map, count);
+}
+
 int git_index_entry_srch(const void *key, const void *array_member)
 {
 	const struct entry_srch_key *srch_key = key;
@@ -1620,7 +1628,8 @@ int git_index__fill(git_index *index, const git_vector *source_entries)
 		return 0;
 
 	if (git_vector_size_hint(&index->entries, source_entries->length) < 0 ||
-	    git_idxmap_resize(index->entries_map, (size_t)(source_entries->length * 1.3)) < 0)
+	    index_map_resize(index->entries_map, (size_t)(source_entries->length * 1.3),
+			     index->ignore_case) < 0)
 		return -1;
 
 	git_vector_foreach(source_entries, i, source_entry) {
@@ -2608,11 +2617,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
 
 	assert(!index->entries.length);
 
-	if (index->ignore_case &&
-	    (error = git_idxmap_icase_resize((git_idxmap_icase *) index->entries_map,
-					     header.entry_count)) < 0)
-		return error;
-	else if ((error = git_idxmap_resize(index->entries_map, header.entry_count)) < 0)
+	if ((error = index_map_resize(index->entries_map, header.entry_count, index->ignore_case)) < 0)
 		return error;
 
 	/* Parse all the entries */
@@ -3125,11 +3130,7 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
 	if ((error = git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, &data)) < 0)
 		goto cleanup;
 
-	if (index->ignore_case &&
-	    (error = git_idxmap_icase_resize((git_idxmap_icase *) entries_map,
-					     entries.length)) < 0)
-		goto cleanup;
-	else if ((error = git_idxmap_resize(entries_map, entries.length)) < 0)
+	if ((error = index_map_resize(entries_map, entries.length, index->ignore_case)) < 0)
 		goto cleanup;
 
 	git_vector_foreach(&entries, i, e) {
@@ -3185,12 +3186,8 @@ static int git_index_read_iterator(
 	    (error = git_idxmap_new(&new_entries_map)) < 0)
 		goto done;
 
-	if (index->ignore_case && new_length_hint &&
-	    (error = git_idxmap_icase_resize((git_idxmap_icase *) new_entries_map,
-					     new_length_hint)) < 0)
-		goto done;
-	else if (new_length_hint &&
-	         (error = git_idxmap_resize(new_entries_map, new_length_hint)) < 0)
+	if (new_length_hint && (error = index_map_resize(new_entries_map, new_length_hint,
+							 index->ignore_case)) < 0)
 		goto done;
 
 	opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |