mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 05:06:43 +00:00
prevent integer underflow in ReadAllHashesInRange
This commit is contained in:
parent
d2a5dba48f
commit
0dc323e853
1 changed files with 13 additions and 1 deletions
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"math/big"
|
"math/big"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
|
|
@ -87,10 +88,21 @@ type NumberHash struct {
|
||||||
// heights, both canonical and reorged forks included.
|
// heights, both canonical and reorged forks included.
|
||||||
// This method considers both limits to be _inclusive_.
|
// This method considers both limits to be _inclusive_.
|
||||||
func ReadAllHashesInRange(db ethdb.Iteratee, first, last uint64) []*NumberHash {
|
func ReadAllHashesInRange(db ethdb.Iteratee, first, last uint64) []*NumberHash {
|
||||||
|
// Return empty result for inverted ranges
|
||||||
|
if last < first {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
rangeLen := last - first
|
||||||
|
var capHint int
|
||||||
|
if rangeLen >= uint64(math.MaxInt) {
|
||||||
|
capHint = math.MaxInt
|
||||||
|
} else {
|
||||||
|
capHint = int(rangeLen) + 1
|
||||||
|
}
|
||||||
var (
|
var (
|
||||||
start = encodeBlockNumber(first)
|
start = encodeBlockNumber(first)
|
||||||
keyLength = len(headerPrefix) + 8 + 32
|
keyLength = len(headerPrefix) + 8 + 32
|
||||||
hashes = make([]*NumberHash, 0, 1+last-first)
|
hashes = make([]*NumberHash, 0, capHint)
|
||||||
it = db.NewIterator(headerPrefix, start)
|
it = db.NewIterator(headerPrefix, start)
|
||||||
)
|
)
|
||||||
defer it.Release()
|
defer it.Release()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue