summaryrefslogtreecommitdiff
path: root/test/parallel
diff options
context:
space:
mode:
authorEvan Lucas <evanlucas@me.com>2015-10-22 06:58:26 -0500
committerEvan Lucas <evanlucas@me.com>2015-10-23 16:09:46 -0500
commitb6207906c452be4c5f049a098fc645fdfb897306 (patch)
treeab31fb41df7fb7bfcef06fc2f375ee0761d63624 /test/parallel
parent594500f9f1c36db13dd2f7b0b380b545e7463324 (diff)
downloadnode-new-b6207906c452be4c5f049a098fc645fdfb897306.tar.gz
fs: pass err to callback if buffer is too big
In fs.readFile, if an encoding is specified and toString fails, do not throw an error. Instead, pass the error to the callback. Fixes: https://github.com/nodejs/node/issues/2767 PR-URL: https://github.com/nodejs/node/pull/3485 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Diffstat (limited to 'test/parallel')
-rw-r--r--test/parallel/test-fs-readfile-tostring-fail.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/test/parallel/test-fs-readfile-tostring-fail.js b/test/parallel/test-fs-readfile-tostring-fail.js
new file mode 100644
index 0000000000..b5c4ea8f7a
--- /dev/null
+++ b/test/parallel/test-fs-readfile-tostring-fail.js
@@ -0,0 +1,52 @@
+'use strict';
+
+const common = require('../common');
+const assert = require('assert');
+const fs = require('fs');
+const path = require('path');
+const kStringMaxLength = process.binding('buffer').kStringMaxLength;
+
+common.refreshTmpDir();
+
+const file = path.join(common.tmpDir, 'toobig.txt');
+const stream = fs.createWriteStream(file, {
+ flags: 'a'
+});
+
+const size = kStringMaxLength / 200;
+const a = new Buffer(size).fill('a');
+
+for (var i = 0; i < 201; i++) {
+ stream.write(a);
+}
+
+stream.end();
+stream.on('finish', common.mustCall(function() {
+ // make sure that the toString does not throw an error
+ fs.readFile(file, 'utf8', common.mustCall(function(err, buf) {
+ assert.ok(err instanceof Error);
+ assert.strictEqual('toString failed', err.message);
+ }));
+}));
+
+function destroy() {
+ try {
+ fs.unlinkSync(file);
+ } catch (err) {
+ // it may not exist
+ }
+}
+
+process.on('exit', destroy);
+
+process.on('SIGINT', function() {
+ destroy();
+ process.exit();
+});
+
+// To make sure we don't leave a very large file
+// on test machines in the event this test fails.
+process.on('uncaughtException', function(err) {
+ destroy();
+ throw err;
+});