Skip to content

Fix division by zero in currency conversion functions - #1229

Open
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/currency-division-by-zero
Open

Fix division by zero in currency conversion functions#1229
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/currency-division-by-zero

Conversation

@pavankumar-vh

Copy link
Copy Markdown

Overview

Fix a potential division by zero bug in currency conversion functions.

Bug Description

Both convertCreditsToUsdCents and convertStripeGrantAmountToCredits would produce Infinity or NaN when centsPerCredit is 0 or negative:

  • convertCreditsToUsdCents: Would return 0 * Infinity = NaN or Infinity * credits = Infinity
  • convertStripeGrantAmountToCredits: Would return amountInCents / 0 = Infinity or NaN

Fix

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 protection

Scope

This change only touches common/ which is an approved contribution area per the Contributing Guide.

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).
@codebuff-team

Copy link
Copy Markdown
Contributor

Thanks for digging into common/src/util/currency.ts — division-by-zero and negative-rate guards are a sensible thing to think about in conversion utilities.

The concern with this specific fix is the failure mode: silently returning 0 credits/cents when centsPerCredit is invalid is dangerous in a money-conversion path. If centsPerCredit is ever 0 or negative due to a misconfiguration or bad Stripe price setup, you'd rather that be loud (thrown error, logged warning, or an assertion) than silently converted into "zero cost" or "zero credits granted" — either could quietly break billing in either direction (undercharging or granting no credits) without anyone noticing. Silent-zero is arguably worse than the current NaN/Infinity behavior, which at least tends to produce visibly broken output that gets caught in QA rather than a plausible-looking but wrong 0.

A few things I'd want addressed before this is portable:

  • Confirm how centsPerCredit is actually produced/validated upstream — is <=0 even reachable in practice, or is this purely defensive? If it's genuinely reachable, that call site probably needs the real fix.
  • Consider throwing (throw new Error(...)) instead of returning 0, so callers can't silently proceed with bad math.
  • Add unit tests for both functions (zero, negative, and normal rates) since the PR itself notes none exist — for a currency function this is the first thing a maintainer will ask for.

Good instinct, but the chosen fallback behavior needs more thought given this touches money calculations.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:needs-work Right idea, not mergeable as written labels Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:needs-work Right idea, not mergeable as written

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants