fs: fix fs.readFileSync fd leak when get RangeError
authorJackson Tian <shyvo1987@gmail.com>
Wed, 6 Aug 2014 03:21:59 +0000 (11:21 +0800)
committerTimothy J Fontaine <tjfontaine@gmail.com>
Thu, 7 Aug 2014 18:27:12 +0000 (11:27 -0700)
lib/fs.js

index 990eb6d..1e3dc64 100644 (file)
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -304,7 +304,13 @@ fs.readFileSync = function(path, options) {
   if (size === 0) {
     buffers = [];
   } else {
-    buffer = new Buffer(size);
+    var threw = true;
+    try {
+      buffer = new Buffer(size);
+      threw = false;
+    } finally {
+      if (threw) fs.closeSync(fd);
+    }
   }
 
   var done = false;