Skip to content

fix division by zero panics and input validation - #46

Open
YashejShah wants to merge 4 commits into
razorpay:masterfrom
YashejShah:fix/panic-fixes-and-input-validation
Open

fix division by zero panics and input validation#46
YashejShah wants to merge 4 commits into
razorpay:masterfrom
YashejShah:fix/panic-fixes-and-input-validation

Conversation

@YashejShah

Copy link
Copy Markdown

what's wrong

was going through the code and found several division-by-zero scenarios that cause panics in production. pretty concerning for a financial calculations library.

panics fixed

  • Fv() panics when rate is zero — added zero-rate branch (same pattern Pmt() already uses)
  • Pv() same issue
  • Nper() divides by rate without checking, also can produce NaN from math.Log of negative values
  • Flat.GetPrincipal() panics when periods is 0
  • Frequency.Value() returns 0 for invalid enum values, causing downstream division by zero
  • getRateRatio() newton-raphson iteration can hit zero denominators

other fixes

  • fixed isAlmostEqual test helper — was computing ||a| - |b|| instead of |a - b|, which masks bugs with negative values
  • fixed sanityCheckUpdate rounding logic for negative diffs
  • added Config.Validate() for upfront input validation
  • replaced magic 0 with paymentperiod.ENDING in tests
  • added test cases for all zero-rate scenarios
  • updated go.mod version

all existing tests pass, added new ones for the edge cases.

- Fv() panics when rate is zero, added zero-rate branch matching
  the pattern Pmt() already uses
- Pv() same issue as Fv()
- Nper() divides by rate without checking, also can produce NaN
  from math.Log of negative values
- Flat.GetPrincipal() and GetPayment() panic when periods is 0
- Frequency.Value() returns 0 for invalid enum values, causing
  downstream division by zero in getInterestRatePerPeriodInDecimal
- getRateRatio() newton-raphson iteration can hit zero denominators
- fixed isAlmostEqual computing ||a| - |b|| instead of |a - b|,
  which was masking bugs when comparing negative values
- fixed sanityCheckUpdate using wrong diff formula that broke
  on negative rounding differences
- replaced implicit 0 with paymentperiod.ENDING in test cases
- added test cases for all zero-rate edge cases
- added Config.Validate() for upfront sanity checks on amount
  and interest before processing
- added tests for validation edge cases
- bumped go.mod from 1.15 to 1.21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant