-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -609,6 +609,12 @@ fs.read = function(fd, buffer, offset, length, position, callback) { | |
}; | ||
} | ||
|
||
if (length === 0) { | ||
return process.nextTick(function() { | ||
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); | ||
|
@@ -648,6 +654,14 @@ fs.readSync = function(fd, buffer, offset, length, position) { | |
offset = 0; | ||
} | ||
|
||
if (length === 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
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); |
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); |
There was a problem hiding this comment.
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 returnsstring
.So,
fs.read
will collapse.There was a problem hiding this comment.
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
,''
, and0
.Unless I'm missing something.
There was a problem hiding this comment.
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.