From 822ee634ea9752ae0d43e550e647c58c67301508 Mon Sep 17 00:00:00 2001 From: Koji AGAWA Date: Sun, 12 May 2024 15:31:02 +0900 Subject: [PATCH] fix: index out of bound at readSync when offset is non-zero --- src/file.ts | 2 +- tests/fs/read.test.ts | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/file.ts b/src/file.ts index d04dfc75a..e78b8ff07 100644 --- a/src/file.ts +++ b/src/file.ts @@ -620,7 +620,7 @@ export class PreloadFile extends File { // No copy/read. Return immediatly for better performance return bytesRead; } - new Uint8Array(buffer.buffer, 0, length).set(this._buffer.slice(position, end), offset); + new Uint8Array(buffer.buffer, offset, length).set(this._buffer.slice(position, end)); return bytesRead; } diff --git a/tests/fs/read.test.ts b/tests/fs/read.test.ts index 0f41c45c4..3f27c5298 100644 --- a/tests/fs/read.test.ts +++ b/tests/fs/read.test.ts @@ -53,4 +53,13 @@ describe('read buffer', () => { expect(bufferSync.toString()).toBe(expected); expect(bytesRead).toBe(expected.length); }); + + test('read file synchronously to non-zero offset', async () => { + const fd = fs.openSync(filepath, 'r'); + const buffer = Buffer.alloc(expected.length + 10); + const bytesRead = fs.readSync(fd, buffer, 10, expected.length, 0); + + expect(buffer.subarray(10, buffer.length).toString()).toBe(expected); + expect(bytesRead).toBe(expected.length); + }); });