Skip to content

Commit

Permalink
fs: make ReadStream throw TypeError on NaN
Browse files Browse the repository at this point in the history
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: nodejs#19775
Fixes: nodejs#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>
  • Loading branch information
ryzokuken authored and BridgeAR committed May 1, 2018
1 parent 3bdc9ef commit 4699e96
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const binding = process.binding('fs');
const fs = exports;
const { Buffer } = require('buffer');
const errors = require('internal/errors');
const {
ERR_INVALID_ARG_TYPE
} = errors.codes;
const { Readable, Writable } = require('stream');
const EventEmitter = require('events');
const { FSReqWrap } = binding;
Expand Down Expand Up @@ -2020,19 +2023,13 @@ function ReadStream(path, options) {
this.closed = false;

if (this.start !== undefined) {
if (typeof this.start !== 'number') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'start',
'number',
this.start);
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') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'end',
'number',
this.end);
} else if (typeof this.end !== 'number' || Number.isNaN(this.end)) {
throw new ERR_INVALID_ARG_TYPE('end', 'number', this.end);
}

if (this.start > this.end) {
Expand All @@ -2051,6 +2048,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();
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-fs-read-stream-throw-type-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 });

0 comments on commit 4699e96

Please sign in to comment.