Skip to content

Commit 38a6929

Browse files
ryzokukenlpinca
authored andcommitted
fs: make ReadStream throw TypeError on NaN
Make ReadStream (and thus createReadStream) throw a TypeError signalling towards an invalid argument type when either options.start or options.end (or obviously, both) are set to NaN. Also add regression tests for the same. PR-URL: #19775 Fixes: #19715 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 496d602 commit 38a6929

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/fs.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,12 +2009,12 @@ function ReadStream(path, options) {
20092009
this.closed = false;
20102010

20112011
if (this.start !== undefined) {
2012-
if (typeof this.start !== 'number') {
2012+
if (typeof this.start !== 'number' || Number.isNaN(this.start)) {
20132013
throw new ERR_INVALID_ARG_TYPE('start', 'number', this.start);
20142014
}
20152015
if (this.end === undefined) {
20162016
this.end = Infinity;
2017-
} else if (typeof this.end !== 'number') {
2017+
} else if (typeof this.end !== 'number' || Number.isNaN(this.end)) {
20182018
throw new ERR_INVALID_ARG_TYPE('end', 'number', this.end);
20192019
}
20202020

@@ -2031,6 +2031,8 @@ function ReadStream(path, options) {
20312031
// (That is a semver-major change).
20322032
if (typeof this.end !== 'number')
20332033
this.end = Infinity;
2034+
else if (Number.isNaN(this.end))
2035+
throw new ERR_INVALID_ARG_TYPE('end', 'number', this.end);
20342036

20352037
if (typeof this.fd !== 'number')
20362038
this.open();

test/parallel/test-fs-read-stream-throw-type-error.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ const common = require('../common');
33
const fixtures = require('../common/fixtures');
44
const fs = require('fs');
55

6+
// This test ensures that appropriate TypeError is thrown by createReadStream
7+
// when an argument with invalid type is passed
8+
69
const example = fixtures.path('x.txt');
710
// Should not throw.
811
fs.createReadStream(example, undefined);
@@ -25,3 +28,8 @@ createReadStreamErr(example, 123);
2528
createReadStreamErr(example, 0);
2629
createReadStreamErr(example, true);
2730
createReadStreamErr(example, false);
31+
32+
// createReadSteam _should_ throw on NaN
33+
createReadStreamErr(example, { start: NaN });
34+
createReadStreamErr(example, { end: NaN });
35+
createReadStreamErr(example, { start: NaN, end: NaN });

0 commit comments

Comments
 (0)