fs: use stat.st_size only to read regular files
authorSantiago Gimeno <santiago.gimeno@gmail.com>
Thu, 5 Mar 2015 17:33:38 +0000 (18:33 +0100)
committerBert Belder <bertbelder@gmail.com>
Thu, 12 Mar 2015 23:40:33 +0000 (16:40 -0700)
Using st_size to read non-regular files can lead to not reading all the
data.

PR-URL: https://github.com/iojs/io.js/pull/1074
Reviewed-By: Bert Belder <bertbelder@gmail.com>
lib/fs.js

index 9828b30..adcb708 100644 (file)
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -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 = [];
@@ -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);