From 0b3f4c57653153ff420f981e54a606fe7a5fea5b Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 21 Mar 2022 13:48:59 +0000 Subject: [PATCH] Prevent usize overflow when no records found --- lib/src/dbs/channel.rs | 8 ++++++-- lib/src/dbs/iterate.rs | 8 ++++++-- lib/src/kvs/tx.rs | 32 ++++++++++++++++++++++++-------- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/lib/src/dbs/channel.rs b/lib/src/dbs/channel.rs index 4a14cac3..e9a3034e 100644 --- a/lib/src/dbs/channel.rs +++ b/lib/src/dbs/channel.rs @@ -147,12 +147,16 @@ impl Table { }; if !res.is_empty() { // Get total results - let n = res.len() - 1; + let n = res.len(); + // Exit when settled + if n == 0 { + break; + } // Loop over results for (i, (k, v)) in res.into_iter().enumerate() { if ctx.is_ok() { // Ready the next - if i == n { + if n == i + 1 { nxt = Some(k.clone()); } // Parse the key-value diff --git a/lib/src/dbs/iterate.rs b/lib/src/dbs/iterate.rs index a4afa641..cfb92a69 100644 --- a/lib/src/dbs/iterate.rs +++ b/lib/src/dbs/iterate.rs @@ -144,12 +144,16 @@ impl Table { }; if !res.is_empty() { // Get total results - let n = res.len() - 1; + let n = res.len(); + // Exit when settled + if n == 0 { + break; + } // Loop over results for (i, (k, v)) in res.into_iter().enumerate() { if ctx.is_ok() { // Ready the next - if i == n { + if n == i + 1 { nxt = Some(k.clone()); } // Parse the key-value diff --git a/lib/src/kvs/tx.rs b/lib/src/kvs/tx.rs index 0569c620..2455bb9a 100644 --- a/lib/src/kvs/tx.rs +++ b/lib/src/kvs/tx.rs @@ -191,11 +191,15 @@ impl Transaction { } }; // Get total results - let n = res.len() - 1; + let n = res.len(); + // Exit when settled + if n == 0 { + break; + } // Loop over results for (i, (k, v)) in res.into_iter().enumerate() { // Ready the next - if i == n { + if n == i + 1 { nxt = Some(k.clone()); } // Delete @@ -234,11 +238,15 @@ impl Transaction { } }; // Get total results - let n = res.len() - 1; + let n = res.len(); + // Exit when settled + if n == 0 { + break; + } // Loop over results for (i, (k, _)) in res.into_iter().enumerate() { // Ready the next - if i == n { + if n == i + 1 { nxt = Some(k.clone()); } // Delete @@ -278,11 +286,15 @@ impl Transaction { } }; // Get total results - let n = res.len() - 1; + let n = res.len(); + // Exit when settled + if n == 0 { + break; + } // Loop over results for (i, (k, v)) in res.into_iter().enumerate() { // Ready the next - if i == n { + if n == i + 1 { nxt = Some(k.clone()); } // Delete @@ -321,11 +333,15 @@ impl Transaction { } }; // Get total results - let n = res.len() - 1; + let n = res.len(); + // Exit when settled + if n == 0 { + break; + } // Loop over results for (i, (k, _)) in res.into_iter().enumerate() { // Ready the next - if i == n { + if n == i + 1 { nxt = Some(k.clone()); } // Delete