From e1d15b5ed5077ae459715e6a9fc81ceb62b7ca9c Mon Sep 17 00:00:00 2001
From: Hanicef <gustaf@hanicef.me>
Date: Wed, 7 Feb 2024 18:42:24 +0100
Subject: [PATCH] Fix segfault when removing mobjs while iterating thinglist

---
 src/lua_maplib.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/lua_maplib.c b/src/lua_maplib.c
index f8e2808a2..b629c764d 100644
--- a/src/lua_maplib.c
+++ b/src/lua_maplib.c
@@ -486,6 +486,8 @@ static int lib_iterateSectorThinglist(lua_State *L)
 	if (!lua_isnil(L, 1))
 	{
 		thing = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ));
+		if (P_MobjWasRemoved(thing))
+			return luaL_error(L, "current entry in thinglist was removed; avoid calling P_MobjWasRemoved on entries!");
 		thing = thing->snext;
 	}
 	else
-- 
GitLab