Skip to content

Commit

Permalink
fix: add raw iterator validation before calling next method (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksuss committed Oct 26, 2023
1 parent 266571e commit 4b8a2ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/db_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,19 @@ impl<'a, D: DBAccess> DBRawIteratorWithThreadMode<'a, D> {

/// Seeks to the next key.
pub fn next(&mut self) {
unsafe {
ffi::rocksdb_iter_next(self.inner.as_ptr());
if self.valid() {
unsafe {
ffi::rocksdb_iter_next(self.inner.as_ptr());
}
}
}

/// Seeks to the previous key.
pub fn prev(&mut self) {
unsafe {
ffi::rocksdb_iter_prev(self.inner.as_ptr());
if self.valid() {
unsafe {
ffi::rocksdb_iter_prev(self.inner.as_ptr());
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/test_raw_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,17 @@ pub fn test_seek_for_prev() {
assert_item(&iter, b"k2", b"v2");
}
}

#[test]
pub fn test_next_without_seek() {
let n = DBPath::new("test_forgot_seek");
{
let db = DB::open_default(&n).unwrap();
db.put(b"k1", b"v1").unwrap();
db.put(b"k2", b"v2").unwrap();
db.put(b"k4", b"v4").unwrap();

let mut iter = db.raw_iterator();
iter.next();
}
}

0 comments on commit 4b8a2ce

Please sign in to comment.