Skip to content

Commit 10ca3bb

Browse files
wbtbengl
authored andcommitted
fs: fix default length parameter for fs.read
Currently, specifying an `offset` without a `length` throws an `ERR_OUT_OF_RANGE` error. This commit provides a more sensible default. This change should only affect cases where no length is specified and a nonzero offset is, which are currently throwing errors. PR-URL: #40349 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent dcb51b0 commit 10ca3bb

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

doc/api/fs.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ added:
392392
* `offset` {integer} The location in the buffer at which to start filling.
393393
**Default:** `0`
394394
* `length` {integer} The number of bytes to read. **Default:**
395-
`buffer.byteLength`
395+
`buffer.byteLength - offset`
396396
* `position` {integer} The location where to begin reading data from the
397397
file. If `null`, data will be read from the current file position, and
398398
the position will be updated. If `position` is an integer, the current
@@ -3119,7 +3119,7 @@ changes:
31193119
* `options` {Object}
31203120
* `buffer` {Buffer|TypedArray|DataView} **Default:** `Buffer.alloc(16384)`
31213121
* `offset` {integer} **Default:** `0`
3122-
* `length` {integer} **Default:** `buffer.byteLength`
3122+
* `length` {integer} **Default:** `buffer.byteLength - offset`
31233123
* `position` {integer|bigint} **Default:** `null`
31243124
* `callback` {Function}
31253125
* `err` {Error}

lib/fs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ function read(fd, buffer, offset, length, position, callback) {
621621
({
622622
buffer = Buffer.alloc(16384),
623623
offset = 0,
624-
length = buffer.byteLength,
624+
length = buffer.byteLength - offset,
625625
position
626626
} = options);
627627
}

0 commit comments

Comments
 (0)