Skip to content

Commit d5b0bac

Browse files
matheustavaresgitster
authored andcommitted
grep: fix racy calls in grep_objects()
deref_tag() calls is_promisor_object() and parse_object(), both of which perform lazy initializations and other thread-unsafe operations. If it was only called by grep_objects() this wouldn't be a problem as the latter is only executed by the main thread. However, deref_tag() is also present in read_object_file()'s call stack. So calling deref_tag() in grep_objects() without acquiring the grep_read_mutex may incur in a race condition with object reading operations (such as the ones internally performed by fill_textconv(), called at fill_textconv_grep()). The same problem happens with the call to gitmodules_config_oid() which also has parse_object() in its call stack. Fix that protecting both calls with the said grep_read_mutex. Signed-off-by: Matheus Tavares <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent faf123c commit d5b0bac

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

builtin/grep.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,13 +658,18 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
658658

659659
for (i = 0; i < nr; i++) {
660660
struct object *real_obj;
661+
662+
grep_read_lock();
661663
real_obj = deref_tag(opt->repo, list->objects[i].item,
662664
NULL, 0);
665+
grep_read_unlock();
663666

664667
/* load the gitmodules file for this rev */
665668
if (recurse_submodules) {
666669
submodule_free(opt->repo);
670+
grep_read_lock();
667671
gitmodules_config_oid(&real_obj->oid);
672+
grep_read_unlock();
668673
}
669674
if (grep_object(opt, pathspec, real_obj, list->objects[i].name,
670675
list->objects[i].path)) {

0 commit comments

Comments
 (0)