]> git.ipfire.org Git - thirdparty/git.git/commitdiff
var: avoid a segmentation fault when `HOME` is unset
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Mon, 4 Sep 2023 06:21:26 +0000 (06:21 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 5 Sep 2023 22:28:26 +0000 (15:28 -0700)
The code introduced in 576a37fccbf (var: add attributes files locations,
2023-06-27) paid careful attention to use `xstrdup()` for pointers known
never to be `NULL`, and `xstrdup_or_null()` otherwise.

One spot was missed, though: `git_attr_global_file()` can return `NULL`,
when the `HOME` variable is not set (and neither `XDG_CONFIG_HOME`), a
scenario not too uncommon in certain server scenarios.

Fix this, and add a test case to avoid future regressions.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Acked-by: brian m. carlson <bk2204@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/var.c
t/t0007-git-var.sh

index ef45710a2053419cc49d3f2f42aea3e1c6996afa..05ba69e47b366b7d4777c9d58ac86d5e4fa6b5b8 100644 (file)
@@ -64,7 +64,7 @@ static char *git_attr_val_system(int ident_flag UNUSED)
 
 static char *git_attr_val_global(int ident_flag UNUSED)
 {
-       char *file = xstrdup(git_attr_global_file());
+       char *file = xstrdup_or_null(git_attr_global_file());
        if (file) {
                normalize_path_copy(file, file);
                return file;
index 8cb597f99c4cead2388681416d3cc0eddd90fa0f..ff4fd9348cca6470f4dbacbc4c9a3d775f15c06d 100755 (executable)
@@ -268,4 +268,13 @@ test_expect_success 'listing and asking for variables are exclusive' '
        test_must_fail git var -l GIT_COMMITTER_IDENT
 '
 
+test_expect_success '`git var -l` works even without HOME' '
+       (
+               XDG_CONFIG_HOME= &&
+               export XDG_CONFIG_HOME &&
+               unset HOME &&
+               git var -l
+       )
+'
+
 test_done