From 10ca3bb92251aebcbc8a0b89028b00141f448785 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 06fccffc32aedd..27fb50c5018b4e 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 @@ -3119,7 +3119,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 b55d428d8dbc90..594a1c0b220af5 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); }