Skip to content

Commit 47ef8aa

Browse files
committed
add prefix check in NewIter
1 parent b5eaf02 commit 47ef8aa

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

storage/operation/badgerimpl/reader.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package badgerimpl
22

33
import (
4+
"bytes"
45
"errors"
6+
"fmt"
57
"io"
68

79
"github.com/dgraph-io/badger/v2"
@@ -54,8 +56,13 @@ func (b dbReader) Get(key []byte) ([]byte, io.Closer, error) {
5456
// - have a prefix equal to the endPrefix OR
5557
// - have a prefix that is lexicographically between startPrefix and endPrefix
5658
//
59+
// it returns error if the startPrefix key is greater than the endPrefix key
5760
// no errors are expected during normal operation
5861
func (b dbReader) NewIter(startPrefix, endPrefix []byte, ops storage.IteratorOption) (storage.Iterator, error) {
62+
if bytes.Compare(startPrefix, endPrefix) > 0 {
63+
return nil, fmt.Errorf("startPrefix key must be less than or equal to endPrefix key")
64+
}
65+
5966
return newBadgerIterator(b.db, startPrefix, endPrefix, ops), nil
6067
}
6168

storage/operation/pebbleimpl/reader.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package pebbleimpl
22

33
import (
4+
"bytes"
45
"errors"
6+
"fmt"
57
"io"
68

79
"github.com/cockroachdb/pebble"
@@ -50,7 +52,14 @@ func (b dbReader) Get(key []byte) ([]byte, io.Closer, error) {
5052
// - have a prefix equal to startPrefix OR
5153
// - have a prefix equal to the endPrefix OR
5254
// - have a prefix that is lexicographically between startPrefix and endPrefix
55+
//
56+
// it returns error if the startPrefix key is greater than the endPrefix key
57+
// no errors are expected during normal operation
5358
func (b dbReader) NewIter(startPrefix, endPrefix []byte, ops storage.IteratorOption) (storage.Iterator, error) {
59+
if bytes.Compare(startPrefix, endPrefix) > 0 {
60+
return nil, fmt.Errorf("startPrefix key must be less than or equal to endPrefix key")
61+
}
62+
5463
return newPebbleIterator(b.db, startPrefix, endPrefix, ops)
5564
}
5665

0 commit comments

Comments
 (0)