-
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 2 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,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); | ||
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. Would |
||
} | ||
if (this.end === undefined) { | ||
this.end = Infinity; | ||
} else if (typeof this.end !== 'number') { | ||
} else if (typeof this.end !== 'number' || Number.isNaN(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. Can you add this for the other 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. @addaleax Sure! On it. 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. @addaleax done. |
||
throw new ERR_INVALID_ARG_TYPE('end', 'number', this.end); | ||
} | ||
|
||
|
@@ -2028,7 +2028,7 @@ function ReadStream(path, options) { | |
// 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') | ||
if (typeof this.end !== 'number' || Number.isNaN(this.end)) | ||
this.end = Infinity; | ||
|
||
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. |
||
|
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'); | ||
|
@@ -18,10 +22,16 @@ const createReadStreamErr = (path, opt) => { | |
{ | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
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); | ||
|
||
// Should also throw on NaN (for https://github.com/nodejs/node/pull/19732) | ||
createReadStreamErr(example, { start: NaN }); | ||
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. I think we can leave only And at least we need to delete 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. @anliting If 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. If we are going to test 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. @anliting That would take a different branch, though? 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. Oh, I see that line (which makes "If we are going to test the end part ..." means if we are going to test if this line work: 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. We might need to change
to
to pass 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. @anliting Ah, right! Yes, we should probably do that. |
||
createReadStreamErr(example, { end: NaN }); | ||
createReadStreamErr(example, { start: NaN, end: NaN }); | ||
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. Please add some tests with 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. @addaleax Should the case 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. @anliting I think 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. Exactly what I had been thinking. Doesn't throwing an error on 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. My appointment on that But passing 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.
👍
It’s probably a good idea to let the docs reflect this, but it might not even have to be semver-minor, if it’s just documenting existing behaviour. (After all, this is supposed to be a bug fix PR.) Like, you’re right that technically we’d be adding support for something that is not documented as part of the API, but with Node we have to be conservative about these things and can’t just say “X is unsupported because it’s undocumented”. 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. @addaleax Okay, now I understand your position for this; I'll get used to that. XD 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. @addaleax so, should I also make changes to docs in this PR? 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. @ryzokuken You can do that, or you can do it in another PR, or you can leave it up for somebody else to document this. But preferably one of the first two options :) 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. option 2 sounds perfect. Let me address all the concerns regarding the code in this PR and I'd make another one for the documentation changes. |
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.
As suggested by @anliting we can probably use just
Number.isInteger()
.