From f3bf1fbf600050fde155e6a1a766b6f848012208 Mon Sep 17 00:00:00 2001 From: ii14 <59243201+ii14@users.noreply.github.com> Date: Mon, 5 Dec 2022 19:59:04 +0100 Subject: fix(secure): crash when hitting escape in prompt (#21283) - use pcall when calling vim.secure.read from C - catch keyboard interrupts in vim.secure.read, rethrow other errors - selecting "view" in prompt runs :view command - simplify lua stack cleanup with lua_gettop and lua_settop Co-authored-by: ii14 --- src/nvim/lua/executor.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 48ab2e3d70..1c8fe3e28e 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -2197,11 +2197,17 @@ plain: char *nlua_read_secure(const char *path) { lua_State *const lstate = global_lstate; + const int top = lua_gettop(lstate); + lua_getglobal(lstate, "vim"); lua_getfield(lstate, -1, "secure"); lua_getfield(lstate, -1, "read"); lua_pushstring(lstate, path); - lua_call(lstate, 1, 1); + if (nlua_pcall(lstate, 1, 1)) { + nlua_error(lstate, _("Error executing vim.secure.read: %.*s")); + lua_settop(lstate, top); + return NULL; + } size_t len = 0; const char *contents = lua_tolstring(lstate, -1, &len); @@ -2212,15 +2218,15 @@ char *nlua_read_secure(const char *path) memcpy(buf, contents, len + 1); } - // Pop return value, "vim", and "secure" - lua_pop(lstate, 3); - + lua_settop(lstate, top); return buf; } bool nlua_trust(const char *action, const char *path) { lua_State *const lstate = global_lstate; + const int top = lua_gettop(lstate); + lua_getglobal(lstate, "vim"); lua_getfield(lstate, -1, "secure"); lua_getfield(lstate, -1, "trust"); @@ -2241,6 +2247,7 @@ bool nlua_trust(const char *action, const char *path) if (nlua_pcall(lstate, 1, 2)) { nlua_error(lstate, _("Error executing vim.secure.trust: %.*s")); + lua_settop(lstate, top); return false; } @@ -2260,8 +2267,6 @@ bool nlua_trust(const char *action, const char *path) } } - // Pop return values, "vim" and "secure" - lua_pop(lstate, 4); - + lua_settop(lstate, top); return success; } -- cgit