Skip to content

Commit e44b018

Browse files
pks-tgitster
authored andcommitted
builtin/config: check for writeability after source is set up
The `check_write()` function verifies that we do not try to write to a config source that cannot be written to, like for example stdin. But while the new subcommands do call this function, they do so before calling `handle_config_location()`. Consequently, we only end up checking the default config location for writeability, not the location that was actually specified by the caller of git-config(1). Fix this by calling `check_write()` after `handle_config_location()`. We will further clarify the relationship between those two functions in a subsequent commit where we remove the global state that both implicitly rely on. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9cab5e8 commit e44b018

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

builtin/config.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,6 @@ static int cmd_config_set(int argc, const char **argv, const char *prefix)
843843

844844
argc = parse_options(argc, argv, prefix, opts, builtin_config_set_usage,
845845
PARSE_OPT_STOP_AT_NON_OPTION);
846-
check_write();
847846
check_argc(argc, 2, 2);
848847

849848
if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern)
@@ -856,6 +855,7 @@ static int cmd_config_set(int argc, const char **argv, const char *prefix)
856855
comment = git_config_prepare_comment_string(comment_arg);
857856

858857
handle_config_location(prefix);
858+
check_write();
859859

860860
value = normalize_value(argv[0], argv[1], &default_kvi);
861861

@@ -891,13 +891,13 @@ static int cmd_config_unset(int argc, const char **argv, const char *prefix)
891891

892892
argc = parse_options(argc, argv, prefix, opts, builtin_config_unset_usage,
893893
PARSE_OPT_STOP_AT_NON_OPTION);
894-
check_write();
895894
check_argc(argc, 1, 1);
896895

897896
if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern)
898897
die(_("--fixed-value only applies with 'value-pattern'"));
899898

900899
handle_config_location(prefix);
900+
check_write();
901901

902902
if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern)
903903
return git_config_set_multivar_in_file_gently(given_config_source.file,
@@ -918,10 +918,10 @@ static int cmd_config_rename_section(int argc, const char **argv, const char *pr
918918

919919
argc = parse_options(argc, argv, prefix, opts, builtin_config_rename_section_usage,
920920
PARSE_OPT_STOP_AT_NON_OPTION);
921-
check_write();
922921
check_argc(argc, 2, 2);
923922

924923
handle_config_location(prefix);
924+
check_write();
925925

926926
ret = git_config_rename_section_in_file(given_config_source.file,
927927
argv[0], argv[1]);
@@ -943,10 +943,10 @@ static int cmd_config_remove_section(int argc, const char **argv, const char *pr
943943

944944
argc = parse_options(argc, argv, prefix, opts, builtin_config_remove_section_usage,
945945
PARSE_OPT_STOP_AT_NON_OPTION);
946-
check_write();
947946
check_argc(argc, 1, 1);
948947

949948
handle_config_location(prefix);
949+
check_write();
950950

951951
ret = git_config_rename_section_in_file(given_config_source.file,
952952
argv[0], NULL);
@@ -997,10 +997,10 @@ static int cmd_config_edit(int argc, const char **argv, const char *prefix)
997997
};
998998

999999
argc = parse_options(argc, argv, prefix, opts, builtin_config_edit_usage, 0);
1000-
check_write();
10011000
check_argc(argc, 0, 0);
10021001

10031002
handle_config_location(prefix);
1003+
check_write();
10041004

10051005
return show_editor();
10061006
}

t/t1300-config.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,6 +2835,12 @@ test_expect_success 'specifying multiple modes causes failure' '
28352835
test_cmp expect err
28362836
'
28372837

2838+
test_expect_success 'writing to stdin is rejected' '
2839+
echo "fatal: writing to stdin is not supported" >expect &&
2840+
test_must_fail git config ${mode_set} --file - foo.bar baz 2>err &&
2841+
test_cmp expect err
2842+
'
2843+
28382844
done
28392845

28402846
test_done

0 commit comments

Comments
 (0)