git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] config --get --path: check for unset $HOME

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Jul 26, 2010, 15:06 UTC
Message-ID
<20100726150651.GA4021@burratino>
In-Reply-To
<20100726140756.GH12476@radis.liafa.jussieu.fr>

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 <jcristau@debian.org>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
Julien Cristau wrote:
Show 7 quoted lines
> * 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(-)
diff --git a/path.c b/path.c
index b4c8d91..6b23023 100644
--- a/path.c
+++ b/path.c
@@ -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);
diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh
index f11f98c..7ddab5f 100755
--- a/t/t1300-repo-config.sh
+++ b/t/t1300-repo-config.sh
@@ -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"
-- 
1.7.1
Next: Julien Cristau
Message 1 of 5 in “config --get --path: check for unset $HOME”
  1. config --get --path: check for unset $HOMEJonathan Nieder, Jul 26, 2010
  2. Julien CristauJul 26, 2010
  3. Ævar Arnfjörð BjarmasonJul 26, 2010
  4. Jonathan NiederJul 26, 2010
  5. Ævar Arnfjörð BjarmasonJul 26, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.