Skip to content

Commit

Permalink
var: avoid a segmentation fault when HOME is unset
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dscho committed Sep 3, 2023
1 parent 43c8a30 commit 6ad63c8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion builtin/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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;
Expand Down
9 changes: 9 additions & 0 deletions t/t0007-git-var.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6ad63c8

Please sign in to comment.