Skip to content

Commit 79bf149

Browse files
jrngitster
authored andcommitted
config --get --path: check for unset $HOME
If $HOME is unset (as in some automated build situations), currently git config --path path.home "~" git config --path --get path.home segfaults. Error out with Failed to expand user dir in: '~/' instead. Reported-by: Julien Cristau <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 28bf4ba commit 79bf149

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

path.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ char *expand_user_path(const char *path)
316316
size_t username_len = first_slash - username;
317317
if (username_len == 0) {
318318
const char *home = getenv("HOME");
319+
if (!home)
320+
goto return_null;
319321
strbuf_add(&user_path, home, strlen(home));
320322
} else {
321323
struct passwd *pw = getpw_str(username, username_len);

t/t1300-repo-config.sh

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,19 +707,41 @@ test_expect_success 'set --path' '
707707
git config --path path.trailingtilde "foo~" &&
708708
test_cmp expect .git/config'
709709

710+
if test "${HOME+set}"
711+
then
712+
test_set_prereq HOMEVAR
713+
fi
714+
710715
cat >expect <<EOF
711716
$HOME/
712717
/dev/null
713718
foo~
714719
EOF
715720

716-
test_expect_success 'get --path' '
721+
test_expect_success HOMEVAR 'get --path' '
717722
git config --get --path path.home > result &&
718723
git config --get --path path.normal >> result &&
719724
git config --get --path path.trailingtilde >> result &&
720725
test_cmp expect result
721726
'
722727

728+
cat >expect <<\EOF
729+
/dev/null
730+
foo~
731+
EOF
732+
733+
test_expect_success 'get --path copes with unset $HOME' '
734+
(
735+
unset HOME;
736+
test_must_fail git config --get --path path.home \
737+
>result 2>msg &&
738+
git config --get --path path.normal >>result &&
739+
git config --get --path path.trailingtilde >>result
740+
) &&
741+
grep "[Ff]ailed to expand.*~/" msg &&
742+
test_cmp expect result
743+
'
744+
723745
rm .git/config
724746

725747
git config quote.leading " test"

0 commit comments

Comments
 (0)