From 74903b525a0bdae4ded26a598918dae1cf115a53 Mon Sep 17 00:00:00 2001 From: Ujjwal Sharma Date: Tue, 3 Apr 2018 23:04:20 +0530 Subject: [PATCH] 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. Fixes: https://github.com/nodejs/node/issues/19715 --- lib/fs.js | 6 ++++-- test/parallel/test-fs-read-stream-throw-type-error.js | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 2de5f8dd6e205b..14fc9d00ded120 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -2008,12 +2008,12 @@ function ReadStream(path, options) { this.closed = false; if (this.start !== undefined) { - if (typeof this.start !== 'number') { + if (typeof this.start !== 'number' || Number.isNaN(this.start)) { throw new ERR_INVALID_ARG_TYPE('start', 'number', this.start); } if (this.end === undefined) { this.end = Infinity; - } else if (typeof this.end !== 'number') { + } else if (typeof this.end !== 'number' || Number.isNaN(this.end)) { throw new ERR_INVALID_ARG_TYPE('end', 'number', this.end); } @@ -2030,6 +2030,8 @@ function ReadStream(path, options) { // (That is a semver-major change). if (typeof this.end !== 'number') this.end = Infinity; + else if (Number.isNaN(this.end)) + throw new ERR_INVALID_ARG_TYPE('end', 'number', this.end); if (typeof this.fd !== 'number') this.open(); diff --git a/test/parallel/test-fs-read-stream-throw-type-error.js b/test/parallel/test-fs-read-stream-throw-type-error.js index c2b0d5452c55e3..5cba76fa394636 100644 --- a/test/parallel/test-fs-read-stream-throw-type-error.js +++ b/test/parallel/test-fs-read-stream-throw-type-error.js @@ -3,6 +3,9 @@ const common = require('../common'); const fixtures = require('../common/fixtures'); const fs = require('fs'); +// This test ensures that appropriate TypeError is thrown by createReadStream +// when an argument with invalid type is passed + const example = fixtures.path('x.txt'); // Should not throw. fs.createReadStream(example, undefined); @@ -25,3 +28,8 @@ createReadStreamErr(example, 123); createReadStreamErr(example, 0); createReadStreamErr(example, true); createReadStreamErr(example, false); + +// createReadSteam _should_ throw on NaN +createReadStreamErr(example, { start: NaN }); +createReadStreamErr(example, { end: NaN }); +createReadStreamErr(example, { start: NaN, end: NaN });