Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Contributor

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.

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');
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down
9 changes: 8 additions & 1 deletion test/parallel/test-fs-open.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 TypeError. That should be unconditional, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me rephrase. You should assert that the error is a TypeError, not put it in an if statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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';
}
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can condense this to assert.throws(function() { ... }, /File open flags may only be string or integer/).

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
});