shithub: choc

Download patch

ref: 7607140288b936d29f86b67eec1cbfa3eb60f5ca
parent: ed8cbb08e812716e9496db7bc805af27d3eddb41
author: Fabian Greffrath <fabian@greffrath.com>
date: Tue May 24 17:02:05 EDT 2016

Use a heuristic approach to detect infinite state cycles

Count the number of times the loop in P_SetMobjState() executes
and exit with an error once an arbitrary very large limit is reached.

--- a/src/doom/p_mobj.c
+++ b/src/doom/p_mobj.c
@@ -44,6 +44,12 @@
 //
 int test;
 
+// Use a heuristic approach to detect infinite state cycles: Count the number
+// of times the loop in P_SetMobjState() executes and exit with an error once
+// an arbitrary very large limit is reached.
+
+#define MOBJ_CYCLE_LIMIT 1000000
+
 boolean
 P_SetMobjState
 ( mobj_t*	mobj,
@@ -50,6 +56,7 @@
   statenum_t	state )
 {
     state_t*	st;
+    int	cycle_counter = 0;
 
     do
     {
@@ -72,6 +79,11 @@
 	    st->action.acp1(mobj);	
 	
 	state = st->nextstate;
+
+	if (cycle_counter++ > MOBJ_CYCLE_LIMIT)
+	{
+	    I_Error("P_SetMobjState: Infinite state cycle detected!");
+	}
     } while (!mobj->tics);
 				
     return true;