Skip to content

Commit 5d987f7

Browse files
committed
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 parent de70bb4 commit 5d987f7

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/config_file.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -698,41 +698,41 @@ static int do_match_gitdir(
698698
int *matches,
699699
const git_repository *repo,
700700
const char *cfg_file,
701-
const char *value,
701+
const char *condition,
702702
bool case_insensitive)
703703
{
704-
git_buf path = GIT_BUF_INIT;
704+
git_buf pattern = GIT_BUF_INIT;
705705
int error, fnmatch_flags;
706706

707-
if (value[0] == '.' && git_path_is_dirsep(value[1])) {
708-
git_path_dirname_r(&path, cfg_file);
709-
git_buf_joinpath(&path, path.ptr, value + 2);
710-
} else if (value[0] == '~' && git_path_is_dirsep(value[1]))
711-
git_sysdir_expand_global_file(&path, value + 1);
712-
else if (!git_path_is_absolute(value))
713-
git_buf_joinpath(&path, "**", value);
707+
if (condition[0] == '.' && git_path_is_dirsep(condition[1])) {
708+
git_path_dirname_r(&pattern, cfg_file);
709+
git_buf_joinpath(&pattern, pattern.ptr, condition + 2);
710+
} else if (condition[0] == '~' && git_path_is_dirsep(condition[1]))
711+
git_sysdir_expand_global_file(&pattern, condition + 1);
712+
else if (!git_path_is_absolute(condition))
713+
git_buf_joinpath(&pattern, "**", condition);
714714
else
715-
git_buf_sets(&path, value);
715+
git_buf_sets(&pattern, condition);
716+
717+
if (git_path_is_dirsep(condition[strlen(condition) - 1]))
718+
git_buf_puts(&pattern, "**");
716719

717-
if (git_buf_oom(&path)) {
720+
if (git_buf_oom(&pattern)) {
718721
error = -1;
719722
goto out;
720723
}
721724

722-
if (git_path_is_dirsep(value[strlen(value) - 1]))
723-
git_buf_puts(&path, "**");
724-
725725
fnmatch_flags = FNM_PATHNAME|FNM_LEADING_DIR;
726726
if (case_insensitive)
727727
fnmatch_flags |= FNM_IGNORECASE;
728728

729-
if ((error = p_fnmatch(path.ptr, git_repository_path(repo), fnmatch_flags)) < 0)
729+
if ((error = p_fnmatch(pattern.ptr, git_repository_path(repo), fnmatch_flags)) < 0)
730730
goto out;
731731

732732
*matches = (error == 0);
733733

734734
out:
735-
git_buf_dispose(&path);
735+
git_buf_dispose(&pattern);
736736
return error;
737737
}
738738

0 commit comments

Comments
 (0)