Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: readFile on dir should fail EISDIR not EBADF #286

Merged
merged 1 commit into from
Apr 20, 2020
Merged
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
3 changes: 3 additions & 0 deletions lib/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,9 @@ Binding.prototype.read = function(
throw new FSError('EBADF');
}
const file = descriptor.getItem();
if (file instanceof Directory) {
throw new FSError('EISDIR');
}
if (!(file instanceof File)) {
// deleted or not a regular file
throw new FSError('EBADF');
Expand Down
12 changes: 8 additions & 4 deletions test/lib/fs.readFile.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('fs.readFile(filename, [options], callback)', function() {
it('fails for directory', function(done) {
fs.readFile('path/to', function(err, data) {
assert.instanceOf(err, Error);
assert.equal(err.code, 'EBADF');
assert.equal(err.code, 'EISDIR');
done();
});
});
Expand All @@ -55,7 +55,7 @@ describe('fs.readFile(filename, [options], callback)', function() {
},
function(err) {
assert.instanceOf(err, Error);
assert.equal(err.code, 'EBADF');
assert.equal(err.code, 'EISDIR');
done();
}
);
Expand Down Expand Up @@ -104,9 +104,13 @@ describe('fs.readFileSync(filename, [options])', function() {
});

it('fails for directory', function() {
assert.throws(function() {
try {
fs.readFileSync('path/to');
});
assert.fail('should not succeed.');
} catch (err) {
assert.instanceOf(err, Error);
assert.equal(err.code, 'EISDIR');
}
});

it('fails for bad path', function() {
Expand Down