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 <avarab@gmail.com> Signed-off-by: Glen Choo <chooglen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Glen Choo committed Jul 26, 2022 at 22:21 UTC 776f184893d2861a729aa4b91d69931036e03e4b
1 file changed +8 -3
config.c
+8 -3
@@ -1979,6 +1979,8 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename,
1979 int ret = -1;
1980 FILE *f;
1981
1982 + if (!filename)
1983 + BUG("filename cannot be NULL");
1984 f = fopen_or_warn(filename, "r");
1985 if (f) {
1986 ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
@@ -2645,9 +2647,12 @@ static void read_protected_config(void)
2647 system_config = git_system_config();
2648 git_global_config(&user_config, &xdg_config);
2649
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);
2656 git_configset_add_parameters(&protected_config);
2657
2658 free(system_config);