Skip to content

Commit

Permalink
repl: avoid crashing from null and undefined errors
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
priyank-p authored and cjihrig committed Nov 6, 2017
1 parent c4736cf commit 05f9047
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,13 @@ function REPLServer(prompt,
}
} catch (e) {
err = e;
if (err.message === 'Script execution interrupted.') {

if (err && err.message === 'Script execution interrupted.') {
// The stack trace for this case is not very useful anyway.
Object.defineProperty(err, 'stack', { value: '' });
}

if (err && process.domain) {
if (process.domain) {
debug('not recoverable, send to domain');
process.domain.emit('error', err);
process.domain.exit();
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-repl-throw-null-or-undefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
require('../common');

// This test ensures that the repl does not
// crash or emit error when throwing `null|undefined`
// ie `throw null` or `throw undefined`

const assert = require('assert');
const repl = require('repl');

const r = repl.start();

assert.doesNotThrow(() => {
r.write('throw null\n');
r.write('throw undefined\n');
}, TypeError, 'repl crashes/throw error on `throw null|undefined`');

r.write('.exit\n');

0 comments on commit 05f9047

Please sign in to comment.