Skip to contentSkip to conversation

config --get --path: check for unset $HOME

PATCH In review Jonathan Nieder 3 participants 5 messages Jun 15, 2016 5 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): 20100723012322.GA27113@burratino

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): 20100726005111.GA29755@burratino

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw

PATCH 1/1 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.

---

Quoted 7 lines · Julien Cristau

* expecting success:

        git config --get --path path.home > result &&
        git config --get --path path.normal >> result &&
        git config --get --path path.trailingtilde >> result &&
        test_cmp expect result

Segmentation fault

Good catch.

 path.c                 |    2 ++
 t/t1300-repo-config.sh |   24 +++++++++++++++++++++++-
 2 files changed, 25 insertions(+), 1 deletions(-)
path.c +2 -0
index b4c8d91..6b23023 100644
@@ -316,6 +316,8 @@ char *expand_user_path(const char *path)
 		size_t username_len = first_slash - username;
 		if (username_len == 0) {
 			const char *home = getenv("HOME");
+			if (!home)
+				goto return_null;
 			strbuf_add(&user_path, home, strlen(home));
 		} else {
 			struct passwd *pw = getpw_str(username, username_len);
t/t1300-repo-config.sh +23 -1
index f11f98c..7ddab5f 100755
@@ -707,19 +707,41 @@ test_expect_success 'set --path' '
 	git config --path path.trailingtilde "foo~" &&
 	test_cmp expect .git/config'
 
+if test "${HOME+set}"
+then
+	test_set_prereq HOMEVAR
+fi
+
 cat >expect <<EOF
 $HOME/
 /dev/null
 foo~
 EOF
 
-test_expect_success 'get --path' '
+test_expect_success HOMEVAR 'get --path' '
 	git config --get --path path.home > result &&
 	git config --get --path path.normal >> result &&
 	git config --get --path path.trailingtilde >> result &&
 	test_cmp expect result
 '
 
+cat >expect <<\EOF
+/dev/null
+foo~
+EOF
+
+test_expect_success 'get --path copes with unset $HOME' '
+	(
+		unset HOME;
+		test_must_fail git config --get --path path.home \
+			>result 2>msg &&
+		git config --get --path path.normal >>result &&
+		git config --get --path path.trailingtilde >>result
+	) &&
+	grep "[Ff]ailed to expand.*~/" msg &&
+	test_cmp expect result
+'
+
 rm .git/config
 
 git config quote.leading " test"
Signature · 1 line
1.7.1
#7 lore raw
In reply to Jonathan Nieder
Quoted 8 lines · Jonathan Nieder

               size_t username_len = first_slash - username;
               if (username_len == 0) {
                       const char *home = getenv("HOME");
+                       if (!home)
+                               goto return_null; strbuf_add(&user_path, home, strlen(home)); } else {
                       struct passwd *pw = getpw_str(username, username_len);

Should we maybe fall back on checking pw_dir in getpwuid() if $HOME isn't set?

#8 lore raw
Quoted 1 line · Ævar Arnfjörð Bjarmason

Should we maybe fall back on checking pw_dir in getpwuid() if $HOME isn't set?

Maybe. Why? On Unix I suspect it is such a rare case as to not justify complicating the rules, but I do not know if it would help on other platforms.

Jonathan

#9 lore raw
Quoted 7 lines · Jonathan Nieder
Quoted 1 line · Ęvar Arnfjörš Bjarmason

Should we maybe fall back on checking pw_dir in getpwuid() if $HOME isn't set?

Maybe.  Why?  On Unix I suspect it is such a rare case as to not justify complicating the rules, but I do not know if it would help on other platforms.

I don't know if this is worth it, but in the Perl world this is a fairly common idiom:

    my $home = $ENV{HOME} || (getpwuid($<))[7];

Sometimes you're executing under some buggy wrapper that's scrubbing your environment. I recall running into this issue e.g. on some odd Solaris webserver, which was executing CGI scripts under my user, but $HOME wasn't set.