Skip to content

Commit 256a94e

Browse files
dschogitster
authored andcommitted
var: avoid a segmentation fault when HOME is unset
The code introduced in 576a37f (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 <[email protected]> Acked-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ed773a1 commit 256a94e

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

builtin/var.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static char *git_attr_val_system(int ident_flag UNUSED)
6464

6565
static char *git_attr_val_global(int ident_flag UNUSED)
6666
{
67-
char *file = xstrdup(git_attr_global_file());
67+
char *file = xstrdup_or_null(git_attr_global_file());
6868
if (file) {
6969
normalize_path_copy(file, file);
7070
return file;

t/t0007-git-var.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,13 @@ test_expect_success 'listing and asking for variables are exclusive' '
268268
test_must_fail git var -l GIT_COMMITTER_IDENT
269269
'
270270

271+
test_expect_success '`git var -l` works even without HOME' '
272+
(
273+
XDG_CONFIG_HOME= &&
274+
export XDG_CONFIG_HOME &&
275+
unset HOME &&
276+
git var -l
277+
)
278+
'
279+
271280
test_done

0 commit comments

Comments
 (0)