-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
fs: make ReadStream throw error on NaN #19732
Changes from 7 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 |
---|---|---|
|
@@ -2009,12 +2009,15 @@ function ReadStream(path, options) { | |
|
||
if (this.start !== undefined) { | ||
if (typeof this.start !== 'number') { | ||
throw new ERR_INVALID_ARG_TYPE('start', 'number', this.start); | ||
throw new ERR_INVALID_ARG_TYPE('options.start', 'number', options.start); | ||
} | ||
if (!Number.isInteger(this.start)) { | ||
throw new ERR_OUT_OF_RANGE('options.start', 'an integer', options.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 (!Number.isInteger(this.end)) { | ||
throw new ERR_INVALID_ARG_TYPE('end', 'integer', this.end); | ||
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. The same as above: distinguishing the type from the rest of the error would be good. You can actually copy the same part and just change what variable it applies to. 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. For example, change if (this.end === undefined) {
this.end = Infinity;
} else if (!Number.isInteger(this.end)) {
throw new ERR_INVALID_ARG_TYPE('end', 'integer', this.end);
} to if (this.end === undefined) {
this.end = Infinity;
}else{
if(typeof this.end!=='number'){
throw new ERR_INVALID_ARG_TYPE('end','number',this.end)
}
if (!Number.isInteger(this.end)) {
throw new ERR_OUT_OF_RANGE('end', 'integer', this.end);
}
} . |
||
} | ||
|
||
if (this.start > this.end) { | ||
|
@@ -2023,13 +2026,15 @@ function ReadStream(path, options) { | |
} | ||
|
||
this.pos = this.start; | ||
} | ||
|
||
// Backwards compatibility: Make sure `end` is a number regardless of `start`. | ||
// TODO(addaleax): Make the above typecheck not depend on `start` instead. | ||
// (That is a semver-major change). | ||
if (typeof this.end !== 'number') | ||
} else if (typeof this.end !== 'number') { | ||
// Backwards compatibility: Make sure `end` is a number regardless of | ||
// `start`. | ||
// TODO(addaleax): Make the above typecheck not depend on `start` instead. | ||
// (That is a semver-major change). | ||
this.end = Infinity; | ||
} else if (!Number.isInteger(this.end)) { | ||
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. As we decided to support 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. Also change line 2019. (Comments are not allowed there.) |
||
throw new ERR_OUT_OF_RANGE('options.end', 'an integer', options.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'); | ||
|
@@ -10,18 +14,23 @@ fs.createReadStream(example, null); | |
fs.createReadStream(example, 'utf8'); | ||
fs.createReadStream(example, { encoding: 'utf8' }); | ||
|
||
const createReadStreamErr = (path, opt) => { | ||
const createReadStreamErr = (path, opt, errorCode) => { | ||
common.expectsError( | ||
() => { | ||
fs.createReadStream(path, opt); | ||
}, | ||
{ | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
code: errorCode, | ||
type: TypeError | ||
}); | ||
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. Nit: unnecessary change. |
||
}; | ||
|
||
createReadStreamErr(example, 123); | ||
createReadStreamErr(example, 0); | ||
createReadStreamErr(example, true); | ||
createReadStreamErr(example, false); | ||
createReadStreamErr(example, 123, 'ERR_INVALID_ARG_TYPE'); | ||
createReadStreamErr(example, 0, 'ERR_INVALID_ARG_TYPE'); | ||
createReadStreamErr(example, true, 'ERR_INVALID_ARG_TYPE'); | ||
createReadStreamErr(example, false, 'ERR_INVALID_ARG_TYPE'); | ||
|
||
// Should also throw on NaN (for https://github.com/nodejs/node/pull/19732) | ||
createReadStreamErr(example, { start: NaN }, 'ERR_OUT_OF_RANGE'); | ||
createReadStreamErr(example, { end: NaN }, 'ERR_OUT_OF_RANGE'); | ||
createReadStreamErr(example, { start: NaN, end: NaN }, 'ERR_OUT_OF_RANGE'); | ||
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. Can you please still add tests for Infinity? :-) That is independent of making a breaking change. Setting start to Infinity will still throw right now and setting end to infinity should not throw. |
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.
Sorry, I guess I expressed myself not well enough in my last comment. What I meant is that you can move the type check inside of the
Number.isInteger
check. That way it will only do the type check in the negative case.As in:
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.
No, I was wrong. @BridgeAR is faster when
Number.isInteger(this.start)
.----EDITED----
I think his approach is okay, maybe faster.
With
, both
ERR_INVALID_ARG_TYPE
andERR_OUT_OF_RANGE
cases run 2 checks to be reached; while with,
ERR_INVALID_ARG_TYPE
runs 1 check to be reached, andERR_INVALID_ARG_TYPE
runs 2.