Fix Blob.slice() for negative start and inverted ranges#57374
Open
durvesh1992 wants to merge 1 commit into
Open
Fix Blob.slice() for negative start and inverted ranges#57374durvesh1992 wants to merge 1 commit into
durvesh1992 wants to merge 1 commit into
Conversation
javache
requested changes
Jun 30, 2026
javache
left a comment
Contributor
There was a problem hiding this comment.
Please revert the changes to no-deep-imports
Blob.slice() handled a negative `end` (relative to the end of the blob) but not a negative `start`, so `blob.slice(-100)` produced a negative offset and an oversized blob instead of slicing the last 100 bytes. It also computed `size = end - start` without clamping, so calling `slice(start, end)` with `end` before `start` produced a negative size. Normalize a negative start relative to the blob size (matching the existing end handling) and clamp the resulting size to a minimum of 0, per the W3C Blob.slice() semantics. Add tests for negative start, negative end, and end-precedes-start.
3e8ace9 to
9d2aa9c
Compare
Author
|
Thanks for the review! Done — I've rebased this branch to drop the |
|
@javache has imported this pull request. If you are a Meta employee, you can view this in D110173869. |
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.
Summary
Blob.slice(start, end)is modeled on the W3C Blob API, where bothstartandendmay be negative (relative to the end of the blob) and anendthat precedesstartyields an empty blob.The current implementation handles a negative
endbut not a negativestart:So:
blob.slice(-100)setsoffsetto a negative value and makessizelarger than the blob, instead of slicing the last 100 bytes.blob.slice(200, 100)(end before start) produces a negative size.Fix
startrelative to the blob size (Math.max(this.size + start, 0)), mirroring the existingendhandling.Math.max(end - start, 0)so an inverted range yields an empty blob.Test plan
Added tests to
Libraries/Blob/__tests__/Blob-test.jsfor negative start, negative end, and end-precedes-start. The negative-start and inverted-range cases fail before this change (negative offset / negative size) and pass after; existing slice tests and the rest of the suite still pass (8/8). ESLint clean on the changed files.Changelog:
[General] [Fixed] - Fix
Blob.slice()for negative start offsets and inverted ranges