Skip to content

Commit 776f184

Browse files
chooglengitster
authored andcommitted
config.c: NULL check when reading protected config
In read_protected_config(), check whether each file name is NULL before attempting to read it, and add a BUG() call to git_config_from_file_with_options() to make this error easier to catch in the future. The NULL checks mirror what do_git_config_sequence() does (which read_protected_config() is modeled after). Without these NULL checks, multiple tests fail with "make SANITIZE=address", e.g. in the final test of t4010, xdg_config is NULL causing us to call fopen(NULL). Reported-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Glen Choo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8d1a744 commit 776f184

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

config.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,8 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename,
19791979
int ret = -1;
19801980
FILE *f;
19811981

1982+
if (!filename)
1983+
BUG("filename cannot be NULL");
19821984
f = fopen_or_warn(filename, "r");
19831985
if (f) {
19841986
ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
@@ -2645,9 +2647,12 @@ static void read_protected_config(void)
26452647
system_config = git_system_config();
26462648
git_global_config(&user_config, &xdg_config);
26472649

2648-
git_configset_add_file(&protected_config, system_config);
2649-
git_configset_add_file(&protected_config, xdg_config);
2650-
git_configset_add_file(&protected_config, user_config);
2650+
if (system_config)
2651+
git_configset_add_file(&protected_config, system_config);
2652+
if (xdg_config)
2653+
git_configset_add_file(&protected_config, xdg_config);
2654+
if (user_config)
2655+
git_configset_add_file(&protected_config, user_config);
26512656
git_configset_add_parameters(&protected_config);
26522657

26532658
free(system_config);

0 commit comments

Comments
 (0)