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: do not pass Buffer when toString() fails #9670

Merged
merged 1 commit into from
Dec 14, 2016
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
11 changes: 4 additions & 7 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ function readFileAfterClose(err) {
var buffer = null;
var callback = context.callback;

if (context.err)
return callback(context.err);
if (context.err || err)
return callback(context.err || err);

if (context.size === 0)
buffer = Buffer.concat(context.buffers, context.pos);
Expand All @@ -406,8 +406,6 @@ function readFileAfterClose(err) {
else
buffer = context.buffer;

if (err) return callback(err, buffer);

if (context.encoding) {
return tryToString(buffer, context.encoding, callback);
}
Expand All @@ -416,13 +414,12 @@ function readFileAfterClose(err) {
}

function tryToString(buf, encoding, callback) {
var e = null;
try {
buf = buf.toString(encoding);
} catch (err) {
e = err;
return callback(err);
}
callback(e, buf);
callback(null, buf);
}

function tryStatSync(fd, isUserFd) {
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-fs-readfile-tostring-fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ stream.on('finish', common.mustCall(function() {
fs.readFile(file, 'utf8', common.mustCall(function(err, buf) {
assert.ok(err instanceof Error);
assert.strictEqual('"toString()" failed', err.message);
assert.strictEqual(buf, undefined);
}));
}));

Expand Down