https://github.com/DavidSpickett created 
https://github.com/llvm/llvm-project/pull/217837

Fixes #217359

The first issue is obvious, when checking the previous breakpoint we should 
push back prev_pos->second, instead of pos->second.

The second problem is what happens when lower_bound returns end(). Before, `if 
(pos != end)` would stop us checking the previous breakpoint.

If lower_bound returned end() but there were breakpoints, the last breakpoint 
may start before addr and extend past it. This was being missed.

To fix that:
* Remove the `if (pos != end)` check.
* Return early if there are no breakpoints.
* If lower_bound does not find the first one, look at the previous one.
* Look at the rest from pos onwards.

The earlly return means we know that
m_breakpoints.begin() != m_breakpoints.end() and so even if lower_bound returns 
end(), we are safe to decrement that iterator.

I think this will fix the API tests in #217348, when those are enabled from 
debugserver.

>From 396578efcebae6d768c5ba8f5a9e3a701e075b9b Mon Sep 17 00:00:00 2001
From: David Spickett <[email protected]>
Date: Fri, 21 Aug 2026 07:40:46 +0000
Subject: [PATCH] [lldb][debugserver] Fix bugs in
 FindBreakpointsThatOverlapRange

Fixes #217359

The first issue is obvious, when checking the previous breakpoint
we should push back prev_pos->second, instead of pos->second.

The second problem is what happens when lower_bound returns end().
Before, `if (pos != end)` would stop us checking the previous
breakpoint.

If lower_bound returned end() but there were breakpoints, the
last breakpoint may start before addr and extend past it. This
was being missed.

To fix that:
* Remove the `if (pos != end)` check.
* Return early if there are no breakpoints.
* If lower_bound does not find the first one, look at
  the previous one.
* Look at the rest from pos onwards.

The earlly return means we know that
m_breakpoints.begin() != m_breakpoints.end() and so even
if lower_bound returns end(), we are safe to decrement that
iterator.

I think this will fix the API tests in #217348, when
those are enabled from debugserver.
---
 .../debugserver/source/DNBBreakpoint.cpp      | 46 ++++++++++---------
 1 file changed, 25 insertions(+), 21 deletions(-)

diff --git a/lldb/tools/debugserver/source/DNBBreakpoint.cpp 
b/lldb/tools/debugserver/source/DNBBreakpoint.cpp
index 74f0fb17129f3..eecce96d2d2bd 100644
--- a/lldb/tools/debugserver/source/DNBBreakpoint.cpp
+++ b/lldb/tools/debugserver/source/DNBBreakpoint.cpp
@@ -125,34 +125,38 @@ DNBBreakpointList::FindNearestWatchpoint(nub_addr_t addr) 
const {
 size_t DNBBreakpointList::FindBreakpointsThatOverlapRange(
     nub_addr_t addr, nub_addr_t size, std::vector<DNBBreakpoint *> &bps) {
   bps.clear();
+
+  if (m_breakpoints.empty())
+    return bps.size();
+
   iterator end = m_breakpoints.end();
   // Find the first breakpoint with an address >= to "addr"
   iterator pos = m_breakpoints.lower_bound(addr);
-  if (pos != end) {
-    if (pos != m_breakpoints.begin()) {
-      // Watch out for a breakpoint at an address less than "addr" that might
-      // still overlap
-      iterator prev_pos = pos;
-      --prev_pos;
-      if (prev_pos->second.IntersectsRange(addr, size, NULL, NULL, NULL))
-        bps.push_back(&pos->second);
-    }
 
-    while (pos != end) {
-      // When we hit a breakpoint whose start address is greater than "addr +
-      // size" we are done.
-      // Do the math in a way that doesn't risk unsigned overflow with bad
-      // input.
-      if ((pos->second.Address() - addr) >= size)
-        break;
+  if (pos != m_breakpoints.begin()) {
+    // Watch out for a breakpoint at an address less than "addr" that might
+    // still overlap
+    iterator prev_pos = pos;
+    --prev_pos;
+    if (prev_pos->second.IntersectsRange(addr, size, NULL, NULL, NULL))
+      bps.push_back(&prev_pos->second);
+  }
 
-      // Check if this breakpoint overlaps, and if it does, add it to the list
-      if (pos->second.IntersectsRange(addr, size, NULL, NULL, NULL))
-        bps.push_back(&pos->second);
+  while (pos != end) {
+    // When we hit a breakpoint whose start address is greater than "addr +
+    // size" we are done.
+    // Do the math in a way that doesn't risk unsigned overflow with bad
+    // input.
+    if ((pos->second.Address() - addr) >= size)
+      break;
 
-      ++pos;
-    }
+    // Check if this breakpoint overlaps, and if it does, add it to the list
+    if (pos->second.IntersectsRange(addr, size, NULL, NULL, NULL))
+      bps.push_back(&pos->second);
+
+    ++pos;
   }
+
   return bps.size();
 }
 

_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to