Skip to content

Commit 4fad66f

Browse files
committed
vim-patch:8.0.0056
Problem: When setting 'filetype' there is no check for a valid name. Solution: Only allow valid characters in 'filetype', 'syntax' and 'keymap'. vim/vim@d0b5138
1 parent 42033bc commit 4fad66f

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

Diff for: src/nvim/option.c

+27-3
Original file line numberDiff line numberDiff line change
@@ -2399,6 +2399,18 @@ static char *set_string_option(const int opt_idx, const char *const value,
23992399
return r;
24002400
}
24012401

2402+
/// Return true if "val" is a valid 'filetype' name.
2403+
/// Also used for 'syntax' and 'keymap'.
2404+
static bool valid_filetype(char_u *val)
2405+
{
2406+
for (char_u *s = val; *s != NUL; s++) {
2407+
if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)".-_", *s) == NULL) {
2408+
return false;
2409+
}
2410+
}
2411+
return true;
2412+
}
2413+
24022414
/*
24032415
* Handle string options that need some action to perform when changed.
24042416
* Returns NULL for success, or an error message for an error.
@@ -2623,8 +2635,12 @@ did_set_string_option (
26232635
xfree(p_penc);
26242636
p_penc = p;
26252637
} else if (varp == &curbuf->b_p_keymap) {
2626-
/* load or unload key mapping tables */
2627-
errmsg = keymap_init();
2638+
if (!valid_filetype(*varp)) {
2639+
errmsg = e_invarg;
2640+
} else {
2641+
// load or unload key mapping tables
2642+
errmsg = keymap_init();
2643+
}
26282644

26292645
if (errmsg == NULL) {
26302646
if (*curbuf->b_p_keymap != NUL) {
@@ -3118,8 +3134,16 @@ did_set_string_option (
31183134
if (check_opt_strings(p_icm, p_icm_values, false) != OK) {
31193135
errmsg = e_invarg;
31203136
}
3121-
// Options that are a list of flags.
3137+
} else if (gvarp == &p_ft) {
3138+
if (!valid_filetype(*varp)) {
3139+
errmsg = e_invarg;
3140+
}
3141+
} else if (gvarp == &p_syn) {
3142+
if (!valid_filetype(*varp)) {
3143+
errmsg = e_invarg;
3144+
}
31223145
} else {
3146+
// Options that are a list of flags.
31233147
p = NULL;
31243148
if (varp == &p_ww)
31253149
p = (char_u *)WW_ALL;

Diff for: src/nvim/testdir/test_options.vim

+50
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,53 @@ function! Test_path_keep_commas()
3838

3939
set path&
4040
endfunction
41+
42+
func Test_filetype_valid()
43+
set ft=valid_name
44+
call assert_equal("valid_name", &filetype)
45+
set ft=valid-name
46+
call assert_equal("valid-name", &filetype)
47+
48+
call assert_fails(":set ft=wrong;name", "E474:")
49+
call assert_fails(":set ft=wrong\\\\name", "E474:")
50+
call assert_fails(":set ft=wrong\\|name", "E474:")
51+
call assert_fails(":set ft=wrong/name", "E474:")
52+
call assert_fails(":set ft=wrong\\\nname", "E474:")
53+
call assert_equal("valid-name", &filetype)
54+
55+
exe "set ft=trunc\x00name"
56+
call assert_equal("trunc", &filetype)
57+
endfunc
58+
59+
func Test_syntax_valid()
60+
set syn=valid_name
61+
call assert_equal("valid_name", &syntax)
62+
set syn=valid-name
63+
call assert_equal("valid-name", &syntax)
64+
65+
call assert_fails(":set syn=wrong;name", "E474:")
66+
call assert_fails(":set syn=wrong\\\\name", "E474:")
67+
call assert_fails(":set syn=wrong\\|name", "E474:")
68+
call assert_fails(":set syn=wrong/name", "E474:")
69+
call assert_fails(":set syn=wrong\\\nname", "E474:")
70+
call assert_equal("valid-name", &syntax)
71+
72+
exe "set syn=trunc\x00name"
73+
call assert_equal("trunc", &syntax)
74+
endfunc
75+
76+
func Test_keymap_valid()
77+
call assert_fails(":set kmp=valid_name", "E544:")
78+
call assert_fails(":set kmp=valid_name", "valid_name")
79+
call assert_fails(":set kmp=valid-name", "E544:")
80+
call assert_fails(":set kmp=valid-name", "valid-name")
81+
82+
call assert_fails(":set kmp=wrong;name", "E474:")
83+
call assert_fails(":set kmp=wrong\\\\name", "E474:")
84+
call assert_fails(":set kmp=wrong\\|name", "E474:")
85+
call assert_fails(":set kmp=wrong/name", "E474:")
86+
call assert_fails(":set kmp=wrong\\\nname", "E474:")
87+
88+
call assert_fails(":set kmp=trunc\x00name", "E544:")
89+
call assert_fails(":set kmp=trunc\x00name", "trunc")
90+
endfunc

0 commit comments

Comments
 (0)