Skip to content

Commit 63a4268

Browse files
JacksonTianindutny
authored andcommitted
fs: fs.readFile should not throw uncaughtException
Reviewed-By: Fedor Indutny <fedor@indutny.com>
1 parent e643fe4 commit 63a4268

2 files changed

Lines changed: 67 additions & 4 deletions

File tree

lib/fs.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,13 @@ fs.readFile = function(path, options, callback_) {
239239
return read();
240240
}
241241

242-
if (size > kMaxLength)
243-
throw new RangeError('File size is greater than possible Buffer: ' +
244-
'0x3FFFFFFF bytes');
245-
242+
if (size > kMaxLength) {
243+
var err = new RangeError('File size is greater than possible Buffer: ' +
244+
'0x3FFFFFFF bytes');
245+
return fs.close(fd, function() {
246+
callback(err);
247+
});
248+
}
246249
buffer = new Buffer(size);
247250
read();
248251
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
25+
var path = require('path'),
26+
fs = require('fs'),
27+
filename = path.join(common.fixturesDir, 'large_file.txt');
28+
29+
var filesize = 1024 * 1024 * 1024;
30+
31+
function makeFile(done) {
32+
var buf = new Buffer(filesize / 1024);
33+
buf.fill('a');
34+
35+
try { fs.unlinkSync(filename); } catch (e) {}
36+
var w = 1024;
37+
var ws = fs.createWriteStream(filename);
38+
ws.on('close', done);
39+
ws.on('drain', write);
40+
write();
41+
function write() {
42+
do {
43+
w--;
44+
} while (false !== ws.write(buf) && w > 0);
45+
if (w === 0)
46+
ws.end();
47+
}
48+
}
49+
50+
makeFile(function () {
51+
fs.readFile(filename, function(err) {
52+
assert.ok(err, 'should get RangeError');
53+
assert.equal(err.name, 'RangeError', 'should get RangeError');
54+
try { fs.unlinkSync(filename); } catch (e) {}
55+
});
56+
});
57+
58+
process.on('uncaughtException', function (err) {
59+
assert.ok(!err, 'should not throw uncaughtException');
60+
});

0 commit comments

Comments
 (0)