Skip to content

Commit faf123c

Browse files
matheustavaresgitster
authored andcommitted
grep: fix race conditions at grep_submodule()
There're currently two function calls in builtin/grep.c:grep_submodule() which might result in race conditions: - submodule_from_path(): it has config_with_options() in its call stack which, in turn, may have read_object_file() in its own. Therefore, calling the first function without acquiring grep_read_mutex may end up causing a race condition with other object read operations performed by worker threads (for example, at the fill_textconv() call in grep.c:fill_textconv_grep()). - parse_object_or_die(): it falls into the same problem, having repo_has_object_file(the_repository, ...) in its call stack. Besides that, parse_object(), which is also called by parse_object_or_die(), is thread-unsafe and also called by object reading functions. It's unlikely to really fall into a data race with these operations as the volume of calls to them is usually very low. But we better protect ourselves against this possibility, anyway. So, to solve these issues, move both of these function calls into the critical section of grep_read_mutex. Signed-off-by: Matheus Tavares <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c3a5bb3 commit faf123c

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

builtin/grep.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,7 @@ static int grep_submodule(struct grep_opt *opt,
407407
{
408408
struct repository subrepo;
409409
struct repository *superproject = opt->repo;
410-
const struct submodule *sub = submodule_from_path(superproject,
411-
&null_oid, path);
410+
const struct submodule *sub;
412411
struct grep_opt subopt;
413412
int hit;
414413

@@ -419,6 +418,7 @@ static int grep_submodule(struct grep_opt *opt,
419418
* object.
420419
*/
421420
grep_read_lock();
421+
sub = submodule_from_path(superproject, &null_oid, path);
422422

423423
if (!is_submodule_active(superproject, path)) {
424424
grep_read_unlock();
@@ -455,9 +455,8 @@ static int grep_submodule(struct grep_opt *opt,
455455
unsigned long size;
456456
struct strbuf base = STRBUF_INIT;
457457

458-
object = parse_object_or_die(oid, oid_to_hex(oid));
459-
460458
grep_read_lock();
459+
object = parse_object_or_die(oid, oid_to_hex(oid));
461460
data = read_object_with_reference(&subrepo,
462461
&object->oid, tree_type,
463462
&size, NULL);

0 commit comments

Comments
 (0)