chiark / gitweb /
zcoord: Fix some more edge case bugs in range algorithm
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 17 Jul 2021 11:21:35 +0000 (12:21 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 17 Jul 2021 13:18:49 +0000 (14:18 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
base/zcoord.rs

index 3f49412601e38b4b7262d3dddf95e4dca2ec5a08..dcb944dc249a013cb17f9d8954d4feddc55163ce 100644 (file)
@@ -382,8 +382,10 @@ impl Mutable {
     let mut current = a.clone();
     let mut borrowing = false;
     let aso = 'ok: loop { for i in 0.. {
-      if i >= a.limbs.len() && i >= b.limbs.len() {
+      if i > a.limbs.len() && i > b.limbs.len() {
        // Oh actually these numbers are equal!
+        // (We have to tolerate one limb eyond each number, because possibly
+        // we had to borrow and add a limb to the longer number.)
        throw!(RangeImpossible::Empty);
       }
       //dbgc!(&a, &b, &count, i, &current, borrowing);
@@ -392,7 +394,11 @@ impl Mutable {
 
       let la = a.limb_val_lookup(i);
       let lb = b.limb_val_lookup(i);
-      if la == lb { continue }
+      if la == lb && ! borrowing {
+        // We have not yet found any difference between these numbers
+        // (If we had but it wasn't enough, `borrowing` would be `true`.
+        continue
+      }
 
       let wantgaps = count+1;
       let avail = (lb.primitive() as i64) - (la.primitive() as i64)