Skip to content

Commit 8da9394

Browse files
committed
idxmap: have resize functions return proper error code
The currently existing function `git_idxmap_resize` and `git_idxmap_icase_resize` do not return any error codes at all due to their previous implementation making use of a macro. Due to that, it is impossible to see whether the resize operation might have failed due to an out-of-memory situation. Fix this by providing a proper error code. Adjust callers to make use of it.
1 parent 661fc57 commit 8da9394

File tree

3 files changed

+60
-27
lines changed

3 files changed

+60
-27
lines changed

src/idxmap.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ void git_idxmap_icase_clear(git_idxmap_icase *map)
6868
kh_clear(idxicase, map);
6969
}
7070

71+
int git_idxmap_resize(git_idxmap *map, size_t size)
72+
{
73+
if (kh_resize(idx, map, size) < 0) {
74+
git_error_set_oom();
75+
return -1;
76+
}
77+
return 0;
78+
}
79+
80+
int git_idxmap_icase_resize(git_idxmap_icase *map, size_t size)
81+
{
82+
if (kh_resize(idxicase, map, size) < 0) {
83+
git_error_set_oom();
84+
return -1;
85+
}
86+
return 0;
87+
}
88+
7189
void *git_idxmap_get(git_idxmap *map, const git_index_entry *key)
7290
{
7391
size_t idx = git_idxmap_lookup_index(map, key);
@@ -177,16 +195,6 @@ int git_idxmap_icase_has_data(git_idxmap_icase *map, size_t idx)
177195
return kh_exist(map, idx);
178196
}
179197

180-
void git_idxmap_resize(git_idxmap *map, size_t size)
181-
{
182-
kh_resize(idx, map, size);
183-
}
184-
185-
void git_idxmap_icase_resize(git_idxmap_icase *map, size_t size)
186-
{
187-
kh_resize(idxicase, map, size);
188-
}
189-
190198
void git_idxmap_delete_at(git_idxmap *map, size_t idx)
191199
{
192200
kh_del(idx, map, idx);

src/idxmap.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ void git_idxmap_clear(git_idxmap *map);
7676
*/
7777
void git_idxmap_icase_clear(git_idxmap_icase *map);
7878

79+
/**
80+
* Resize the map by allocating more memory.
81+
*
82+
* @param map map that shall be resized
83+
* @param size count of entries that the map shall hold
84+
* @return `0` if the map was successfully resized, a negative
85+
* error code otherwise
86+
*/
87+
int git_idxmap_resize(git_idxmap *map, size_t size);
88+
89+
/**
90+
* Resize the map by allocating more memory.
91+
*
92+
* @param map map that shall be resized
93+
* @param size count of entries that the map shall hold
94+
* @return `0` if the map was successfully resized, a negative
95+
* error code otherwise
96+
*/
97+
int git_idxmap_icase_resize(git_idxmap_icase *map, size_t size);
98+
7999
/**
80100
* Return value associated with the given key.
81101
*
@@ -137,9 +157,6 @@ int git_idxmap_icase_valid_index(git_idxmap_icase *map, size_t idx);
137157
int git_idxmap_has_data(git_idxmap *map, size_t idx);
138158
int git_idxmap_icase_has_data(git_idxmap_icase *map, size_t idx);
139159

140-
void git_idxmap_resize(git_idxmap *map, size_t size);
141-
void git_idxmap_icase_resize(git_idxmap_icase *map, size_t size);
142-
143160
void git_idxmap_delete_at(git_idxmap *map, size_t idx);
144161
void git_idxmap_icase_delete_at(git_idxmap_icase *map, size_t idx);
145162

src/index.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,8 +1619,9 @@ int git_index__fill(git_index *index, const git_vector *source_entries)
16191619
if (!source_entries->length)
16201620
return 0;
16211621

1622-
git_vector_size_hint(&index->entries, source_entries->length);
1623-
git_idxmap_resize(index->entries_map, (size_t)(source_entries->length * 1.3));
1622+
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)
1624+
return -1;
16241625

16251626
git_vector_foreach(source_entries, i, source_entry) {
16261627
git_index_entry *entry = NULL;
@@ -2608,10 +2609,12 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
26082609

26092610
assert(!index->entries.length);
26102611

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

26162619
/* Parse all the entries */
26172620
for (i = 0; i < header.entry_count && buffer_size > INDEX_FOOTER_SIZE; ++i) {
@@ -3125,10 +3128,12 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
31253128
if ((error = git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, &data)) < 0)
31263129
goto cleanup;
31273130

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

31333138
git_vector_foreach(&entries, i, e) {
31343139
INSERT_IN_MAP_EX(index, entries_map, e, error);
@@ -3185,10 +3190,13 @@ static int git_index_read_iterator(
31853190
(error = git_idxmap_new(&new_entries_map)) < 0)
31863191
goto done;
31873192

3188-
if (index->ignore_case && new_length_hint)
3189-
git_idxmap_icase_resize((git_idxmap_icase *) new_entries_map, new_length_hint);
3190-
else if (new_length_hint)
3191-
git_idxmap_resize(new_entries_map, new_length_hint);
3193+
if (index->ignore_case && new_length_hint &&
3194+
(error = git_idxmap_icase_resize((git_idxmap_icase *) new_entries_map,
3195+
new_length_hint)) < 0)
3196+
goto done;
3197+
else if (new_length_hint &&
3198+
(error = git_idxmap_resize(new_entries_map, new_length_hint)) < 0)
3199+
goto done;
31923200

31933201
opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
31943202
GIT_ITERATOR_INCLUDE_CONFLICTS;

0 commit comments

Comments
 (0)