vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit a2d06b2eeaecd5e2018309d000a6614200266e72
parent bc97e9920e701c9238070b4ea1c51139f3af069f
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Fri, 24 Mar 2017 10:42:00 +0100

vis-lua: adjust return value validation of called lua functions

While the invoked Lua functions are executed in protected mode,
the validation of the return values currently happens in unprotected
mode. Thus an invaid return value triggers a lua error and because
we currently do not have a global panic handler registered this will
terminate the editor process.

This commit changes the return value validation to silently fall
back to default values instead of raising errors. If we want to provide
user friendly stack traces showing the origin of the offending value
we would have to move the validation into the Lua code.

Diffstat:
Mvis-lua.c | 10+++++++---
1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/vis-lua.c b/vis-lua.c @@ -479,6 +479,10 @@ static int newindex_common(lua_State *L) { return 0; } +static size_t getpos(lua_State *L, int narg) { + return lua_tounsigned(L, narg); +} + static size_t checkpos(lua_State *L, int narg) { lua_Number n = luaL_checknumber(L, narg); if (n >= 0 && n <= SIZE_MAX && n == (size_t)n) @@ -911,7 +915,7 @@ static size_t motion_lua(Vis *vis, Win *win, void *data, size_t pos) { lua_pushunsigned(L, pos); if (pcall(vis, L, 2, 1) != 0) return EPOS; - return checkpos(L, -1); + return getpos(L, -1); } /*** @@ -965,7 +969,7 @@ static size_t operator_lua(Vis *vis, Text *text, OperatorContext *c) { pushpos(L, c->pos); if (pcall(vis, L, 3, 1) != 0) return EPOS; - return checkpos(L, -1); + return getpos(L, -1); } /*** @@ -1017,7 +1021,7 @@ static Filerange textobject_lua(Vis *vis, Win *win, void *data, size_t pos) { lua_pushunsigned(L, pos); if (pcall(vis, L, 2, 2) != 0 || lua_isnil(L, -1)) return text_range_empty(); - return text_range_new(checkpos(L, -2), checkpos(L, -1)); + return text_range_new(getpos(L, -2), getpos(L, -1)); } /***