]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: move flockfile() closer to unlocked functions
authorJeff King <peff@peff.net>
Fri, 30 Mar 2018 19:26:15 +0000 (15:26 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 30 Mar 2018 19:52:46 +0000 (12:52 -0700)
Commit 260d408e32 (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 <Johannes.Schindelin@gmx.de>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
config.c

index 41862d4a32d27806a14ca7337f4aa98f04278eaf..25686d9ff5064cff0fa9625d0f13aa9297f7ce1a 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1408,6 +1408,7 @@ static int do_config_from_file(config_fn_t fn,
                void *data)
 {
        struct config_source top;
+       int ret;
 
        top.u.file = f;
        top.origin_type = origin_type;
@@ -1418,7 +1419,10 @@ static int do_config_from_file(config_fn_t fn,
        top.do_ungetc = config_file_ungetc;
        top.do_ftell = config_file_ftell;
 
-       return do_config_from(&top, fn, data);
+       flockfile(f);
+       ret = do_config_from(&top, fn, data);
+       funlockfile(f);
+       return ret;
 }
 
 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)
 
        f = fopen_or_warn(filename, "r");
        if (f) {
-               flockfile(f);
                ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename, filename, f, data);
-               funlockfile(f);
                fclose(f);
        }
        return ret;