From e6e78637e77f0d71b2a44d5a87e66bdc4805f97e Mon Sep 17 00:00:00 2001 From: jsvisa Date: Wed, 2 Jul 2025 02:21:49 +0800 Subject: [PATCH] check length outside Signed-off-by: jsvisa --- core/rawdb/accessors_state.go | 27 +++------------------------ triedb/pathdb/history_reader.go | 6 +++--- 2 files changed, 6 insertions(+), 27 deletions(-) diff --git a/core/rawdb/accessors_state.go b/core/rawdb/accessors_state.go index 7a63f55f74..714c1f77d6 100644 --- a/core/rawdb/accessors_state.go +++ b/core/rawdb/accessors_state.go @@ -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 // from one (zero for initial state). func ReadStateStorageIndex(db ethdb.AncientReaderOp, id uint64, offset, length int) ([]byte, error) { - blob, err := 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 + return db.AncientBytes(stateHistoryStorageIndex, id-1, uint64(offset), uint64(length)) } // 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 // 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) { - blob, err := 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 + return db.AncientBytes(stateHistoryAccountData, id-1, uint64(offset), uint64(length)) } // 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 // state). func ReadStateStorageHistory(db ethdb.AncientReaderOp, id uint64, offset, length int) ([]byte, error) { - blob, err := 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 + return db.AncientBytes(stateHistoryStorageData, id-1, uint64(offset), uint64(length)) } // ReadStateHistory retrieves the state history from database with provided id. diff --git a/triedb/pathdb/history_reader.go b/triedb/pathdb/history_reader.go index ba551e3e1e..eaa67bedc5 100644 --- a/triedb/pathdb/history_reader.go +++ b/triedb/pathdb/history_reader.go @@ -145,7 +145,7 @@ func (r *historyReader) readAccountMetadata(address common.Address, historyID ui // state history. 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) - 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) } @@ -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 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 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 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 data, nil