Skip to content

Commit 05e293c

Browse files
peffgitster
authored andcommitted
config: move flockfile() closer to unlocked functions
Commit 260d408 (config: use getc_unlocked when reading from file, 2015-04-16) taught git_config_from_file() to lock the filehandle so that we could safely use the faster unlocked functions to access the handle. However, it split the logic into two places: 1. The master lock/unlock happens in git_config_from_file(). 2. The decision to use the unlocked functions happens in do_config_from_file(). That means that if anybody calls the latter function, they will accidentally use the unlocked functions without holding the lock. And indeed, git_config_from_stdin() does so. In practice, this hasn't been a problem since this code isn't generally multi-threaded (and even if some Git program happened to have another thread running, it's unlikely to be reading from stdin). But it's a good practice to make sure we're always holding the lock before using the unlocked functions. Helped-by: Johannes Schindelin <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3013dff commit 05e293c

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

config.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,7 @@ static int do_config_from_file(config_fn_t fn,
14081408
void *data)
14091409
{
14101410
struct config_source top;
1411+
int ret;
14111412

14121413
top.u.file = f;
14131414
top.origin_type = origin_type;
@@ -1418,7 +1419,10 @@ static int do_config_from_file(config_fn_t fn,
14181419
top.do_ungetc = config_file_ungetc;
14191420
top.do_ftell = config_file_ftell;
14201421

1421-
return do_config_from(&top, fn, data);
1422+
flockfile(f);
1423+
ret = do_config_from(&top, fn, data);
1424+
funlockfile(f);
1425+
return ret;
14221426
}
14231427

14241428
static int git_config_from_stdin(config_fn_t fn, void *data)
@@ -1433,9 +1437,7 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
14331437

14341438
f = fopen_or_warn(filename, "r");
14351439
if (f) {
1436-
flockfile(f);
14371440
ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename, filename, f, data);
1438-
funlockfile(f);
14391441
fclose(f);
14401442
}
14411443
return ret;

0 commit comments

Comments
 (0)