diff --git a/lib/fs.js b/lib/fs.js index f2fcb69a0e..4e3574e325 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -221,12 +221,7 @@ fs.readFileSync = function(path, encoding) { } pos += bytesRead; - - if (size !== 0) { - done = pos >= size; - } else { - done = bytesRead >= 0; - } + done = (bytesRead === 0) || (size !== 0 && pos >= size); } fs.closeSync(fd); @@ -234,6 +229,8 @@ fs.readFileSync = function(path, encoding) { if (size === 0) { // data was collected into the buffers list. buffer = Buffer.concat(buffers, pos); + } else if (pos < size) { + buffer = buffer.slice(0, pos); } if (encoding) buffer = buffer.toString(encoding);