fix(governance-token): panic on mint(amount <= 0), matching transfer/burn - #773
Open
kragent66-glitch wants to merge 1 commit into
Open
Conversation
…burn Closes ThinkLikeAFounder#760 mint accepted zero and negative amounts because it had no input validation, while sibling functions transfer and burn both panic on amount <= 0. A non-positive amount would let current_supply.checked_add succeed (silently decreasing supply), corrupt the recipient's balance, and skew delegated power bookkeeping. Add the same if amount <= 0 { panic!("invalid amount") } guard that transfer uses, placed after the admin check and before the supply arithmetic. Cover it with two regression tests for amount=0 and amount=-1; both fail on pre-fix code (verified via sabotage check) and pass with the fix.
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
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.
Fixes #760.
Problem
governance-token::minthad no input validation onamount, while the sibling functionstransferandburnin the same file both panic onamount <= 0. A non-positiveamountwould:current_supply.checked_add(amount)(silently decreasingTotalSupplywhenamount < 0)balance + amountOnly the admin can call
mint, so this isn't externally exploitable by an attacker — but a buggy or compromised admin flow would corrupt global token accounting with no panic to flag it, and the inconsistency withtransfer/burnis itself a real defect.Approach (minimal)
mint, immediately after the admin check:transfer("invalid amount") rather thanburn's ("burn amount must be positive") sincemintis the most generic token-mutating function of the three.test.rs:test_mint_zero_amount_rejectedandtest_mint_negative_amount_rejected, both#[should_panic(expected = "invalid amount")].This is independent of #670, which added a
supply overflowguard viachecked_add.checked_add(-N)on a non-overflowingcurrent_supplystill succeeds and silently decreases supply; the input guard that #760 calls for was not added by that PR.Verification
cargo test -p pulsar-governance-token --lib→ 19 passed; 0 failed, including both new tests.test_mint_negative_amount_rejected,test_mint_zero_amount_rejected); all other mint tests still passed. Restored the guard → all 19 pass.rustc 1.95.0. CI usesdtolnay/rust-toolchain@stableandcargo test --workspace.contracts/governance-token/src/lib.rs(+4 lines: guard) andcontracts/governance-token/src/test.rs(+20 lines: 2 new tests) plus the two matching test snapshot JSON files.Known issues / notes
Cargo.lockis not tracked in this repo (gitignored) so no lockfile churn is included in the PR.