-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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: fix incorrect error message in fs.open family when flags are not string or int #2873
Changes from all commits
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 |
---|---|---|
|
@@ -465,11 +465,16 @@ fs.readFileSync = function(path, options) { | |
|
||
// Used by binding.open and friends | ||
function stringToFlags(flag) { | ||
// Only mess with strings | ||
if (typeof flag !== 'string') { | ||
// Integers are already good to go | ||
if (Number.isInteger(flag)) { | ||
return flag; | ||
} | ||
|
||
// We only allow strings from here on | ||
if (typeof flag !== 'string') { | ||
throw new TypeError('File open flags may only be string or integer'); | ||
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. Anyone who sees this message and looks at the docs would immediately report think this a bug, since it conflicts with the docs. If you want to say that, I suggest documenting the behaviour. I strongly suspect that the fact that flags can be an int is historical, kept for backwards compat from the v0.small days, and should be deprecated, but its hard to deprecate a piece of an API. |
||
} | ||
|
||
switch (flag) { | ||
case 'r' : return O_RDONLY; | ||
case 'rs' : // fall through | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,8 +32,15 @@ fs.open(__filename, 'rs', function(err, fd) { | |
openFd2 = fd; | ||
}); | ||
|
||
assert.throws(function() { | ||
fs.open('/path/to/file/that/does/not/exist', { bad: 'flags' }, assert.fail); | ||
}, function(err) { | ||
if (err instanceof 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. I don't think you want to check if the error is a 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. Well I wrote the error as a TypeError since its thrown on bad types only, so might as well test for it, no? 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. Let me rephrase. You should assert that the error is a 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 see my comment below to @bnoordhuis. I can't seem to satisfy both your requests. 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. Do what @bnoordhuis is asking for :-) |
||
return err.message === 'File open flags may only be string or integer'; | ||
} | ||
}); | ||
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. You can condense this 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. Then I wouldn't be checking that it's a TypeError. That's not a problem? |
||
|
||
process.on('exit', function() { | ||
assert.ok(openFd); | ||
assert.ok(openFd2); | ||
}); | ||
|
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.
This check doesn't work as you think it does: the C++ layer requires flags to fit within 32 bits, this check here only requires it to be integral. Try with
0xfffffffff
, and you will see.That's why in my PR I pass anything with typeof number to the C++ layer, it seems easier to let it do this check, because it does it correctly. Though you can put more code in here, checking the bounds futher.