Skip to content

Commit

Permalink
Fixed crash when passing bad arg to table.foreach
Browse files Browse the repository at this point in the history
Also added a safety net to lua_getmetatable - someone crashed there?
  • Loading branch information
MCJack123 committed Mar 21, 2021
1 parent a1e9550 commit 160b93c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/ltablib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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)) {
Expand Down

0 comments on commit 160b93c

Please sign in to comment.