Skip to content

Commit

Permalink
fs: use stat.st_size only to read regular files
Browse files Browse the repository at this point in the history
Using st_size to read non-regular files can lead to not reading all the
data.

PR-URL: #1074
Reviewed-By: Bert Belder <bertbelder@gmail.com>
  • Loading branch information
santigimeno authored and piscisaureus committed Mar 12, 2015
1 parent 0782c24 commit a6af709
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/fs.js
Expand Up @@ -317,7 +317,7 @@ function readFileAfterStat(err, st) {
if (err)
return context.close(err);

var size = context.size = st.size;
var size = context.size = st.isFile() ? st.size : 0;

if (size === 0) {
context.buffers = [];
Expand Down Expand Up @@ -395,10 +395,12 @@ fs.readFileSync = function(path, options) {
var flag = options.flag || 'r';
var fd = fs.openSync(path, flag, 0o666);

var st;
var size;
var threw = true;
try {
size = fs.fstatSync(fd).size;
st = fs.fstatSync(fd);
size = st.isFile() ? st.size : 0;
threw = false;
} finally {
if (threw) fs.closeSync(fd);
Expand Down

0 comments on commit a6af709

Please sign in to comment.