triedb/pathdb: fix 32-bit integer overflow in history trienode decoder

This commit is contained in:
jsvisa 2025-11-04 17:15:50 +08:00
parent 28c59b7a76
commit 78ff9911c5

View file

@ -19,6 +19,7 @@ package pathdb
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"iter"
"maps"
@ -392,6 +393,12 @@ func decodeSingle(keySection []byte, onValue func([]byte, int, int) error) ([]st
nValue, nn := binary.Uvarint(keySection[keyOff:]) // value length (varint)
keyOff += nn
// Validate that the values can fit in an int to prevent overflow on 32-bit systems
const maxInt = int(^uint(0) >> 1)
if nShared > uint64(maxInt) || nUnshared > uint64(maxInt) || nValue > uint64(maxInt) {
return nil, errors.New("key size too large")
}
// Resolve unshared key
if keyOff+int(nUnshared) > len(keySection) {
return nil, fmt.Errorf("key length too long, unshared key length: %d, off: %d, section size: %d", nUnshared, keyOff, len(keySection))