From 21d6af287e5ae1e61d6184920ce7525ee88418b1 Mon Sep 17 00:00:00 2001
From: Connor McLaughlin <stenzek@gmail.com>
Date: Fri, 14 May 2021 16:43:11 +1000
Subject: [PATCH] System: Fix a possible crash with runahead

---
 src/core/system.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/core/system.cpp b/src/core/system.cpp
index 8ad14096e..01210d588 100644
--- a/src/core/system.cpp
+++ b/src/core/system.cpp
@@ -2306,8 +2306,11 @@ void DoRunahead()
   {
     // we need to replay and catch up - load the state,
     s_runahead_replay_pending = false;
-    if (!LoadMemoryState(s_runahead_states.front()))
+    if (s_runahead_states.empty() || !LoadMemoryState(s_runahead_states.front()))
+    {
+      s_runahead_states.clear();
       return;
+    }
 
     // and throw away all the states, forcing us to catch up below
     // TODO: can we leave one frame here and run, avoiding the extra save?
@@ -2365,7 +2368,7 @@ void DoMemorySaveStates()
 
 void SetRunaheadReplayFlag()
 {
-  if (s_runahead_frames == 0)
+  if (s_runahead_frames == 0 || s_runahead_states.empty())
     return;
 
   Log_DevPrintf("Runahead rewind pending...");