book: the address a device loss names can be any u64

neighbours() added one to it directly, so u64::MAX panicked in a debug
build and wrapped past the range start in a release one: the loss
report aborted instead of naming what was near. The module's lower
bounds already saturate; the upper one does now too, and the book test
asks for the top address.

(cherry picked from commit 18632ec2b639a04c83e5bd74449e12bf28731e93)
This commit is contained in:
Cannnabis authored and trustno1-coder committed 2026-11-27 17:29:34 -08:00
1 parent 0745a5de72
commit 17c07bd8eb
1 file changed
+5 -1
+5 -1
View File
@@ -146,7 +146,9 @@ impl Book {
/// The live entry that starts last at or below `at`, and the next above.
fn neighbours(&self, at: u64) -> [Option<&Entry>; 2] {
let below = self.by_start.range(..=(at, u64::MAX)).next_back();
let above = self.by_start.range((at + 1, 0)..).next();
// Saturating, as the lower bounds above: `at` is an address the
// card reported, any u64.
let above = self.by_start.range((at.saturating_add(1), 0)..).next();
[below, above].map(|e| e.and_then(|((_, id), ())| self.live.get(id)))
}
@@ -748,5 +750,7 @@ mod tests {
assert_eq!(b.neighbours(0x1000_0100)[1].map(|e| e.what.as_str()), Some("two"));
b.remove(two, 3, 13);
assert!(b.neighbours(u64::MAX - 1)[0].is_none() && b.by_object.is_empty());
// An address the card reports can be anything, the top one included.
assert!(b.neighbours(u64::MAX)[0].is_none() && b.neighbours(u64::MAX)[1].is_none());
}
}