Skip to content

Commit

Permalink
Merge pull request #56 from atty303/fix-file-read-sync
Browse files Browse the repository at this point in the history
fix: index out of bound at readSync when offset is non-zero
  • Loading branch information
james-pre authored May 12, 2024
2 parents 9e588af + 822ee63 commit ca98b1e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ export class PreloadFile<FS extends FileSystem> 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;
}

Expand Down
9 changes: 9 additions & 0 deletions tests/fs/read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit ca98b1e

Please sign in to comment.