Skip to content

fs: fix fs.readFile, fs.exists{Sync} regression caused by assertions #17852

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

Closed
wants to merge 2 commits 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
10 changes: 10 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ fs.accessSync = function(path, mode) {
fs.exists = function(path, callback) {
if (handleError((path = getPathFromURL(path)), cb))
return;
if (typeof path !== 'string' && !(path instanceof Buffer)) {
Copy link
Contributor

@starkwang starkwang Dec 25, 2017

Choose a reason for hiding this comment

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

Should these type checks be placed at the beginning of these methods?

Copy link
Member

@Trott Trott Dec 25, 2017

Choose a reason for hiding this comment

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

@starkwang No because the check must happen after getPathFromURL(path) is called. The path may be a string, a Buffer, or a URL-like object.

throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'path',
['string', 'Buffer', 'URL']);
}
if (!nullCheck(path, cb)) return;
var req = new FSReqWrap();
req.oncomplete = cb;
Expand All @@ -361,6 +365,9 @@ Object.defineProperty(fs.exists, internalUtil.promisify.custom, {
fs.existsSync = function(path) {
try {
handleError((path = getPathFromURL(path)));
if (typeof path !== 'string' && !(path instanceof Buffer)) {
return false;
}
nullCheck(path);
binding.stat(pathModule.toNamespacedPath(path));
return true;
Expand Down Expand Up @@ -389,6 +396,9 @@ fs.readFile = function(path, options, callback) {
req.oncomplete(null, path);
});
return;
} else if (typeof path !== 'string' && !(path instanceof Buffer)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'path',
['string', 'Buffer', 'URL']);
}

binding.open(pathModule.toNamespacedPath(path),
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-fs-exists.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,14 @@ fs.exists(new URL('https://foo'), common.mustCall(function(y) {

assert(fs.existsSync(f));
assert(!fs.existsSync(`${f}-NO`));

common.expectsError(
() => { fs.exists(() => {}); },
{
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "path" argument must be one of type string, Buffer, or URL',
type: TypeError
}
);

assert(!fs.existsSync());
10 changes: 10 additions & 0 deletions test/parallel/test-fs-readfile-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

'use strict';
const common = require('../common');
const fs = require('fs');

// Test that fs.readFile fails correctly on a non-existent file.

Expand Down Expand Up @@ -54,3 +55,12 @@ test({ NODE_DEBUG: 'fs' }, common.mustCall((data) => {
assert(/EISDIR/.test(data));
assert(/test-fs-readfile-error/.test(data));
}));

common.expectsError(
() => { fs.readFile(() => {}); },
{
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "path" argument must be one of type string, Buffer, or URL',
type: TypeError
}
);