Skip to content

Commit 2015808

Browse files
committed
fix memory overlap check (#39)
1 parent b1568b7 commit 2015808

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

main.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,15 @@ template <typename T> struct range_map {
177177
if (r.to != r.from) {
178178
assert(r.to > r.from);
179179
// check we don't overlap any existing map entries
180-
auto f = m.lower_bound(r.to); // f is first element that starts after or on r.to
181-
if (f != m.begin()) {
182-
f--;
183-
}
184-
if (f != m.end()) {
185-
// due to edge cases above, f is either the entry before
186-
// or after r, so just check for any overlap
187-
fail(ERROR_FORMAT, "Found overlapping memory ranges 0x%08x->0x%08x and 0x%08x->%08x\n", f->first, f->second.first, r.from, r.to);
180+
181+
auto f = m.upper_bound(r.from); // first element that starts after r.from
182+
if (f != m.begin()) f--; // back up, to catch element that starts on or before r.from
183+
for(; f != m.end() && f->first < r.to; f++) { // loop till we can't possibly overlap
184+
range r2(f->first, f->second.first);
185+
if (r2.intersects(r)) {
186+
fail(ERROR_FORMAT, "Found overlapping memory ranges 0x%08x->0x%08x and 0x%08x->%08x\n",
187+
r.from, r.to, r2.from, r2.to);
188+
}
188189
}
189190
m.insert(std::make_pair(r.from, std::make_pair(r.to, t)));
190191
}

0 commit comments

Comments
 (0)