diff --git a/src/bun.js/bindings/JSBuffer.cpp b/src/bun.js/bindings/JSBuffer.cpp index 3164d12779..1cda87a575 100644 --- a/src/bun.js/bindings/JSBuffer.cpp +++ b/src/bun.js/bindings/JSBuffer.cpp @@ -1957,7 +1957,7 @@ static JSC::EncodedJSValue jsBufferPrototypeFunction_sliceBody(JSC::JSGlobalObje if (castedThis->isResizableOrGrowableShared()) { auto* subclassStructure = globalObject->JSResizableOrGrowableSharedBufferSubclassStructure(); - auto* uint8Array = JSC::JSUint8Array::create(lexicalGlobalObject, subclassStructure, WTF::move(buffer), byteOffset + startOffset, std::nullopt); + auto* uint8Array = JSC::JSUint8Array::create(lexicalGlobalObject, subclassStructure, WTF::move(buffer), byteOffset + startOffset, newLength); RETURN_IF_EXCEPTION(throwScope, {}); if (!uint8Array) [[unlikely]] { throwOutOfMemoryError(globalObject, throwScope); diff --git a/test/js/node/buffer.test.js b/test/js/node/buffer.test.js index 9e8ef4d43d..6320a5069f 100644 --- a/test/js/node/buffer.test.js +++ b/test/js/node/buffer.test.js @@ -930,6 +930,25 @@ for (let withOverridenBufferWrite of [false, true]) { expect(() => buf.subarray(0, 5)).toThrow(TypeError); }); + it("slice() on resizable ArrayBuffer returns fixed-length view", () => { + const rab = new ArrayBuffer(10, { maxByteLength: 20 }); + const buf = Buffer.from(rab); + buf[0] = 1; + buf[1] = 2; + buf[2] = 3; + buf[3] = 4; + buf[4] = 5; + + const sliced = buf.slice(0, 5); + expect(sliced.length).toBe(5); + expect(sliced[0]).toBe(1); + expect(sliced[4]).toBe(5); + + // Growing the buffer should NOT change the slice length + rab.resize(20); + expect(sliced.length).toBe(5); + }); + function forEachUnicode(label, test) { ["ucs2", "ucs-2", "utf16le", "utf-16le"].forEach(encoding => it(`${label} (${encoding})`, test.bind(null, encoding)),