fix(lua): memory leak when using invalid syntax with exists() (#26530)

This commit is contained in:
zeertzjq 2023-12-12 20:34:02 +08:00 committed by GitHub
parent 1d4a5cd185
commit b40170f7a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 36 deletions

View File

@ -2323,10 +2323,12 @@ bool nlua_func_exists(const char *lua_funcname)
vim_snprintf(str, length, "return %s", lua_funcname); vim_snprintf(str, length, "return %s", lua_funcname);
ADD_C(args, CSTR_AS_OBJ(str)); ADD_C(args, CSTR_AS_OBJ(str));
Error err = ERROR_INIT; Error err = ERROR_INIT;
Object result = NLUA_EXEC_STATIC("return type(loadstring(...)()) =='function'", args, &err); Object result = NLUA_EXEC_STATIC("return type(loadstring(...)()) == 'function'", args, &err);
xfree(str); xfree(str);
api_clear_error(&err);
if (result.type != kObjectTypeBoolean) { if (result.type != kObjectTypeBoolean) {
api_free_object(result);
return false; return false;
} }
return result.data.boolean; return result.data.boolean;

View File

@ -3351,32 +3351,23 @@ describe('vim.keymap', function()
eq(1, exec_lua[[return GlobalCount]]) eq(1, exec_lua[[return GlobalCount]])
end) end)
end)
it('exists() can check a lua function', function()
eq(true, exec_lua[[ describe('Vimscript function exists()', function()
_G.test = function() print("hello") end it('can check a lua function', function()
return vim.fn.exists('v:lua.test') == 1 eq(1, exec_lua[[
]]) _G.test = function() print("hello") end
return vim.fn.exists('v:lua.test')
eq(true, exec_lua[[ ]])
return vim.fn.exists('v:lua.require("mpack").decode') == 1
]]) eq(1, funcs.exists('v:lua.require("mpack").decode'))
eq(1, funcs.exists("v:lua.require('mpack').decode"))
eq(true, exec_lua[[ eq(1, funcs.exists('v:lua.require"mpack".decode'))
return vim.fn.exists("v:lua.require('vim.lsp').start") == 1 eq(1, funcs.exists("v:lua.require'mpack'.decode"))
]]) eq(1, funcs.exists("v:lua.require('vim.lsp').start"))
eq(1, funcs.exists('v:lua.require"vim.lsp".start'))
eq(true, exec_lua[[ eq(1, funcs.exists("v:lua.require'vim.lsp'.start"))
return vim.fn.exists('v:lua.require"vim.lsp".start') == 1 eq(0, funcs.exists("v:lua.require'vim.lsp'.unknown"))
]]) eq(0, funcs.exists('v:lua.?'))
end)
eq(true, exec_lua[[
return vim.fn.exists("v:lua.require'vim.lsp'.start") == 1
]])
eq(false, exec_lua[[
return vim.fn.exists("v:lua.require'vim.lsp'.unknown") == 1
]])
end)
end) end)

View File

@ -639,9 +639,7 @@ t2]])
{1:}t1 | {1:}t1 |
{1:}^ | {1:}^ |
{1:+}{2:+-- 2 lines: # h2·····················}| {1:+}{2:+-- 2 lines: # h2·····················}|
{3:~ }| {3:~ }|*3
{3:~ }|
{3:~ }|
{4:-- INSERT --} | {4:-- INSERT --} |
]]} ]]}
@ -656,10 +654,7 @@ t2]])
{1:-}^t1 | {1:-}^t1 |
{1:-}# h2 | {1:-}# h2 |
{1:}t2 | {1:}t2 |
{3:~ }| {3:~ }|*4
{3:~ }|
{3:~ }|
{3:~ }|
1 line less; before #2 {MATCH:.*}| 1 line less; before #2 {MATCH:.*}|
]]} ]]}
end) end)