fs: close file if fstat() fails in readFile()
authorcjihrig <cjihrig@gmail.com>
Wed, 28 May 2014 22:34:04 +0000 (18:34 -0400)
committerBert Belder <bertbelder@gmail.com>
Thu, 29 May 2014 01:57:55 +0000 (18:57 -0700)
Currently, if fstat() fails in readFile(), the callback
is invoked without closing the file. This commit closes
the file before calling back.

Closes #7697

lib/fs.js

index cdcc13d..e8e8250 100644 (file)
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -229,7 +229,12 @@ fs.readFile = function(path, options, callback_) {
     fd = fd_;
 
     fs.fstat(fd, function(er, st) {
-      if (er) return callback(er);
+      if (er) {
+        return fs.close(fd, function() {
+          callback(er);
+        });
+      }
+
       size = st.size;
       if (size === 0) {
         // the kernel lies about many files.