Skip to content

Commit 7a7ee5c

Browse files
authored
Merge pull request #2668 from CortexFoundation/dev
firstKey should be less than or equal to all keys in the list
2 parents 292b281 + da52b45 commit 7a7ee5c

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

trie/proof.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ func hasRightElement(node node, key []byte) bool {
436436
//
437437
// The firstKey is paired with firstProof, not necessarily the same as keys[0]
438438
// (unless firstProof is an existent proof). Similarly, lastKey and lastProof
439-
// are paired.
439+
// are paired. The firstKey should be less than or equal to all keys in the list.
440440
//
441441
// Expect the normal case, this function can also be used to verify the following
442442
// range proofs:
@@ -522,8 +522,14 @@ func VerifyRangeProof(rootHash common.Hash, firstKey []byte, lastKey []byte, key
522522
}
523523
return diskdb, tr, notary, hasRightElement(root, firstKey), nil
524524
}
525+
// Short circuit if the key of first element is greater than firstKey.
526+
// A nil firstKey slice is equivalent to an empty slice.
527+
if bytes.Compare(firstKey, keys[0]) > 0 {
528+
return nil, nil, nil, false, errors.New("unexpected key-value pairs preceding the requested range")
529+
}
525530
// Special case, there is only one element and two edge keys are same.
526531
// In this case, we can't construct two edge paths. So handle it here.
532+
lastKey = keys[len(keys)-1]
527533
if len(keys) == 1 && bytes.Equal(firstKey, lastKey) {
528534
root, val, err := proofToPath(rootHash, nil, firstKey, notary, false)
529535
if err != nil {
@@ -584,7 +590,9 @@ func VerifyRangeProof(rootHash common.Hash, firstKey []byte, lastKey []byte, key
584590
tr.root = nil
585591
}
586592
for index, key := range keys {
587-
tr.Update(key, values[index])
593+
if err := tr.Update(key, values[index]); err != nil {
594+
return nil, nil, nil, false, err
595+
}
588596
}
589597
if tr.Hash() != rootHash {
590598
return nil, nil, nil, false, fmt.Errorf("invalid proof, want hash %x, got %x", rootHash, tr.Hash())

0 commit comments

Comments
 (0)