From 160b93c0b867f8e483f967fed839dedba3a27972 Mon Sep 17 00:00:00 2001 From: MCJack123 Date: Sun, 21 Mar 2021 11:31:58 -0400 Subject: [PATCH] Fixed crash when passing bad arg to table.foreach Also added a safety net to lua_getmetatable - someone crashed there? --- src/lapi.c | 1 + src/ltablib.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lapi.c b/src/lapi.c index c12dc12..d9db268 100644 --- a/src/lapi.c +++ b/src/lapi.c @@ -607,6 +607,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { case LUA_TUSERDATA: mt = uvalue(obj)->metatable; break; + case LUA_TNONE: break; /* safety net */ default: mt = G(L)->mt[ttype(obj)]; break; diff --git a/src/ltablib.c b/src/ltablib.c index 7e12d82..a0de881 100644 --- a/src/ltablib.c +++ b/src/ltablib.c @@ -28,7 +28,7 @@ static int foreachi (lua_State *L) { goto resume; } n = aux_igetn(L, 1, -1); - luaL_checktype(L, 2, LUA_TFUNCTION); + luaL_checktype(L, 2, LUA_TFUNCTION); lua_settop(L, 2); lua_pushinteger(L, n); /* cache n because aux_igetn may be expensive */ for (i=1; i <= n; i++) { @@ -47,6 +47,7 @@ static int foreachi (lua_State *L) { static int foreach (lua_State *L) { if (lua_vcontext(L)) goto resume; + luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 2, LUA_TFUNCTION); lua_pushnil(L); /* first key */ while (lua_next(L, 1)) {