-
Notifications
You must be signed in to change notification settings - Fork 4.1k
fix: prevent gas type overflow in ABCI responses #25271
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
DeVikingMark
wants to merge
16
commits into
cosmos:main
Choose a base branch
from
DeVikingMark:prevent-gas-type-overflow-in-ABCI-responses
base: main
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.
+179
−13
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8b6626a
Update abci.go
DeVikingMark 8b0016a
Update abci.go
DeVikingMark e025e7e
Update abci_test.go
DeVikingMark beccde3
Merge branch 'main' into prevent-gas-type-overflow-in-ABCI-responses
DeVikingMark bc343b5
Update int.go
DeVikingMark 4991c0c
Update int_test.go
DeVikingMark d7df113
Update abci.go
DeVikingMark 3621fe2
Update baseapp.go
DeVikingMark ce45330
Update abci.go
DeVikingMark f571554
Update abci_test.go
DeVikingMark 0dd9e93
Merge branch 'main' into prevent-gas-type-overflow-in-ABCI-responses
aljo242 5cffdf1
Merge branch 'main' into prevent-gas-type-overflow-in-ABCI-responses
aljo242 53d4506
Update deterministic_test.go
DeVikingMark df07616
Merge branch 'main' into prevent-gas-type-overflow-in-ABCI-responses
DeVikingMark 1ec6aec
Merge branch 'main' into prevent-gas-type-overflow-in-ABCI-responses
aljo242 b305509
test for codecov
DeVikingMark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,22 @@ | ||
| package errors | ||
|
|
||
| import ( | ||
| "math" | ||
|
|
||
| abci "github.com/cometbft/cometbft/abci/types" | ||
|
|
||
| errorsmod "cosmossdk.io/errors" | ||
| ) | ||
|
|
||
| // safeInt64FromUint64 converts uint64 to int64 with overflow checking. | ||
| // If the value is too large to fit in int64, it returns math.MaxInt64. | ||
| func safeInt64FromUint64(val uint64) int64 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like we should just have this as a common function so we don't duplicate across the code |
||
| if val > math.MaxInt64 { | ||
| return math.MaxInt64 | ||
| } | ||
| return int64(val) | ||
| } | ||
|
|
||
| // ResponseCheckTxWithEvents returns an ABCI ResponseCheckTx object with fields filled in | ||
| // from the given error, gas values and events. | ||
| func ResponseCheckTxWithEvents(err error, gw, gu uint64, events []abci.Event, debug bool) *abci.ResponseCheckTx { | ||
|
|
@@ -14,8 +25,8 @@ func ResponseCheckTxWithEvents(err error, gw, gu uint64, events []abci.Event, de | |
| Codespace: space, | ||
| Code: code, | ||
| Log: log, | ||
| GasWanted: int64(gw), | ||
| GasUsed: int64(gu), | ||
| GasWanted: safeInt64FromUint64(gw), | ||
| GasUsed: safeInt64FromUint64(gu), | ||
| Events: events, | ||
| } | ||
| } | ||
|
|
@@ -28,8 +39,8 @@ func ResponseExecTxResultWithEvents(err error, gw, gu uint64, events []abci.Even | |
| Codespace: space, | ||
| Code: code, | ||
| Log: log, | ||
| GasWanted: int64(gw), | ||
| GasUsed: int64(gu), | ||
| GasWanted: safeInt64FromUint64(gw), | ||
| GasUsed: safeInt64FromUint64(gu), | ||
| Events: events, | ||
| } | ||
| } | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package errors | ||
|
|
||
| import ( | ||
| "math" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSafeInt64FromUint64(t *testing.T) { | ||
| cases := map[string]struct { | ||
| gasWanted uint64 | ||
| gasUsed uint64 | ||
| expectedWanted int64 | ||
| expectedUsed int64 | ||
| }{ | ||
| "normal values": { | ||
| gasWanted: 1000, | ||
| gasUsed: 500, | ||
| expectedWanted: 1000, | ||
| expectedUsed: 500, | ||
| }, | ||
| "zero values": { | ||
| gasWanted: 0, | ||
| gasUsed: 0, | ||
| expectedWanted: 0, | ||
| expectedUsed: 0, | ||
| }, | ||
| "max int64 values": { | ||
| gasWanted: math.MaxInt64, | ||
| gasUsed: math.MaxInt64, | ||
| expectedWanted: math.MaxInt64, | ||
| expectedUsed: math.MaxInt64, | ||
| }, | ||
| "overflow case": { | ||
| gasWanted: math.MaxInt64 + 1, | ||
| gasUsed: math.MaxInt64 + 1, | ||
| expectedWanted: math.MaxInt64, | ||
| expectedUsed: math.MaxInt64, | ||
| }, | ||
| "max uint64 values": { | ||
| gasWanted: math.MaxUint64, | ||
| gasUsed: math.MaxUint64, | ||
| expectedWanted: math.MaxInt64, | ||
| expectedUsed: math.MaxInt64, | ||
| }, | ||
| } | ||
|
|
||
| for testName, tc := range cases { | ||
| t.Run(testName, func(t *testing.T) { | ||
| // Test ResponseCheckTxWithEvents | ||
| response := ResponseCheckTxWithEvents(nil, tc.gasWanted, tc.gasUsed, nil, false) | ||
| require.Equal(t, tc.expectedWanted, response.GasWanted, testName+" - CheckTx GasWanted") | ||
| require.Equal(t, tc.expectedUsed, response.GasUsed, testName+" - CheckTx GasUsed") | ||
|
|
||
| // Test ResponseExecTxResultWithEvents | ||
| execResponse := ResponseExecTxResultWithEvents(nil, tc.gasWanted, tc.gasUsed, nil, false) | ||
| require.Equal(t, tc.expectedWanted, execResponse.GasWanted, testName+" - ExecTx GasWanted") | ||
| require.Equal(t, tc.expectedUsed, execResponse.GasUsed, testName+" - ExecTx GasUsed") | ||
| }) | ||
| } | ||
| } |
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.
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.
💡 Verification agent
🧩 Analysis chain
Correctly uses saturating conversion when constructing CheckTxResponse.
Covers the success path; the error path already routes through errors.ResponseCheckTxWithEvents which now also saturates. Nice.
Run to spot any remaining direct casts of gas into int64:
🏁 Script executed:
Length of output: 4134
Refactor ExecTxResult to use saturating conversions for Gas fields
The success path in
deliverTxstill uses directint64casts, which can overflow. Replace these with the existingsafeInt64FromUint64helper to ensure saturation:• File
baseapp/baseapp.go, around line 723This aligns the success path with the CheckTx responses and error paths that already saturate.
🤖 Prompt for AI Agents