Skip to content

Commit 05f9047

Browse files
priyank-pcjihrig
authored andcommitted
repl: avoid crashing from null and undefined errors
When `throw undefined` or `throw null` is executed, the REPL crashes. This change does a check for `null|undefined` before accessing an error's properties to prevent crashing. Fixes: #16545 Fixes: #16607 PR-URL: #16574 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Lance Ball <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
1 parent c4736cf commit 05f9047

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/repl.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,13 @@ function REPLServer(prompt,
257257
}
258258
} catch (e) {
259259
err = e;
260-
if (err.message === 'Script execution interrupted.') {
260+
261+
if (err && err.message === 'Script execution interrupted.') {
261262
// The stack trace for this case is not very useful anyway.
262263
Object.defineProperty(err, 'stack', { value: '' });
263264
}
264265

265-
if (err && process.domain) {
266+
if (process.domain) {
266267
debug('not recoverable, send to domain');
267268
process.domain.emit('error', err);
268269
process.domain.exit();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
require('../common');
3+
4+
// This test ensures that the repl does not
5+
// crash or emit error when throwing `null|undefined`
6+
// ie `throw null` or `throw undefined`
7+
8+
const assert = require('assert');
9+
const repl = require('repl');
10+
11+
const r = repl.start();
12+
13+
assert.doesNotThrow(() => {
14+
r.write('throw null\n');
15+
r.write('throw undefined\n');
16+
}, TypeError, 'repl crashes/throw error on `throw null|undefined`');
17+
18+
r.write('.exit\n');

0 commit comments

Comments
 (0)