From 543a5a6eb63e2f23814e669eafdacbdea297bfee Mon Sep 17 00:00:00 2001 From: Pavan Kumar VH Date: Thu, 3 Sep 2026 10:32:03 +0530 Subject: [PATCH] Fix division by zero in currency conversion functions 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). --- common/src/util/currency.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/common/src/util/currency.ts b/common/src/util/currency.ts index b3499c01e5..efa72aaf82 100644 --- a/common/src/util/currency.ts +++ b/common/src/util/currency.ts @@ -8,6 +8,7 @@ export function convertCreditsToUsdCents( credits: number, centsPerCredit: number, ): number { + if (centsPerCredit <= 0) return 0 return Math.ceil(credits * centsPerCredit) } @@ -21,5 +22,6 @@ export function convertStripeGrantAmountToCredits( amountInCents: number, centsPerCredit: number, ): number { + if (centsPerCredit <= 0) return 0 return Math.floor(amountInCents / centsPerCredit) }