[PATCH v4 1/5] environment.c: fix potential segfault by get_git_common_dir()
- From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
- Date
- Apr 4, 2017, 10:21 UTC
- Message-ID
- <20170404102123.25315-2-pclouds@gmail.com>
- In-Reply-To
- <20170404102123.25315-1-pclouds@gmail.com>
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 <pclouds@gmail.com> --- environment.c | 2 ++ 1 file changed, 2 insertions(+)
diff --git a/environment.c b/environment.c index 42dc3106d2..2986ee7200 100644 --- a/environment.c +++ b/environment.c @@ -214,6 +214,8 @@ const char *get_git_dir(void) const char *get_git_common_dir(void) { + if (!git_dir) + setup_git_env(); return git_common_dir; }
-- 2.11.0.157.gd943d85