fix(helpers): guard calculateSize against non-finite and negative input - #3194
Open
yfwmaniish wants to merge 1 commit into
Open
fix(helpers): guard calculateSize against non-finite and negative input#3194yfwmaniish wants to merge 1 commit into
yfwmaniish wants to merge 1 commit into
Conversation
Console (appwrite/console)Project ID: Sites (1)
Tip MCP server integration brings LLM superpowers to Claude Desktop and Cursor IDE |
Contributor
Greptile SummaryThe PR hardens
Confidence Score: 5/5The 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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

What
calculateSize()returns broken strings for non-finite, negative, or sub-1-byte input: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 aNaNor negative unit index, sosizes[i]reads back asundefined.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.totalSizecalculateSize(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 guardingNaNinhelpers/numbers(seenumbers.test.ts).Fix
src/lib/helpers/sizeConvertion.ts:'0 Bytes'for any non-finite or<= 0input (extends the existingbytes === 0guard).[0, sizes.length - 1]so sub-1-byte and very large values map to a real unit instead ofundefined.Valid inputs are unchanged.
Tests
Added
src/lib/helpers/sizeConvertion.test.ts(the helper had no test file):NaN,±Infinity,undefined/null, negative, sub-1-byteRan
format(Prettier clean),test:unit(277 passed; the unrelatedoauth2-cimdsuite fails to load withTypeError: Invalid URLon a clean checkout too, and is untouched by this change).