Skip to content

Commit 1ec09b0

Browse files
committed
src: don't print garbage errors
If JS throws an object whose toString() method throws, then Node attempts to print an empty message, but actually prints garbage. This commit checks for this case, and prints a message instead. Fixes: #4079 PR-URL: #4112 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Chris Dickinson <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent e6e7891 commit 1ec09b0

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/node.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -1511,8 +1511,10 @@ static void ReportException(Environment* env,
15111511
name.IsEmpty() ||
15121512
name->IsUndefined()) {
15131513
// Not an error object. Just print as-is.
1514-
node::Utf8Value message(env->isolate(), er);
1515-
PrintErrorString("%s\n", *message);
1514+
String::Utf8Value message(er);
1515+
1516+
PrintErrorString("%s\n", *message ? *message :
1517+
"<toString() threw exception>");
15161518
} else {
15171519
node::Utf8Value name_string(env->isolate(), name);
15181520
node::Utf8Value message_string(env->isolate(), message);

test/fixtures/throws_error7.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
throw {
2+
toString: function() {
3+
throw this;
4+
}
5+
};

test/parallel/test-error-reporting.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ function errExec(script, callback) {
2424

2525
// Count the tests
2626
exits++;
27-
28-
console.log('.');
2927
});
3028
}
3129

@@ -64,6 +62,11 @@ errExec('throws_error6.js', function(err, stdout, stderr) {
6462
assert.ok(/SyntaxError/.test(stderr));
6563
});
6664

65+
// Object that throws in toString() doesn't print garbage
66+
errExec('throws_error7.js', function(err, stdout, stderr) {
67+
assert.ok(/<toString\(\) threw exception/.test(stderr));
68+
});
69+
6770
process.on('exit', function() {
68-
assert.equal(6, exits);
71+
assert.equal(7, exits);
6972
});

0 commit comments

Comments
 (0)