Skip to content

Commit 7fc97eb

Browse files
committed
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.
1 parent ab45887 commit 7fc97eb

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/index.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ GIT_INLINE(int) index_map_delete(git_idxmap *map, git_index_entry *e, bool ignor
141141
return git_idxmap_delete(map, e);
142142
}
143143

144+
GIT_INLINE(int) index_map_resize(git_idxmap *map, size_t count, bool ignore_case)
145+
{
146+
if (ignore_case)
147+
return git_idxmap_icase_resize((git_idxmap_icase *) map, count);
148+
else
149+
return git_idxmap_resize(map, count);
150+
}
151+
144152
int git_index_entry_srch(const void *key, const void *array_member)
145153
{
146154
const struct entry_srch_key *srch_key = key;
@@ -1620,7 +1628,8 @@ int git_index__fill(git_index *index, const git_vector *source_entries)
16201628
return 0;
16211629

16221630
if (git_vector_size_hint(&index->entries, source_entries->length) < 0 ||
1623-
git_idxmap_resize(index->entries_map, (size_t)(source_entries->length * 1.3)) < 0)
1631+
index_map_resize(index->entries_map, (size_t)(source_entries->length * 1.3),
1632+
index->ignore_case) < 0)
16241633
return -1;
16251634

16261635
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)
26082617

26092618
assert(!index->entries.length);
26102619

2611-
if (index->ignore_case &&
2612-
(error = git_idxmap_icase_resize((git_idxmap_icase *) index->entries_map,
2613-
header.entry_count)) < 0)
2614-
return error;
2615-
else if ((error = git_idxmap_resize(index->entries_map, header.entry_count)) < 0)
2620+
if ((error = index_map_resize(index->entries_map, header.entry_count, index->ignore_case)) < 0)
26162621
return error;
26172622

26182623
/* Parse all the entries */
@@ -3125,11 +3130,7 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
31253130
if ((error = git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, &data)) < 0)
31263131
goto cleanup;
31273132

3128-
if (index->ignore_case &&
3129-
(error = git_idxmap_icase_resize((git_idxmap_icase *) entries_map,
3130-
entries.length)) < 0)
3131-
goto cleanup;
3132-
else if ((error = git_idxmap_resize(entries_map, entries.length)) < 0)
3133+
if ((error = index_map_resize(entries_map, entries.length, index->ignore_case)) < 0)
31333134
goto cleanup;
31343135

31353136
git_vector_foreach(&entries, i, e) {
@@ -3185,12 +3186,8 @@ static int git_index_read_iterator(
31853186
(error = git_idxmap_new(&new_entries_map)) < 0)
31863187
goto done;
31873188

3188-
if (index->ignore_case && new_length_hint &&
3189-
(error = git_idxmap_icase_resize((git_idxmap_icase *) new_entries_map,
3190-
new_length_hint)) < 0)
3191-
goto done;
3192-
else if (new_length_hint &&
3193-
(error = git_idxmap_resize(new_entries_map, new_length_hint)) < 0)
3189+
if (new_length_hint && (error = index_map_resize(new_entries_map, new_length_hint,
3190+
index->ignore_case)) < 0)
31943191
goto done;
31953192

31963193
opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |

0 commit comments

Comments
 (0)