Skip to content

Commit e3ebc35

Browse files
moygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0e8593d commit e3ebc35

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

builtin/config.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,20 @@ int cmd_config(int argc, const char **argv, const char *prefix)
387387

388388
home_config_paths(&user_config, &xdg_config, "config");
389389

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))
391401
given_config_file = xdg_config;
392-
else if (user_config)
393-
given_config_file = user_config;
394402
else
395-
die("$HOME not set");
403+
given_config_file = user_config;
396404
}
397405
else if (use_system_config)
398406
given_config_file = git_etc_gitconfig();

config.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,12 +940,12 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
940940
found += 1;
941941
}
942942

943-
if (!access(xdg_config, R_OK)) {
943+
if (xdg_config && !access(xdg_config, R_OK)) {
944944
ret += git_config_from_file(fn, xdg_config, data);
945945
found += 1;
946946
}
947947

948-
if (!access(user_config, R_OK)) {
948+
if (user_config && !access(user_config, R_OK)) {
949949
ret += git_config_from_file(fn, user_config, data);
950950
found += 1;
951951
}

0 commit comments

Comments
 (0)