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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 24, 2012 at 07:53 UTC f0c1c15c41bdcdaf71c69355ac83789466820879
2 files changed +13 -5
attr.c
+7 -5
@@ -520,11 +520,13 @@ static void bootstrap_attr_stack(void)
520 home_config_paths(NULL, &xdg_attributes_file, "attributes");
521 git_attributes_file = xdg_attributes_file;
522 }
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 + }
530 }
531
532 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
t/t1306-xdg-files.sh
+6
@@ -106,6 +106,12 @@ test_expect_success 'Checking attributes in the XDG attributes file' '
106 test_cmp expected actual
107 '
108
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 +'
115
116 test_expect_success 'Checking attributes in both XDG and local attributes files' '
117 echo "f -attr_f" >.gitattributes &&