mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 10:58:56 +00:00
## Summary Follow-up to #26819 ([review comment](https://github.com/oven-sh/bun/pull/26819#discussion_r2781484939)). Fixes `Buffer.slice()` / `Buffer.subarray()` on resizable `ArrayBuffer` / growable `SharedArrayBuffer` to return a **fixed-length view** instead of a length-tracking view. ## Problem The resizable/growable branch was passing `std::nullopt` to `JSUint8Array::create()`, which creates a length-tracking view. When the underlying buffer grows, the sliced view's length would incorrectly expand: ```js const rab = new ArrayBuffer(10, { maxByteLength: 20 }); const buf = Buffer.from(rab); const sliced = buf.slice(0, 5); sliced.length; // 5 rab.resize(20); sliced.length; // was 10 (wrong), now 5 (correct) ``` Node.js specifies that `Buffer.slice()` always returns a fixed-length view (verified on Node.js v22). ## Fix Replace `std::nullopt` with `newLength` in the `isResizableOrGrowableShared()` branch of `jsBufferPrototypeFunction_sliceBody`. ## Test Added a regression test that creates a `Buffer` from a resizable `ArrayBuffer`, slices it, resizes the buffer, and verifies the slice length doesn't change. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>