summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-06-13 19:00:06 +0200
committerPatrick Steinhardt <ps@pks.im>2019-06-15 09:34:21 +0200
commit5d987f7d0378c04be5b25f8793b2d814fc1c6e5e (patch)
tree2e313c03951795da33aa130d0382e64caa14876d
parentde70bb46ae4ea91c26f6afdc210f37b8980b7a76 (diff)
downloadlibgit2-5d987f7d0378c04be5b25f8793b2d814fc1c6e5e.tar.gz
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.
-rw-r--r--src/config_file.c32
1 files changed, 16 insertions, 16 deletions
diff --git a/src/config_file.c b/src/config_file.c
index 958a1638c..ae7a32bac 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;
}