Skip to content

fix(helpers): guard calculateSize against non-finite and negative input - #3194

Open
yfwmaniish wants to merge 1 commit into
appwrite:mainfrom
yfwmaniish:fix-calculatesize-invalid-input
Open

fix(helpers): guard calculateSize against non-finite and negative input#3194
yfwmaniish wants to merge 1 commit into
appwrite:mainfrom
yfwmaniish:fix-calculatesize-invalid-input

Conversation

@yfwmaniish

Copy link
Copy Markdown

What

calculateSize() returns broken strings for non-finite, negative, or sub-1-byte input:

Input Before After
NaN "NaN undefined" "0 Bytes"
undefined / null "NaN undefined" "0 Bytes"
Infinity "NaN undefined" "0 Bytes"
-5 "NaN undefined" "0 Bytes"
0.5 "500 undefined" "0.5 Bytes"

The cause: Math.log(bytes) of a non-finite/negative value yields a NaN or negative unit index, so sizes[i] reads back as undefined.

Why it matters

Several call sites pass API size fields that can be nullish (e.g. a deployment still building or failed):

  • calculateSize(deployment.buildSize), deployment.sourceSize, deployment.totalSize
  • calculateSize(file.sizeOriginal), calculateSize(backup.size)

One deployments table already works around exactly this with calculateSize(deployment?.totalSize ?? 0) — but the sibling call sites don't, so users can see "NaN undefined" in the UI. Handling this gracefully also matches the existing convention of guarding NaN in helpers/numbers (see numbers.test.ts).

Fix

src/lib/helpers/sizeConvertion.ts:

  • Return '0 Bytes' for any non-finite or <= 0 input (extends the existing bytes === 0 guard).
  • Clamp the unit index to [0, sizes.length - 1] so sub-1-byte and very large values map to a real unit instead of undefined.

Valid inputs are unchanged.

Tests

Added src/lib/helpers/sizeConvertion.test.ts (the helper had no test file):

  • normal scaling — base 1000 and 1024, and the decimals argument
  • regression cases — NaN, ±Infinity, undefined/null, negative, sub-1-byte
TZ=EST vitest run src/lib/helpers/sizeConvertion.test.ts
→ Test Files 1 passed (1) · Tests 9 passed (9)

Ran format (Prettier clean), test:unit (277 passed; the unrelated oauth2-cimd suite fails to load with TypeError: Invalid URL on a clean checkout too, and is untouched by this change).

Copilot AI lite review requested due to automatic review settings September 5, 2026 07:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@appwrite

appwrite Bot commented Sep 5, 2026

Copy link
Copy Markdown

Console (appwrite/console)

Project ID: 688b7bf400350cbd60e9

Sites (1)
Site Status Logs Preview QR
 console-stage
688b7cf6003b1842c9dc
Queued Queued Authorize Preview URL QR Code

Tip

MCP server integration brings LLM superpowers to Claude Desktop and Cursor IDE

@greptile-apps

greptile-apps Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR hardens calculateSize against invalid and extreme numeric inputs and adds focused regression coverage.

  • Returns 0 Bytes for non-finite and non-positive values.
  • Clamps unit selection so sub-byte and very large finite values always use a defined unit.
  • Tests ordinary scaling, alternate bases, decimal precision, nullish runtime values, and numeric edge cases.

Confidence Score: 5/5

The PR appears safe to merge, with no actionable correctness, security, or repository-rule issues identified.

The new guard covers the realistic nullish API-size path, the unit-index clamp remains valid for supported bases, and the tests exercise both existing behavior and the reported regressions.

Important Files Changed

Filename Overview
src/lib/helpers/sizeConvertion.ts Safely handles invalid values and constrains calculated unit indexes without changing ordinary size formatting.
src/lib/helpers/sizeConvertion.test.ts Adds comprehensive tests for normal scaling and the invalid, nullish, negative, and sub-byte regression cases.

Reviews (1): Last reviewed commit: "fix(helpers): guard calculateSize agains..." | Re-trigger Greptile

calculateSize() rendered "NaN undefined" / "500 undefined" for non-finite,
nullish, or negative sizes because Math.log() produced an out-of-range unit
index into the sizes array. Return "0 Bytes" for non-finite/<=0 input and
clamp the unit index to a valid range. Valid inputs are unchanged.

Adds sizeConvertion.test.ts (the helper had no tests) covering normal scaling
plus the NaN/Infinity/undefined/null/negative/sub-1-byte regression cases.
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.

2 participants