fs: harden validation of start option in createWriteStream · nodejs/node@70f4f08 (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -1470,9 +1470,11 @@ returned by this method has a default `highWaterMark` of 64 kb.
1470 1470
1471 1471 `options` can include `start` and `end` values to read a range of bytes from
1472 1472 the file instead of the entire file. Both `start` and `end` are inclusive and
1473 -start counting at 0. If `fd` is specified and `start` is omitted or `undefined`,
1474 -`fs.createReadStream()` reads sequentially from the current file position.
1475 -The `encoding` can be any one of those accepted by [`Buffer`][].
1473 +start counting at 0, allowed values are in the
1474 +[0, [`Number.MAX_SAFE_INTEGER`][]] range. If `fd` is specified and `start` is
1475 +omitted or `undefined`, `fs.createReadStream()` reads sequentially from the
1476 +current file position. The `encoding` can be any one of those accepted by
1477 +[`Buffer`][].
1476 1478
1477 1479 If `fd` is specified, `ReadStream` will ignore the `path` argument and will use
1478 1480 the specified file descriptor. This means that no `'open'` event will be
@@ -1548,7 +1550,8 @@ changes:
1548 1550 * Returns: {fs.WriteStream} See [Writable Stream][].
1549 1551
1550 1552 `options` may also include a `start` option to allow writing data at
1551 -some position past the beginning of the file. Modifying a file rather
1553 +some position past the beginning of the file, allowed values are in the
1554 +[0, [`Number.MAX_SAFE_INTEGER`][]] range. Modifying a file rather
1552 1555 than replacing it may require a `flags` mode of `r+` rather than the
1553 1556 default mode `w`. The `encoding` can be any one of those accepted by
1554 1557 [`Buffer`][].
@@ -4953,3 +4956,4 @@ the file contents.
4953 4956 [chcp]: https://ss64.com/nt/chcp.html
4954 4957 [inode]: https://en.wikipedia.org/wiki/Inode
4955 4958 [support of file system `flags`]: #fs_file_system_flags
4959 +[`Number.MAX_SAFE_INTEGER`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/Number/MAX\_SAFE\_INTEGER
Original file line number Diff line number Diff line change
@@ -243,13 +243,7 @@ function WriteStream(path, options) {
243 243 this.closed = false;
244 244
245 245 if (this.start !== undefined) {
246 -if (typeof this.start !== 'number') {
247 -throw new ERR_INVALID_ARG_TYPE('start', 'number', this.start);
248 -}
249 -if (this.start < 0) {
250 -const errVal = `{start: ${this.start}}`;
251 -throw new ERR_OUT_OF_RANGE('start', '>= 0', errVal);
252 -}
246 +checkPosition(this.start, 'start');
253 247
254 248 this.pos = this.start;
255 249 }
Original file line number Diff line number Diff line change
@@ -161,6 +161,7 @@ function run_test_3() {
161 161 assert.strictEqual(fileData, fileDataExpected_3);
162 162
163 163 run_test_4();
164 +run_test_5();
164 165 });
165 166
166 167 file.on('error', function(err) {
@@ -184,7 +185,22 @@ const run_test_4 = common.mustCall(function() {
184 185 const err = {
185 186 code: 'ERR_OUT_OF_RANGE',
186 187 message: 'The value of "start" is out of range. ' +
187 -'It must be >= 0. Received {start: -5}',
188 +'It must be >= 0 and <= 2 ** 53 - 1. Received -5',
189 +type: RangeError
190 +};
191 +common.expectsError(fn, err);
192 +});
193 +
194 +
195 +const run_test_5 = common.mustCall(function() {
196 +// Error: start must be <= 2 ** 53 - 1
197 +const fn = () => {
198 +fs.createWriteStream(filepath, { start: 2 ** 53, flags: 'r+' });
199 +};
200 +const err = {
201 +code: 'ERR_OUT_OF_RANGE',
202 +message: 'The value of "start" is out of range. ' +
203 +'It must be >= 0 and <= 2 ** 53 - 1. Received 9007199254740992',
188 204 type: RangeError
189 205 };
190 206 common.expectsError(fn, err);