fix: reject malformed numeric CLI arguments instead of crashing or coercing to 0 - #24
Open
ayushsingh82 wants to merge 1 commit into
Open
fix: reject malformed numeric CLI arguments instead of crashing or coercing to 0#24ayushsingh82 wants to merge 1 commit into
ayushsingh82 wants to merge 1 commit into
Conversation
…ercing to 0
asBigInt (deposit ids, intent amounts, maxCost, durations) threw a raw
SyntaxError/RangeError on a hex, fractional, or non-numeric string and on a
non-integer number, and silently returned 0n for "" — so a blank --deposit-id
resolved to deposit 0. It now returns a VALIDATION_ERROR and accepts only a
plain decimal integer string or a safe-integer number.
amountToUnits rounded a sub-base-unit amount to 0n (turning a transfer or
approve into a no-op) and handed exponential notation ('1e-7', '1e+21')
straight to parseUnits, which throws. Both are now rejected with a clear error.
Contributor
Author
|
@ADWilkinson small correctness-only fix — no behaviour change on valid input, just turns two silent-failure paths (blank |
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.
While looking at how numeric CLI arguments get parsed, I hit two spots in
src/utils/that fail in ways the caller can't see coming.asBigInthandles deposit ids, intent amounts,maxCostand durations across the deposit / intent / stake / vault / delegate commands. Pass it a hex, fractional, or non-numeric string like"0x10","1.5"or"abc"and it throws a bareSyntaxError; a non-integer number throws a bareRangeError. Neither goes through theVALIDATION_ERRORenvelope the rest of the CLI uses, so the user just gets a stack trace. Worse,BigInt("")is0n, so a blank--deposit-idquietly becomes deposit0with no error at all.amountToUnitshas a similar sharp edge. An amount smaller than one base unit gets rounded down to0nbyparseUnits, so a transfer or approve silently turns into a no-op. AndNumber#toStringswitches to exponential notation for very small or very large magnitudes (1e-7,1e+21), whichparseUnitsrejects outright.The change keeps valid input behaving exactly as before:
asBigIntnow accepts only a plain decimal-integer string or a safe-integer number. Anything else returns aVALIDATION_ERRORthat names the field and shows the value it choked on. Thebigintpassthrough is untouched.amountToUnitsrejects a sub-base-unit amount and an implausibly large one with a clear message, and formats throughtoFixed(decimals)soparseUnitsalways sees plain decimal notation.Checked:
asBigInt—"","0x10","1.5","abc",1.5,NaNand2**53are each rejected with aVALIDATION_ERROR;42,"42"and" -7 "still parse fine.amountToUnits—0.0000001,"0.0000004"(would have rounded to0n) and1e21are each rejected;0.000001still returns1n.npm run checkis green: lint, typecheck, 234 tests, and coverage withparsing.tsandvalidation.tsat 100% lines.