[05/11] gdb, remote: fix crash when accessing removed events

Message ID 20260518183316.127043-6-mohamed.bouhaouel@intel.com
State New
Headers
Series Enable non-stop mode by default for remote targets |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Bouhaouel, Mohamed May 18, 2026, 6:33 p.m. UTC
  From: "Bouhaouel, Mohamed" <mohamed.bouhaouel@intel.com>

Dereferencing events after calling std::remove_if causes GDB to crash
due to accessing invalidated iterators.  Fix by logging events in
std::remove_if.
---
 gdb/remote.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
  

Patch

diff --git a/gdb/remote.c b/gdb/remote.c
index a73362a26ee..94ea4466c17 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -8314,17 +8314,20 @@  remote_target::discard_pending_stop_replies (struct inferior *inf)
 
   /* Discard the stop replies we have already pulled with
      vStopped.  */
-  auto iter = std::remove_if (rs->stop_reply_queue.begin (),
-			      rs->stop_reply_queue.end (),
-			      [=] (const stop_reply_up &event)
-			      {
-				return event->ptid.pid () == inf->pid;
-			      });
-  for (auto it = iter; it != rs->stop_reply_queue.end (); ++it)
-    remote_debug_printf
-      ("discarding queued stop reply: ptid: %s, ws: %s\n",
-       (*it)->ptid.to_string().c_str(),
-       (*it)->ws.to_string ().c_str ());
+  auto iter
+    = std::remove_if (rs->stop_reply_queue.begin (),
+		      rs->stop_reply_queue.end (),
+		      [=] (const stop_reply_up &event)
+		      {
+			if (event->ptid.pid () != inf->pid)
+			  return false;
+
+			remote_debug_printf
+			  ("discarding queued stop reply: ptid: %s, ws: %s\n",
+			   event->ptid.to_string ().c_str (),
+			   event->ws.to_string ().c_str ());
+			return true;
+		      });
   rs->stop_reply_queue.erase (iter, rs->stop_reply_queue.end ());
 }