-
Notifications
You must be signed in to change notification settings - Fork 1.7k
chore: add safety guard for negative cycle index #6527
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b733257
chore: add safety guard for negative cycle index
operagxsasha c9e2299
chore: move guard to method level
operagxsasha 3e334c7
chore: fix return type for beginCycle guard
operagxsasha dad308a
style: fix checkstyle LineLength in ShieldedReceiveTest
operagxsasha 5b426c6
Merge branch 'develop' into patch-3
operagxsasha 8077531
chore: remove redundant beginCycle guard
operagxsasha 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -214,6 +214,10 @@ private long computeReward(long beginCycle, long endCycle, AccountCapsule accoun | |
| } | ||
| if (beginCycle < endCycle) { | ||
| for (Pair<byte[], Long> vote : srAddresses) { | ||
| if (beginCycle == 0) { | ||
| continue; | ||
|
Contributor
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. BTW, the indentation looks off this line. Please fix to match project style (4 spaces). |
||
| } | ||
|
|
||
| byte[] srAddress = vote.getKey(); | ||
| BigInteger beginVi = delegationStore.getWitnessVi(beginCycle - 1, srAddress); | ||
| BigInteger endVi = delegationStore.getWitnessVi(endCycle - 1, srAddress); | ||
|
|
||
Oops, something went wrong.
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.
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.
Correct, this prevents beginCycle from being 0
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.
I agree with @bladehan1's observation. After tracing the call sites of
computeReward(beginCycle, endCycle, accountCapsule):withdrawReward()(line 94):beginCycle = delegationStore.getBeginCycle(address), and the delegation cycle starts from 1.beginCyclehas already been validated (beginCycle > currentCyclereturns early at line 98-100), and may have been incremented (beginCycle += 1at line 116).queryReward()(line 142): same pattern —beginCycleis loaded from store and validated before use.So
beginCycle == 0cannot occur in normal execution flow. Additionally, even as a defensive check, usingcontinueinside the vote loop is incorrect — it skips individual votes rather than handling the invalid cycle at the method level. A proper guard would be placed before the for-loop, e.g.,if (beginCycle <= 0) return 0;.Also noting that the indentation of the added code is inconsistent with the surrounding code style.