Skip to content

Commit a560d87

Browse files
pcloudsgitster
authored andcommitted
environment.c: fix potential segfault by get_git_common_dir()
setup_git_env() must be called before this function to initialize git_common_dir so that it returns a non NULL string. And it must return a non NULL string or segfault can happen because all callers expect so. It does not do so explicitly though and depends on get_git_dir() being called first (which will guarantee setup_git_env()). Avoid this dependency and call setup_git_env() by itself. test-ref-store.c will hit this problem because it's very lightweight, just enough initialization to exercise refs code, and get_git_dir() will never be called until get_worktrees() is, which uses get_git_common_dir and hits a segfault. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent adac811 commit a560d87

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

environment.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ const char *get_git_dir(void)
214214

215215
const char *get_git_common_dir(void)
216216
{
217+
if (!git_dir)
218+
setup_git_env();
217219
return git_common_dir;
218220
}
219221

0 commit comments

Comments
 (0)