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.read into zero buffer should not throw exception #4518

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
14 changes: 14 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,12 @@ fs.read = function(fd, buffer, offset, length, position, callback) {
};
}

if (length === 0) {
return process.nextTick(function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmmm, this is not right. There is actually a deprecated fs.read form which returns string.

So,

  1. This will actually return a buffer and the deprecated fs.read will collapse.
  2. The order of parameters to the callback, in string form, is actually string and the bytesRead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, this calls the redefined callback function (that starts on line 600), so the right arguments should be passed to the callback function, namely: null, '', and 0.

Unless I'm missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You comment prompted me to check the fs.readSync case and the issue you describe exists there. I'll fix it and add a test.

callback && callback(null, 0, buffer);
});
}

function wrapper(err, bytesRead) {
// Retain a reference to buffer so that it can't be GC'ed too soon.
callback && callback(err, bytesRead || 0, buffer);
Expand Down Expand Up @@ -648,6 +654,14 @@ fs.readSync = function(fd, buffer, offset, length, position) {
offset = 0;
}

if (length === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The check here means that even if the file doesn't exist/inaccessible, if the length is zero, we will return valid values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How do you propose to solve this? @bnoordhuis asked me to move the checks to lib/fs.js to avoid some kernel edge case.

Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

@thefourtheye Since the fd itself is passed best we can attempt to do is read directly from it. But as @feross mentioned, there are cross platform issues that prevents this being done correctly. I don't think that assuming the fd is alright is a problem in this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

@trevnorris Ah, you are right. I totally missed that. Then this will not be a problem here.

if (legacy) {
return ['', 0];
} else {
return 0;
}
}

var r = binding.read(fd, buffer, offset, length, position);
if (!legacy) {
return r;
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-fs-read-buffer-zero-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const path = require('path');
const Buffer = require('buffer').Buffer;
const fs = require('fs');
const filepath = path.join(common.fixturesDir, 'x.txt');
const fd = fs.openSync(filepath, 'r');
const bufferAsync = new Buffer(0);
const bufferSync = new Buffer(0);

fs.read(fd, bufferAsync, 0, 0, 0, common.mustCall(function(err, bytesRead) {
assert.equal(bytesRead, 0);
assert.deepEqual(bufferAsync, new Buffer(0));
}));

const r = fs.readSync(fd, bufferSync, 0, 0, 0);
assert.deepEqual(bufferSync, new Buffer(0));
assert.equal(r, 0);
18 changes: 18 additions & 0 deletions test/parallel/test-fs-read-zero-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const filepath = path.join(common.fixturesDir, 'x.txt');
const fd = fs.openSync(filepath, 'r');
const expected = '';

fs.read(fd, 0, 0, 'utf-8', common.mustCall(function(err, str, bytesRead) {
assert.ok(!err);
assert.equal(str, expected);
assert.equal(bytesRead, 0);
}));

const r = fs.readSync(fd, 0, 0, 'utf-8');
assert.equal(r[0], expected);
assert.equal(r[1], 0);