Skip to content

Commit 05bab3e

Browse files
Ramsay Jonesgitster
authored andcommitted
config.c: Fix a static buffer overwrite bug by avoiding mkpath()
On cygwin, test number 21 of t3200-branch.sh (git branch -m q q2 without config should succeed) fails. The failure involves the functions from path.c which parcel out internal static buffers from the git_path() and mkpath() functions. In particular, the rename_ref() function calls safe_create_leading\ _directories() with a filename returned by git_path("logs/%s", ref). safe_create_leading_directories(), in turn, calls stat() on each element of the path it is given. On cygwin, this leads to a call to git_config() for each component of the path, since this test explicitly removes the config file. git_config() calls mkpath(), so on the fourth component of the path, the original buffer passed into the function is overwritten with the config filename. Note that this bug is specific to cygwin and it's schizophrenic stat() functions (see commits adbc0b6, 7faee6b and 7974843). The lack of a config file and a path with at least four elements is also important to trigger the bug. In order to fix the problem, we replace the call to mkpath() with a call to mksnpath() and provide our own buffer. Signed-off-by: Ramsay Jones <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3a81f33 commit 05bab3e

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

config.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -865,12 +865,12 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
865865

866866
home = getenv("HOME");
867867
if (home) {
868-
char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
868+
char buf[PATH_MAX];
869+
char *user_config = mksnpath(buf, sizeof(buf), "%s/.gitconfig", home);
869870
if (!access(user_config, R_OK)) {
870871
ret += git_config_from_file(fn, user_config, data);
871872
found += 1;
872873
}
873-
free(user_config);
874874
}
875875

876876
if (repo_config && !access(repo_config, R_OK)) {

0 commit comments

Comments
 (0)