check length outside

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2025-07-02 02:21:49 +08:00 committed by Gary Rong
parent 95dbdb3b1d
commit e6e78637e7
2 changed files with 6 additions and 27 deletions

View file

@ -189,14 +189,7 @@ func ReadStateAccountIndex(db ethdb.AncientReaderOp, id uint64) []byte {
// history in freezer by minus one since the id of first state history starts // history in freezer by minus one since the id of first state history starts
// from one (zero for initial state). // from one (zero for initial state).
func ReadStateStorageIndex(db ethdb.AncientReaderOp, id uint64, offset, length int) ([]byte, error) { func ReadStateStorageIndex(db ethdb.AncientReaderOp, id uint64, offset, length int) ([]byte, error) {
blob, err := db.AncientBytes(stateHistoryStorageIndex, id-1, uint64(offset), uint64(length)) return db.AncientBytes(stateHistoryStorageIndex, id-1, uint64(offset), uint64(length))
if err != nil {
return nil, err
}
if len(blob) != length {
return blob, errors.New("state storage index data length mismatch")
}
return blob, nil
} }
// ReadStateAccountHistory retrieves the concatenated account data blob for the // ReadStateAccountHistory retrieves the concatenated account data blob for the
@ -204,14 +197,7 @@ func ReadStateStorageIndex(db ethdb.AncientReaderOp, id uint64, offset, length i
// index. Compute the position of state history in freezer by minus one since // index. Compute the position of state history in freezer by minus one since
// the id of first state history starts from one (zero for initial state). // the id of first state history starts from one (zero for initial state).
func ReadStateAccountHistory(db ethdb.AncientReaderOp, id uint64, offset, length int) ([]byte, error) { func ReadStateAccountHistory(db ethdb.AncientReaderOp, id uint64, offset, length int) ([]byte, error) {
blob, err := db.AncientBytes(stateHistoryAccountData, id-1, uint64(offset), uint64(length)) return db.AncientBytes(stateHistoryAccountData, id-1, uint64(offset), uint64(length))
if err != nil {
return nil, err
}
if len(blob) != length {
return blob, errors.New("state account history data length mismatch")
}
return blob, nil
} }
// ReadStateStorageHistory retrieves the concatenated storage slot data blob for // ReadStateStorageHistory retrieves the concatenated storage slot data blob for
@ -220,14 +206,7 @@ func ReadStateAccountHistory(db ethdb.AncientReaderOp, id uint64, offset, length
// one since the id of first state history starts from one (zero for initial // one since the id of first state history starts from one (zero for initial
// state). // state).
func ReadStateStorageHistory(db ethdb.AncientReaderOp, id uint64, offset, length int) ([]byte, error) { func ReadStateStorageHistory(db ethdb.AncientReaderOp, id uint64, offset, length int) ([]byte, error) {
blob, err := db.AncientBytes(stateHistoryStorageData, id-1, uint64(offset), uint64(length)) return db.AncientBytes(stateHistoryStorageData, id-1, uint64(offset), uint64(length))
if err != nil {
return nil, err
}
if len(blob) != length {
return blob, errors.New("state storage history data length mismatch")
}
return blob, nil
} }
// ReadStateHistory retrieves the state history from database with provided id. // ReadStateHistory retrieves the state history from database with provided id.

View file

@ -145,7 +145,7 @@ func (r *historyReader) readAccountMetadata(address common.Address, historyID ui
// state history. // state history.
func (r *historyReader) readStorageMetadata(storageKey common.Hash, storageHash common.Hash, historyID uint64, slotOffset, slotNumber int) ([]byte, error) { func (r *historyReader) readStorageMetadata(storageKey common.Hash, storageHash common.Hash, historyID uint64, slotOffset, slotNumber int) ([]byte, error) {
data, err := rawdb.ReadStateStorageIndex(r.freezer, historyID, slotIndexSize*slotOffset, slotIndexSize*slotNumber) data, err := rawdb.ReadStateStorageIndex(r.freezer, historyID, slotIndexSize*slotOffset, slotIndexSize*slotNumber)
if err != nil { if err != nil || len(data) != slotIndexSize*slotNumber {
return nil, fmt.Errorf("storage indices is truncated, historyID: %d, size: %d, offset: %d, length: %d", historyID, len(data), slotOffset, slotNumber) return nil, fmt.Errorf("storage indices is truncated, historyID: %d, size: %d, offset: %d, length: %d", historyID, len(data), slotOffset, slotNumber)
} }
@ -187,7 +187,7 @@ func (r *historyReader) readAccount(address common.Address, historyID uint64) ([
offset := int(binary.BigEndian.Uint32(metadata[common.AddressLength+1 : common.AddressLength+5])) // four bytes for the account data offset offset := int(binary.BigEndian.Uint32(metadata[common.AddressLength+1 : common.AddressLength+5])) // four bytes for the account data offset
data, err := rawdb.ReadStateAccountHistory(r.freezer, historyID, offset, length) data, err := rawdb.ReadStateAccountHistory(r.freezer, historyID, offset, length)
if err != nil { if err != nil || len(data) != length {
return nil, fmt.Errorf("account data is truncated, address: %#x, historyID: %d, size: %d, offset: %d, len: %d", address, historyID, len(data), offset, length) return nil, fmt.Errorf("account data is truncated, address: %#x, historyID: %d, size: %d, offset: %d, len: %d", address, historyID, len(data), offset, length)
} }
return data, nil return data, nil
@ -214,7 +214,7 @@ func (r *historyReader) readStorage(address common.Address, storageKey common.Ha
offset := int(binary.BigEndian.Uint32(slotMetadata[common.HashLength+1 : common.HashLength+5])) // four bytes for slot data offset offset := int(binary.BigEndian.Uint32(slotMetadata[common.HashLength+1 : common.HashLength+5])) // four bytes for slot data offset
data, err := rawdb.ReadStateStorageHistory(r.freezer, historyID, offset, length) data, err := rawdb.ReadStateStorageHistory(r.freezer, historyID, offset, length)
if err != nil { if err != nil || len(data) != length {
return nil, fmt.Errorf("storage data is truncated, address: %#x, key: %#x, historyID: %d, size: %d, offset: %d, len: %d", address, storageKey, historyID, len(data), offset, length) return nil, fmt.Errorf("storage data is truncated, address: %#x, key: %#x, historyID: %d, size: %d, offset: %d, len: %d", address, storageKey, historyID, len(data), offset, length)
} }
return data, nil return data, nil