fix(api): avoid assertion when autocmd group id is 0 (#23210)

This commit is contained in:
Dhruv Manilawala 2023-04-23 06:53:25 +05:30 committed by GitHub
parent 77ff25b1d9
commit 3ac952d4e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View File

@ -114,7 +114,7 @@ Array nvim_get_autocmds(Dict(get_autocmds) *opts, Error *err)
break;
case kObjectTypeInteger:
group = (int)opts->group.data.integer;
char *name = augroup_name(group);
char *name = group == 0 ? NULL : augroup_name(group);
VALIDATE_INT(augroup_exists(name), "group", opts->group.data.integer, {
goto cleanup;
});
@ -684,7 +684,7 @@ void nvim_del_augroup_by_id(Integer id, Error *err)
FUNC_API_SINCE(9)
{
TRY_WRAP(err, {
char *name = augroup_name((int)id);
char *name = id == 0 ? NULL : augroup_name((int)id);
augroup_del(name, false);
});
}
@ -746,7 +746,7 @@ void nvim_exec_autocmds(Object event, Dict(exec_autocmds) *opts, Error *err)
break;
case kObjectTypeInteger:
au_group = (int)opts->group.data.integer;
char *name = augroup_name(au_group);
char *name = au_group == 0 ? NULL : augroup_name(au_group);
VALIDATE_INT(augroup_exists(name), "group", (int64_t)au_group, {
goto cleanup;
});
@ -840,7 +840,7 @@ static int get_augroup_from_object(Object group, Error *err)
return au_group;
case kObjectTypeInteger:
au_group = (int)group.data.integer;
char *name = augroup_name(au_group);
char *name = au_group == 0 ? NULL : augroup_name(au_group);
VALIDATE_INT(augroup_exists(name), "group", (int64_t)au_group, {
return AUGROUP_ERROR;
});

View File

@ -39,6 +39,10 @@ describe('autocmd api', function()
}))
eq("Invalid 'event' item: expected String, got Array", pcall_err(meths.create_autocmd,
{'FileType', {}}, {}))
eq("Invalid 'group': 0", pcall_err(meths.create_autocmd, 'FileType', {
group = 0,
command = 'ls',
}))
end)
it('doesnt leak when you use ++once', function()
@ -308,6 +312,9 @@ describe('autocmd api', function()
eq("Invalid 'group': 'bogus'", pcall_err(meths.get_autocmds, {
group = 'bogus',
}))
eq("Invalid 'group': 0", pcall_err(meths.get_autocmds, {
group = 0,
}))
eq("Invalid 'group': expected String or Integer, got Array", pcall_err(meths.get_autocmds, {
group = {},
}))
@ -725,6 +732,9 @@ describe('autocmd api', function()
eq("Invalid 'group': expected String or Integer, got Array", pcall_err(meths.exec_autocmds, 'FileType', {
group = {},
}))
eq("Invalid 'group': 0", pcall_err(meths.exec_autocmds, 'FileType', {
group = 0,
}))
eq("Invalid 'buffer': expected Integer, got Array", pcall_err(meths.exec_autocmds, 'FileType', {
buffer = {},
}))
@ -1049,6 +1059,12 @@ describe('autocmd api', function()
eq(false, exec_lua[[return pcall(vim.api.nvim_del_augroup_by_id, -12342)]])
eq('Vim:E367: No such group: "--Deleted--"', pcall_err(meths.del_augroup_by_id, -12312))
eq(false, exec_lua[[return pcall(vim.api.nvim_del_augroup_by_id, 0)]])
eq('Vim:E367: No such group: "[NULL]"', pcall_err(meths.del_augroup_by_id, 0))
eq(false, exec_lua[[return pcall(vim.api.nvim_del_augroup_by_id, 12342)]])
eq('Vim:E367: No such group: "[NULL]"', pcall_err(meths.del_augroup_by_id, 12312))
end)
it('groups work with once', function()
@ -1224,6 +1240,7 @@ describe('autocmd api', function()
eq("Invalid 'event' item: expected String, got Array", pcall_err(meths.clear_autocmds, {
event = {'FileType', {}}
}))
eq("Invalid 'group': 0", pcall_err(meths.clear_autocmds, {group = 0}))
end)
it('should clear based on event + pattern', function()