Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit da3137d

Browse filesBrowse files
cjihrigrvagg
authored andcommitted
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 <info@bnoordhuis.nl> Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <jmwsoft@gmail.com>
1 parent 1d50819 commit da3137d
Copy full SHA for da3137d

File tree

Expand file treeCollapse file tree

3 files changed

+15
-5
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+15
-5
lines changed
Open diff view settings
Collapse file

‎src/node.cc‎

Copy file name to clipboardExpand all lines: src/node.cc
+4-2Lines changed: 4 additions & 2 deletions
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);
Collapse file

‎test/fixtures/throws_error7.js‎

Copy file name to clipboard
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
throw {
2+
toString: function() {
3+
throw this;
4+
}
5+
};
Collapse file

‎test/parallel/test-error-reporting.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-error-reporting.js
+6-3Lines changed: 6 additions & 3 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.