Skip to content

Commit 56b203a

Browse files
committed
config_file: keep reference to config entries when creating iterator
When creating a configuration file iterator, then we first refresh the backend and then afterwards duplicate all refreshed configuration entries into the iterator in order to avoid seeing any concurrent modifications of the entries while iterating. The duplication of entries is not guarded, though, as we do not increase the refcount of the entries that we duplicate right now. This opens us up for a race, as another thread may concurrently refresh the repository configuration and thus swap out the current set of entries. As we didn't increase the refcount, this may lead to the entries being free'd while we iterate over them in the first thread. Fix the issue by properly handling the lifecycle of the backend's entries via `config_file_entries_take` and `git_config_entries_free`, respectively.
1 parent 0927156 commit 56b203a

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/config_file.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,19 @@ static int config_file_iterator(
250250
struct git_config_backend *backend)
251251
{
252252
config_file_backend *b = GIT_CONTAINER_OF(backend, config_file_backend, parent);
253-
git_config_entries *entries = NULL;
253+
git_config_entries *dupped = NULL, *entries = NULL;
254254
int error;
255255

256256
if ((error = config_file_refresh(backend)) < 0 ||
257-
(error = git_config_entries_dup(&entries, b->entries)) < 0 ||
258-
(error = git_config_entries_iterator_new(iter, entries)) < 0)
257+
(error = config_file_entries_take(&entries, b)) < 0 ||
258+
(error = git_config_entries_dup(&dupped, entries)) < 0 ||
259+
(error = git_config_entries_iterator_new(iter, dupped)) < 0)
259260
goto out;
260261

261262
out:
262263
/* Let iterator delete duplicated entries when it's done */
263264
git_config_entries_free(entries);
265+
git_config_entries_free(dupped);
264266
return error;
265267
}
266268

0 commit comments

Comments
 (0)