Skip to content

Commit

Permalink
ucode-mod-lua: various fixes
Browse files Browse the repository at this point in the history
Properly handle accesses to properties of the userdatum itself in the
lua_uv_index() __index metamethod and treat integer keys as array indexes
in case of wrapped ucode array values. Also fix an incorrect refcount
decrement in the function.

Also fix uc_lua_vm_get() and uc_lua_lv_getraw() to gracefully handle
accesses to not defined or non-table values and ensure that those functions
properly reset the Lua stack after they complete.

Signed-off-by: Jo-Philipp Wich <[email protected]>
  • Loading branch information
jow- committed Aug 29, 2022
1 parent 36460df commit 629eb17
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions contrib/package/ucode-mod-lua/src/lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -460,17 +460,20 @@ lua_uv_index(lua_State *L)
{
ucv_userdata_t *ud = luaL_checkudata(L, 1, "ucode.value");
const char *key = luaL_checkstring(L, 2);
uc_value_t *proto, *rv;
bool found;
long long idx;
char *e;

proto = ucv_prototype_get(ud->uv);
rv = ucv_object_get(proto, key, &found);
if (ucv_type(ud->uv) == UC_ARRAY) {
idx = strtoll(key, &e, 10);

if (!found)
return 0;
if (e != key && *e == 0 && idx >= 1 && idx <= (long long)ucv_array_length(ud->uv)) {
ucv_to_lua(ud->vm, ucv_array_get(ud->uv, (size_t)(idx - 1)), L, NULL);

ucv_to_lua(ud->vm, rv, L, NULL);
ucv_put(rv);
return 1;
}
}

ucv_to_lua(ud->vm, ucv_property_get(ud->uv, key), L, NULL);

return 1;
}
Expand Down Expand Up @@ -687,13 +690,22 @@ uc_lua_vm_get(uc_vm_t *vm, size_t nargs)
uc_value_t *key = uc_fn_arg(0);
lua_resource_t *lv;
size_t i;
int top;

if (!L || !*L || ucv_type(key) != UC_STRING)
return NULL;

top = lua_gettop(*L);

lua_getglobal(*L, ucv_string_get(key));

for (i = 1; i < nargs; i++) {
if (lua_type(*L, -1) != LUA_TTABLE) {
lua_settop(*L, top);

return NULL;
}

ucv_to_lua(vm, uc_fn_arg(i), *L, NULL);
lua_gettable(*L, -2);
}
Expand All @@ -702,8 +714,7 @@ uc_lua_vm_get(uc_vm_t *vm, size_t nargs)
lv->ref = luaL_ref(*L, LUA_REGISTRYINDEX);
lv->uvL = ucv_this_to_uvL(vm);

if (nargs > 1)
lua_pop(*L, nargs - 1);
lua_settop(*L, top);

return uc_resource_new(lv_type, lv);
}
Expand Down Expand Up @@ -788,15 +799,24 @@ uc_lua_lv_get_common(uc_vm_t *vm, size_t nargs, bool raw)
lua_State *L = uc_lua_lv_to_L(lv);
uc_value_t *key;
size_t i;
int top;

if (!L)
return NULL;

top = lua_gettop(L);

lua_rawgeti(L, LUA_REGISTRYINDEX, (*lv)->ref);

for (i = 0; i < nargs; i++) {
key = uc_fn_arg(i);

if (lua_type(L, -1) != LUA_TTABLE) {
lua_settop(L, top);

return NULL;
}

if (raw) {
if (ucv_type(key) == UC_INTEGER) {
lua_rawgeti(L, -1, (int)ucv_int64_get(key));
Expand All @@ -816,7 +836,7 @@ uc_lua_lv_get_common(uc_vm_t *vm, size_t nargs, bool raw)
ref->ref = luaL_ref(L, LUA_REGISTRYINDEX);
ref->uvL = ucv_this_to_uvL(vm);

lua_pop(L, nargs);
lua_settop(L, top);

return uc_resource_new(lv_type, ref);
}
Expand Down

0 comments on commit 629eb17

Please sign in to comment.