From da87c731b897d5792fa20d6ad08797ccd7eab55d Mon Sep 17 00:00:00 2001 From: wbt Date: Sat, 19 Feb 2022 12:22:46 -0500 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/40349 Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel --- doc/api/fs.md | 4 ++-- lib/fs.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 17d15c51c5454d..07b52b17ed203c 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -392,7 +392,7 @@ added: * `offset` {integer} The location in the buffer at which to start filling. **Default:** `0` * `length` {integer} The number of bytes to read. **Default:** - `buffer.byteLength` + `buffer.byteLength - offset` * `position` {integer} The location where to begin reading data from the file. If `null`, data will be read from the current file position, and the position will be updated. If `position` is an integer, the current @@ -3060,7 +3060,7 @@ changes: * `options` {Object} * `buffer` {Buffer|TypedArray|DataView} **Default:** `Buffer.alloc(16384)` * `offset` {integer} **Default:** `0` - * `length` {integer} **Default:** `buffer.byteLength` + * `length` {integer} **Default:** `buffer.byteLength - offset` * `position` {integer|bigint} **Default:** `null` * `callback` {Function} * `err` {Error} diff --git a/lib/fs.js b/lib/fs.js index 75349f5b1f6f0e..9fb7473d0fea7a 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -621,7 +621,7 @@ function read(fd, buffer, offset, length, position, callback) { ({ buffer = Buffer.alloc(16384), offset = 0, - length = buffer.byteLength, + length = buffer.byteLength - offset, position } = options); }