-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
fs: make ReadStream throw error on NaN #19732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
473dd3d
83f478e
a4077ea
1ff2f51
23cc8c0
0ae768d
163f911
c49c543
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2008,13 +2008,13 @@ function ReadStream(path, options) { | |
this.closed = false; | ||
|
||
if (this.start !== undefined) { | ||
if (typeof this.start !== 'number') { | ||
throw new ERR_INVALID_ARG_TYPE('start', 'number', this.start); | ||
if (typeof this.start !== 'number' || !Number.isInteger(this.start)) { | ||
throw new ERR_INVALID_ARG_TYPE('start', 'integer', this.start); | ||
|
||
} | ||
if (this.end === undefined) { | ||
this.end = Infinity; | ||
} else if (typeof this.end !== 'number') { | ||
throw new ERR_INVALID_ARG_TYPE('end', 'number', this.end); | ||
} else if (typeof this.end !== 'number' || !Number.isInteger(this.end)) { | ||
throw new ERR_INVALID_ARG_TYPE('end', 'integer', this.end); | ||
|
||
} | ||
|
||
if (this.start > 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.isInteger(this.end)) | ||
throw new ERR_INVALID_ARG_TYPE('end', 'integer', this.end); | ||
|
||
if (typeof this.fd !== 'number') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not be included in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it should not. |
||
this.open(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
|
||
// This test ensures that fs.createReadStream throws a TypeError when invalid | ||
// arguments are passed to it. | ||
|
||
const fs = require('fs'); | ||
|
||
const example = fixtures.path('x.txt'); | ||
|
@@ -25,3 +29,8 @@ createReadStreamErr(example, 123); | |
createReadStreamErr(example, 0); | ||
createReadStreamErr(example, true); | ||
createReadStreamErr(example, false); | ||
|
||
// Should also throw on NaN (for https://github.com/nodejs/node/pull/19732) | ||
createReadStreamErr(example, { start: NaN }); | ||
|
||
createReadStreamErr(example, { end: NaN }); | ||
createReadStreamErr(example, { start: NaN, end: NaN }); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the condition can be just
!Number.isInteger()
as that returnstrue
when passing something that is not a number.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes: http://www.ecma-international.org/ecma-262/8.0/index.html#sec-number.isinteger