Fix division by zero in currency conversion functions - #1229
Fix division by zero in currency conversion functions#1229pavankumar-vh wants to merge 1 commit into
Conversation
Both convertCreditsToUsdCents and convertStripeGrantAmountToCredits would produce Infinity or NaN when centsPerCredit is 0 or negative. Added guards to return 0 in these cases, which is the safest behavior (no conversion possible when the rate is invalid).
|
Thanks for digging into The concern with this specific fix is the failure mode: silently returning A few things I'd want addressed before this is portable:
Good instinct, but the chosen fallback behavior needs more thought given this touches money calculations. |
Overview
Fix a potential division by zero bug in currency conversion functions.
Bug Description
Both
convertCreditsToUsdCentsandconvertStripeGrantAmountToCreditswould produceInfinityorNaNwhencentsPerCreditis 0 or negative:convertCreditsToUsdCents: Would return0 * Infinity = NaNorInfinity * credits = InfinityconvertStripeGrantAmountToCredits: Would returnamountInCents / 0 = InfinityorNaNFix
Added guards to return 0 when
centsPerCredit <= 0, which is the safest behavior (no conversion possible when the rate is invalid).Testing
No existing tests for these functions, but the fix prevents undefined behavior.
Files Changed
common/src/util/currency.ts- Added division by zero protectionScope
This change only touches
common/which is an approved contribution area per the Contributing Guide.