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 confusing flags TypeError msg #2902

Closed
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
7 changes: 4 additions & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,11 @@ fs.readFileSync = function(path, options) {

// Used by binding.open and friends
function stringToFlags(flag) {
// Only mess with strings
if (typeof flag !== 'string') {
if (typeof flag === 'number')
return flag;
}

if (typeof flag !== 'string')
throw new TypeError('flag must be a string');

switch (flag) {
case 'r' : return O_RDONLY;
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-fs-open-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,17 @@ assert.equal(fs._stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
'x +x x+ rx rx+ wxx wax xwx xxx').split(' ').forEach(function(flags) {
assert.throws(function() { fs._stringToFlags(flags); });
});

// Use of numeric flags is permitted.
assert.equal(fs._stringToFlags(O_RDONLY), O_RDONLY);

// Non-numeric/string flags are a type error.
assert.throws(function() { fs._stringToFlags(undefined); }, TypeError);
assert.throws(function() { fs._stringToFlags(null); }, TypeError);
assert.throws(function() { fs._stringToFlags(assert); }, TypeError);
assert.throws(function() { fs._stringToFlags([]); }, TypeError);
assert.throws(function() { fs._stringToFlags({}); }, TypeError);

// Numeric flags that are not int will be passed to the binding, which
// will throw a TypeError
assert.throws(function() { fs.openSync('_', O_RDONLY + 0.1); }, TypeError);