-
Notifications
You must be signed in to change notification settings - Fork 21.5k
core/rawdb: fix underflow in freezer inspect for empty ancients #33203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VolodymyrBg
wants to merge
3
commits into
ethereum:master
Choose a base branch
from
VolodymyrBg:fix/freezer-inspect-empty-ancients
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
core/rawdb: fix underflow in freezer inspect for empty ancients #33203
VolodymyrBg
wants to merge
3
commits into
ethereum:master
from
VolodymyrBg:fix/freezer-inspect-empty-ancients
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
good catch, how about this? diff --git a/core/rawdb/ancient_utils.go b/core/rawdb/ancient_utils.go
index b940d91040..ccaa9776be 100644
--- a/core/rawdb/ancient_utils.go
+++ b/core/rawdb/ancient_utils.go
@@ -34,14 +34,10 @@ type freezerInfo struct {
name string // The identifier of freezer
head uint64 // The number of last stored item in the freezer
tail uint64 // The number of first stored item in the freezer
+ count uint64 // The number of stored items in the freezer
sizes []tableSize // The storage size per table
}
-// count returns the number of stored items in the freezer.
-func (info *freezerInfo) count() uint64 {
- return info.head - info.tail + 1
-}
-
// size returns the storage size of the entire freezer.
func (info *freezerInfo) size() common.StorageSize {
var total common.StorageSize
@@ -65,14 +61,23 @@ func inspect(name string, order map[string]freezerTableConfig, reader ethdb.Anci
if err != nil {
return freezerInfo{}, err
}
- info.head = ancients - 1
-
+ if ancients == 0 {
+ info.head = 0
+ } else {
+ info.head = ancients - 1
+ }
// Retrieve the number of first stored item
tail, err := reader.Tail()
if err != nil {
return freezerInfo{}, err
}
info.tail = tail
+
+ if ancients == 0 {
+ info.count = 0
+ } else {
+ info.count = info.head - info.tail + 1
+ }
return info, nil
}
diff --git a/core/rawdb/database.go b/core/rawdb/database.go
index d5c0f0aab2..448922d9bd 100644
--- a/core/rawdb/database.go
+++ b/core/rawdb/database.go
@@ -644,7 +644,7 @@ func InspectDatabase(db ethdb.Database, keyPrefix, keyStart []byte) error {
fmt.Sprintf("Ancient store (%s)", strings.Title(ancient.name)),
strings.Title(table.name),
table.size.String(),
- fmt.Sprintf("%d", ancient.count()),
+ fmt.Sprintf("%d", ancient.count),
})
}
total.Add(uint64(ancient.size()))
|
Contributor
Author
Ok, implemented |
core/rawdb/ancient_utils.go
Outdated
| func (info *freezerInfo) count() uint64 { | ||
| return info.head - info.tail + 1 | ||
| name string // The identifier of freezer | ||
| ancients uint64 // The total number of stored items in the freezer |
Member
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not needed, please remove it
Contributor
Author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not needed, please remove it
corrected
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
fixed an underflow in core/rawdb/ancient_utils.go where inspect() assigned info.head = ancients - 1 unconditionally, which produced ^uint64(0) when Ancients() == 0. Although freezerInfo.count() previously happened to return 0 in the empty case due to unsigned wraparound and the invariant that Tail() == 0 for an empty freezer, the code was fragile and left head invalid. The fix adds an explicit ancients field to freezerInfo, computes the count as ancients - tail with a guard for ancients <= tail, and only sets head when ancients > 0. This aligns the implementation with the ethdb interface contract (see ethdb/database.go for Ancients() and Tail() semantics) and preserves behavior for non-empty freezers while removing reliance on overflow behavior. The changes are localized to core/rawdb/ancient_utils.go (freezerInfo, count(), and inspect()), and callers such as InspectDatabase in core/rawdb/database.go continue to function the same, now with explicit and safe arithmetic.