Repository navigation
Conversation
A compact exponent-form amount such as "1e1000000000" is cheap to parse and store, but `#subunits` and `#to_s` expand it into an Integer or String proportional to the exponent, exhausting CPU and memory. `Money#initialize` now raises `ArgumentError` when the value's exponent exceeds `Helpers::MAX_INTEGER_DIGITS` (1000), next to the existing NaN and Infinity checks. Because the check lives in `initialize`, it covers strings, BigDecimals, `from_subunits`, YAML loading and arithmetic results. The bound is deliberately far above any real amount, so values larger than a `DECIMAL(21,3)` column keep working, while the largest accepted value still converts to an Integer under 1 KB. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This branch has not been deployed
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.
Problem
Money.newaccepts compact exponent-form amounts like"1e1000000000". Only NaN and Infinity are rejected. Storing the value is cheap, but#subunits(and every converter) callsto_i, and#to_srenders the full number. Both build an Integer or String sized by the exponent, so a 12-byte input can exhaust CPU and memory.Measured locally on
main:#subunitstime1e10000001e100000001e1000000000The gem's own parsers (
Simple,Fuzzy,Accounting,LocaleAware) already reject or neutralize this input. The exposure is apps that pass an untrusted string, BigDecimal or YAML value straight intoMoney.neworMoney.from_amount.Fix
Money#initializeraisesArgumentErrorwhenvalue.exponent > Helpers::MAX_INTEGER_DIGITS(1000). The check sits next to the NaN and Infinity checks, so it covers every construction path: strings, BigDecimals,from_subunits, YAMLinit_with, and arithmetic results.Why 1000 digits
A 21-digit limit (to match the README's
DECIMAL(21,3)column) was considered. It could break code that works today, such as intermediate arithmetic above 1e21 or usingMoneywith the null currency as a general number container. 1000 digits is far beyond any real amount, and the largest allowed value still converts to an Integer under 1 KB.Tests
magnitude bound: strings, BigDecimals,from_subunits, arithmetic overflow, YAML loading, the largest allowed value, values aboveDECIMAL(21,3), and ordinary exponent strings like"1.5e3".steep checkis clean.Notes
initialize. Whichever lands second needs a small rebase.🤖 Generated with Claude Code