Skip to content

Commit 52c484d

Browse files
authored
triedb/pathdb: catch int conversion overflow in 32-bit (#32899)
The limit check for `MaxUint32` is done after the cast to `int`. On 64 bits machines, that will work without a problem. On 32 bits machines, that will always fail. The compiler catches it and refuses to build. Note that this only fixes the compiler build. ~~If the limit is above `MaxInt32` but strictly below `MaxUint32` then this will fail at runtime and we have another issue.~~ I checked and this should not happen during regular execution, although it might happen in tests.
1 parent 6337577 commit 52c484d

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

triedb/pathdb/history_trienode.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func newTrienodeHistory(root common.Hash, parent common.Hash, block uint64, node
161161
// sharedLen returns the length of the common prefix shared by a and b.
162162
func sharedLen(a, b []byte) int {
163163
n := min(len(a), len(b))
164-
for i := 0; i < n; i++ {
164+
for i := range n {
165165
if a[i] != b[i] {
166166
return i
167167
}
@@ -295,7 +295,7 @@ func decodeHeader(data []byte) (*trienodeMetadata, []common.Hash, []uint32, []ui
295295
keyOffsets = make([]uint32, 0, count)
296296
valOffsets = make([]uint32, 0, count)
297297
)
298-
for i := 0; i < count; i++ {
298+
for i := range count {
299299
n := trienodeMetadataSize + trienodeTrieHeaderSize*i
300300
owner := common.BytesToHash(data[n : n+common.HashLength])
301301
if i != 0 && bytes.Compare(owner.Bytes(), owners[i-1].Bytes()) <= 0 {
@@ -348,7 +348,7 @@ func decodeSingle(keySection []byte, onValue func([]byte, int, int) error) ([]st
348348
if len(keySection) < int(8*nRestarts)+4 {
349349
return nil, fmt.Errorf("key section too short, restarts: %d, size: %d", nRestarts, len(keySection))
350350
}
351-
for i := 0; i < int(nRestarts); i++ {
351+
for i := range int(nRestarts) {
352352
o := len(keySection) - 4 - (int(nRestarts)-i)*8
353353
keyOffset := binary.BigEndian.Uint32(keySection[o : o+4])
354354
if i != 0 && keyOffset <= keyOffsets[i-1] {
@@ -469,7 +469,7 @@ func (h *trienodeHistory) decode(header []byte, keySection []byte, valueSection
469469
h.nodeList = make(map[common.Hash][]string)
470470
h.nodes = make(map[common.Hash]map[string][]byte)
471471

472-
for i := 0; i < len(owners); i++ {
472+
for i := range len(owners) {
473473
// Resolve the boundary of key section
474474
keyStart := keyOffsets[i]
475475
keyLimit := len(keySection)
@@ -524,7 +524,7 @@ func newSingleTrienodeHistoryReader(id uint64, reader ethdb.AncientReader, keyRa
524524
}
525525
keyStart := int(keyRange.start)
526526
keyLimit := int(keyRange.limit)
527-
if keyLimit == math.MaxUint32 {
527+
if keyRange.limit == math.MaxUint32 {
528528
keyLimit = len(keyData)
529529
}
530530
if len(keyData) < keyStart || len(keyData) < keyLimit {

0 commit comments

Comments
 (0)