Skip to content

Commit f0c1c15

Browse files
peffgitster
authored andcommitted
attr: make sure we have an xdg path before using it
If we don't have a core.attributesfile configured, we fall back to checking XDG config, which is usually $HOME/.config/git/attributes. However, if $HOME is unset, then home_config_paths will return NULL, and we end up calling fopen(NULL). Depending on your system, this may or may not cause the accompanying test to fail (e.g., on Linux and glibc, the address will go straight to open, which will return EFAULT). However, valgrind will reliably notice the error. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5adf84e commit f0c1c15

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

attr.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -520,11 +520,13 @@ static void bootstrap_attr_stack(void)
520520
home_config_paths(NULL, &xdg_attributes_file, "attributes");
521521
git_attributes_file = xdg_attributes_file;
522522
}
523-
elem = read_attr_from_file(git_attributes_file, 1);
524-
if (elem) {
525-
elem->origin = NULL;
526-
elem->prev = attr_stack;
527-
attr_stack = elem;
523+
if (git_attributes_file) {
524+
elem = read_attr_from_file(git_attributes_file, 1);
525+
if (elem) {
526+
elem->origin = NULL;
527+
elem->prev = attr_stack;
528+
attr_stack = elem;
529+
}
528530
}
529531

530532
if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {

t/t1306-xdg-files.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ test_expect_success 'Checking attributes in the XDG attributes file' '
106106
test_cmp expected actual
107107
'
108108

109+
test_expect_success 'Checking XDG attributes when HOME is unset' '
110+
>expected &&
111+
(sane_unset HOME &&
112+
git check-attr -a f >actual) &&
113+
test_cmp expected actual
114+
'
109115

110116
test_expect_success 'Checking attributes in both XDG and local attributes files' '
111117
echo "f -attr_f" >.gitattributes &&

0 commit comments

Comments
 (0)