config: fix several access(NULL) calls

When $HOME is unset, home_config_paths fails and returns NULL pointers for user_config and xdg_config. Valgrind complains with Syscall param access(pathname) points to unaddressable byte(s). Don't call blindly access() on these variables, but test them for NULL-ness before. Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthieu Moy committed Jul 12, 2012 at 14:04 UTC e3ebc35b1695e9ac6e9b5978cd4e9ffb7b15f657
2 files changed +14 -6
builtin/config.c
+12 -4
@@ -387,12 +387,20 @@ int cmd_config(int argc, const char **argv, const char *prefix)
387
388 home_config_paths(&user_config, &xdg_config, "config");
389
390 - if (access(user_config, R_OK) && !access(xdg_config, R_OK))
390 + if (!user_config)
391 + /*
392 + * It is unknown if HOME/.gitconfig exists, so
393 + * we do not know if we should write to XDG
394 + * location; error out even if XDG_CONFIG_HOME
395 + * is set and points at a sane location.
396 + */
397 + die("$HOME not set");
398 +
399 + if (access(user_config, R_OK) &&
400 + xdg_config && !access(xdg_config, R_OK))
401 given_config_file = xdg_config;
392 - else if (user_config)
393 - given_config_file = user_config;
402 else
395 - die("$HOME not set");
403 + given_config_file = user_config;
404 }
405 else if (use_system_config)
406 given_config_file = git_etc_gitconfig();
config.c
+2 -2
@@ -940,12 +940,12 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
940 found += 1;
941 }
942
943 - if (!access(xdg_config, R_OK)) {
943 + if (xdg_config && !access(xdg_config, R_OK)) {
944 ret += git_config_from_file(fn, xdg_config, data);
945 found += 1;
946 }
947
948 - if (!access(user_config, R_OK)) {
948 + if (user_config && !access(user_config, R_OK)) {
949 ret += git_config_from_file(fn, user_config, data);
950 found += 1;
951 }