Skip to content

Commit 246227f

Browse files
authored
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: nodejs#40349 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 5d4da62 commit 246227f

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
@@ -3245,7 +3245,7 @@ changes:
32453245
* `options` {Object}
32463246
* `buffer` {Buffer|TypedArray|DataView} **Default:** `Buffer.alloc(16384)`
32473247
* `offset` {integer} **Default:** `0`
3248-
* `length` {integer} **Default:** `buffer.byteLength`
3248+
* `length` {integer} **Default:** `buffer.byteLength - offset`
32493249
* `position` {integer|bigint} **Default:** `null`
32503250
* `callback` {Function}
32513251
* `err` {Error}

lib/fs.js

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

0 commit comments

Comments
 (0)