Skip to content

Commit 8c16f08

Browse files
VolodymyrBgfjl
authored andcommitted
core/rawdb: fix underflow in freezer inspect for empty ancients (ethereum#33203)
1 parent f5a157f commit 8c16f08

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

core/rawdb/ancient_utils.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,10 @@ type freezerInfo struct {
3434
name string // The identifier of freezer
3535
head uint64 // The number of last stored item in the freezer
3636
tail uint64 // The number of first stored item in the freezer
37+
count uint64 // The number of stored items in the freezer
3738
sizes []tableSize // The storage size per table
3839
}
3940

40-
// count returns the number of stored items in the freezer.
41-
func (info *freezerInfo) count() uint64 {
42-
return info.head - info.tail + 1
43-
}
44-
4541
// size returns the storage size of the entire freezer.
4642
func (info *freezerInfo) size() common.StorageSize {
4743
var total common.StorageSize
@@ -65,14 +61,24 @@ func inspect(name string, order map[string]freezerTableConfig, reader ethdb.Anci
6561
if err != nil {
6662
return freezerInfo{}, err
6763
}
68-
info.head = ancients - 1
64+
if ancients > 0 {
65+
info.head = ancients - 1
66+
} else {
67+
info.head = 0
68+
}
6969

7070
// Retrieve the number of first stored item
7171
tail, err := reader.Tail()
7272
if err != nil {
7373
return freezerInfo{}, err
7474
}
7575
info.tail = tail
76+
77+
if ancients == 0 {
78+
info.count = 0
79+
} else {
80+
info.count = info.head - info.tail + 1
81+
}
7682
return info, nil
7783
}
7884

core/rawdb/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ func InspectDatabase(db ethdb.Database, keyPrefix, keyStart []byte) error {
644644
fmt.Sprintf("Ancient store (%s)", strings.Title(ancient.name)),
645645
strings.Title(table.name),
646646
table.size.String(),
647-
fmt.Sprintf("%d", ancient.count()),
647+
fmt.Sprintf("%d", ancient.count),
648648
})
649649
}
650650
total.Add(uint64(ancient.size()))

0 commit comments

Comments
 (0)