+50
-10
lines changedFilter options
+50
-10
lines changed Original file line number Diff line number Diff line change
@@ -1384,6 +1384,11 @@ void add_user_command(String name, Object command, Dict(user_command) *opts, int
1384
1384
LuaRef luaref = LUA_NOREF;
1385
1385
LuaRef compl_luaref = LUA_NOREF;
1386
1386
1387
+
if (!uc_validate_name(name.data)) {
1388
+
api_set_error(err, kErrorTypeValidation, "Invalid command name");
1389
+
goto err;
1390
+
}
1391
+
1387
1392
if (mb_islower(name.data[0])) {
1388
1393
api_set_error(err, kErrorTypeValidation, "'name' must begin with an uppercase letter");
1389
1394
goto err;
Original file line number Diff line number Diff line change
@@ -5164,6 +5164,24 @@ char_u *get_command_name(expand_T *xp, int idx)
5164
5164
return cmdnames[idx].cmd_name;
5165
5165
}
5166
5166
5167
+
/// Check for a valid user command name
5168
+
///
5169
+
/// If the given {name} is valid, then a pointer to the end of the valid name is returned.
5170
+
/// Otherwise, returns NULL.
5171
+
char *uc_validate_name(char *name)
5172
+
{
5173
+
if (ASCII_ISALPHA(*name)) {
5174
+
while (ASCII_ISALNUM(*name)) {
5175
+
name++;
5176
+
}
5177
+
}
5178
+
if (!ends_excmd(*name) && !ascii_iswhite(*name)) {
5179
+
return NULL;
5180
+
}
5181
+
5182
+
return name;
5183
+
}
5184
+
5167
5185
int uc_add_command(char_u *name, size_t name_len, char_u *rep, uint32_t argt, long def, int flags,
5168
5186
int compl, char_u *compl_arg, LuaRef compl_luaref, cmd_addr_T addr_type,
5169
5187
LuaRef luaref, bool force)
@@ -5679,31 +5697,26 @@ static void ex_command(exarg_T *eap)
5679
5697
5680
5698
// Get the name (if any) and skip to the following argument.
5681
5699
name = p;
5682
-
if (ASCII_ISALPHA(*p)) {
5683
-
while (ASCII_ISALNUM(*p)) {
5684
-
p++;
5685
-
}
5686
-
}
5687
-
if (!ends_excmd(*p) && !ascii_iswhite(*p)) {
5700
+
end = (char_u *)uc_validate_name((char *)name);
5701
+
if (!end) {
5688
5702
emsg(_("E182: Invalid command name"));
5689
5703
return;
5690
5704
}
5691
-
end = p;
5692
-
name_len = (int)(end - name);
5705
+
name_len = (size_t)(end - name);
5693
5706
5694
5707
// If there is nothing after the name, and no attributes were specified,
5695
5708
// we are listing commands
5696
5709
p = skipwhite(end);
5697
5710
if (!has_attr && ends_excmd(*p)) {
5698
-
uc_list(name, end - name);
5711
+
uc_list(name, name_len);
5699
5712
} else if (!ASCII_ISUPPER(*name)) {
5700
5713
emsg(_("E183: User defined commands must start with an uppercase letter"));
5701
5714
} else if (name_len <= 4 && STRNCMP(name, "Next", name_len) == 0) {
5702
5715
emsg(_("E841: Reserved name, cannot be used for user defined command"));
5703
5716
} else if (compl > 0 && (argt & EX_EXTRA) == 0) {
5704
5717
emsg(_(e_complete_used_without_nargs));
5705
5718
} else {
5706
-
uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg, LUA_NOREF,
5719
+
uc_add_command(name, name_len, p, argt, def, flags, compl, compl_arg, LUA_NOREF,
5707
5720
addr_type_arg, LUA_NOREF, eap->forceit);
5708
5721
}
5709
5722
}
Original file line number Diff line number Diff line change
@@ -180,6 +180,28 @@ describe('nvim_add_user_command', function()
180
180
feed('<C-U>Test b<Tab>')
181
181
eq('Test bbb', funcs.getcmdline())
182
182
end)
183
+
184
+
it('does not allow invalid command names', function()
185
+
matches("'name' must begin with an uppercase letter", pcall_err(exec_lua, [[
186
+
vim.api.nvim_add_user_command('test', 'echo "hi"', {})
187
+
]]))
188
+
189
+
matches('Invalid command name', pcall_err(exec_lua, [[
190
+
vim.api.nvim_add_user_command('t@', 'echo "hi"', {})
191
+
]]))
192
+
193
+
matches('Invalid command name', pcall_err(exec_lua, [[
194
+
vim.api.nvim_add_user_command('T@st', 'echo "hi"', {})
195
+
]]))
196
+
197
+
matches('Invalid command name', pcall_err(exec_lua, [[
198
+
vim.api.nvim_add_user_command('Test!', 'echo "hi"', {})
199
+
]]))
200
+
201
+
matches('Invalid command name', pcall_err(exec_lua, [[
202
+
vim.api.nvim_add_user_command('💩', 'echo "hi"', {})
203
+
]]))
204
+
end)
183
205
end)
184
206
185
207
describe('nvim_del_user_command', function()
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4