Commit b29dc96
Fix Blob.slice() for negative start and inverted ranges (#57374)
Summary:
`Blob.slice(start, end)` is modeled on the [W3C Blob API](https://developer.mozilla.org/en-US/docs/Web/API/Blob/slice), where both `start` and `end` may be negative (relative to the end of the blob) and an `end` that precedes `start` yields an empty blob.
The current implementation handles a negative `end` but not a negative `start`:
```js
if (typeof start === 'number') {
if (start > size) { start = size; } // only the upper bound is checked
offset += start;
size -= start;
if (typeof end === 'number') {
if (end < 0) { end = this.size + end; } // negative end IS handled
if (end > this.size) { end = this.size; }
size = end - start; // not clamped to >= 0
}
}
```
So:
- `blob.slice(-100)` sets `offset` to a **negative** value and makes `size` *larger* than the blob, instead of slicing the last 100 bytes.
- `blob.slice(200, 100)` (end before start) produces a **negative** size.
## Fix
- Normalize a negative `start` relative to the blob size (`Math.max(this.size + start, 0)`), mirroring the existing `end` handling.
- Clamp the resulting size with `Math.max(end - start, 0)` so an inverted range yields an empty blob.
Changelog: [General][Fixed] Fix Blob.slice() for negative start and inverted ranges
Pull Request resolved: #57374
Test Plan:
Added tests to `Libraries/Blob/__tests__/Blob-test.js` for 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
Reviewed By: cortinico
Differential Revision: D110173869
Pulled By: javache
fbshipit-source-id: 9f15bfa9c7d8fbba3f7f3b2473c3b39c9cf9d2fe1 parent f500f42 commit b29dc96
2 files changed
Lines changed: 39 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
89 | 94 | | |
90 | 95 | | |
91 | 96 | | |
| |||
102 | 107 | | |
103 | 108 | | |
104 | 109 | | |
105 | | - | |
| 110 | + | |
| 111 | + | |
106 | 112 | | |
107 | 113 | | |
108 | 114 | | |
| |||
Lines changed: 32 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
84 | 116 | | |
85 | 117 | | |
86 | 118 | | |
| |||
0 commit comments