Skip to content

Commit 0915a5b

Browse files
SyntevoAlexgitster
authored andcommitted
set_git_dir: fix crash when used with real_path()
`real_path()` returns result from a shared buffer, inviting subtle reentrance bugs. One of these bugs occur when invoked this way: set_git_dir(real_path(git_dir)) In this case, `real_path()` has reentrance: real_path read_gitfile_gently repo_set_gitdir setup_git_env set_git_dir_1 set_git_dir Later, `set_git_dir()` uses its now-dead parameter: !is_absolute_path(path) Fix this by using a dedicated `strbuf` to hold `strbuf_realpath()`. Signed-off-by: Alexandr Miloslavskiy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 076cbdc commit 0915a5b

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

builtin/init-db.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,12 @@ int init_db(const char *git_dir, const char *real_git_dir,
356356
if (!exist_ok && !stat(real_git_dir, &st))
357357
die(_("%s already exists"), real_git_dir);
358358

359-
set_git_dir(real_path(real_git_dir));
359+
set_git_dir(real_git_dir, 1);
360360
git_dir = get_git_dir();
361361
separate_git_dir(git_dir, original_git_dir);
362362
}
363363
else {
364-
set_git_dir(real_path(git_dir));
364+
set_git_dir(git_dir, 1);
365365
git_dir = get_git_dir();
366366
}
367367
startup_info->have_repository = 1;

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ const char *get_git_common_dir(void);
543543
char *get_object_directory(void);
544544
char *get_index_file(void);
545545
char *get_graft_file(struct repository *r);
546-
void set_git_dir(const char *path);
546+
void set_git_dir(const char *path, int make_realpath);
547547
int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
548548
int get_common_dir(struct strbuf *sb, const char *gitdir);
549549
const char *get_git_namespace(void);

environment.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,20 @@ static void update_relative_gitdir(const char *name,
345345
free(path);
346346
}
347347

348-
void set_git_dir(const char *path)
348+
void set_git_dir(const char *path, int make_realpath)
349349
{
350+
struct strbuf realpath = STRBUF_INIT;
351+
352+
if (make_realpath) {
353+
strbuf_realpath(&realpath, path, 1);
354+
path = realpath.buf;
355+
}
356+
350357
set_git_dir_1(path);
351358
if (!is_absolute_path(path))
352359
chdir_notify_register(NULL, update_relative_gitdir, NULL);
360+
361+
strbuf_release(&realpath);
353362
}
354363

355364
const char *get_log_output_encoding(void)

path.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ const char *enter_repo(const char *path, int strict)
850850
}
851851

852852
if (is_git_directory(".")) {
853-
set_git_dir(".");
853+
set_git_dir(".", 0);
854854
check_repository_format();
855855
return path;
856856
}

setup.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
725725
}
726726

727727
/* #18, #26 */
728-
set_git_dir(gitdirenv);
728+
set_git_dir(gitdirenv, 0);
729729
free(gitfile);
730730
return NULL;
731731
}
@@ -747,7 +747,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
747747
}
748748
else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) {
749749
/* #16d */
750-
set_git_dir(gitdirenv);
750+
set_git_dir(gitdirenv, 0);
751751
free(gitfile);
752752
return NULL;
753753
}
@@ -759,14 +759,14 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
759759

760760
/* both get_git_work_tree() and cwd are already normalized */
761761
if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */
762-
set_git_dir(gitdirenv);
762+
set_git_dir(gitdirenv, 0);
763763
free(gitfile);
764764
return NULL;
765765
}
766766

767767
offset = dir_inside_of(cwd->buf, worktree);
768768
if (offset >= 0) { /* cwd inside worktree? */
769-
set_git_dir(real_path(gitdirenv));
769+
set_git_dir(gitdirenv, 1);
770770
if (chdir(worktree))
771771
die_errno(_("cannot chdir to '%s'"), worktree);
772772
strbuf_addch(cwd, '/');
@@ -775,7 +775,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
775775
}
776776

777777
/* cwd outside worktree */
778-
set_git_dir(gitdirenv);
778+
set_git_dir(gitdirenv, 0);
779779
free(gitfile);
780780
return NULL;
781781
}
@@ -804,7 +804,7 @@ static const char *setup_discovered_git_dir(const char *gitdir,
804804

805805
/* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
806806
if (is_bare_repository_cfg > 0) {
807-
set_git_dir(offset == cwd->len ? gitdir : real_path(gitdir));
807+
set_git_dir(gitdir, (offset != cwd->len));
808808
if (chdir(cwd->buf))
809809
die_errno(_("cannot come back to cwd"));
810810
return NULL;
@@ -813,7 +813,7 @@ static const char *setup_discovered_git_dir(const char *gitdir,
813813
/* #0, #1, #5, #8, #9, #12, #13 */
814814
set_git_work_tree(".");
815815
if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
816-
set_git_dir(gitdir);
816+
set_git_dir(gitdir, 0);
817817
inside_git_dir = 0;
818818
inside_work_tree = 1;
819819
if (offset >= cwd->len)
@@ -856,10 +856,10 @@ static const char *setup_bare_git_dir(struct strbuf *cwd, int offset,
856856
die_errno(_("cannot come back to cwd"));
857857
root_len = offset_1st_component(cwd->buf);
858858
strbuf_setlen(cwd, offset > root_len ? offset : root_len);
859-
set_git_dir(cwd->buf);
859+
set_git_dir(cwd->buf, 0);
860860
}
861861
else
862-
set_git_dir(".");
862+
set_git_dir(".", 0);
863863
return NULL;
864864
}
865865

0 commit comments

Comments
 (0)