config_file: refactor `do_match_gitdir` to improve readability The function `do_match_gitdir` has some horribly named parameters and variables. Rename them to improve readability. Furthermore, fix a potentially undetected out-of-memory condition when appending "**" to the pattern.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
diff --git a/src/config_file.c b/src/config_file.c
index 958a163..ae7a32b 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -698,41 +698,41 @@ static int do_match_gitdir(
int *matches,
const git_repository *repo,
const char *cfg_file,
- const char *value,
+ const char *condition,
bool case_insensitive)
{
- git_buf path = GIT_BUF_INIT;
+ git_buf pattern = GIT_BUF_INIT;
int error, fnmatch_flags;
- if (value[0] == '.' && git_path_is_dirsep(value[1])) {
- git_path_dirname_r(&path, cfg_file);
- git_buf_joinpath(&path, path.ptr, value + 2);
- } else if (value[0] == '~' && git_path_is_dirsep(value[1]))
- git_sysdir_expand_global_file(&path, value + 1);
- else if (!git_path_is_absolute(value))
- git_buf_joinpath(&path, "**", value);
+ if (condition[0] == '.' && git_path_is_dirsep(condition[1])) {
+ git_path_dirname_r(&pattern, cfg_file);
+ git_buf_joinpath(&pattern, pattern.ptr, condition + 2);
+ } else if (condition[0] == '~' && git_path_is_dirsep(condition[1]))
+ git_sysdir_expand_global_file(&pattern, condition + 1);
+ else if (!git_path_is_absolute(condition))
+ git_buf_joinpath(&pattern, "**", condition);
else
- git_buf_sets(&path, value);
+ git_buf_sets(&pattern, condition);
+
+ if (git_path_is_dirsep(condition[strlen(condition) - 1]))
+ git_buf_puts(&pattern, "**");
- if (git_buf_oom(&path)) {
+ if (git_buf_oom(&pattern)) {
error = -1;
goto out;
}
- if (git_path_is_dirsep(value[strlen(value) - 1]))
- git_buf_puts(&path, "**");
-
fnmatch_flags = FNM_PATHNAME|FNM_LEADING_DIR;
if (case_insensitive)
fnmatch_flags |= FNM_IGNORECASE;
- if ((error = p_fnmatch(path.ptr, git_repository_path(repo), fnmatch_flags)) < 0)
+ if ((error = p_fnmatch(pattern.ptr, git_repository_path(repo), fnmatch_flags)) < 0)
goto out;
*matches = (error == 0);
out:
- git_buf_dispose(&path);
+ git_buf_dispose(&pattern);
return error;
}