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: https://github.com/nodejs/node/issues/16545
Fixes: https://github.com/nodejs/node/issues/16607

PR-URL: https://github.com/nodejs/node/pull/16574
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Lance Ball <lball@redhat.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
This commit is contained in:
cPhost 2017-10-28 16:19:53 +00:00 committed by Lance Ball
parent 8025bba1a3
commit bb59d2bd19
No known key found for this signature in database
GPG Key ID: 1B4326AE55E9408C
2 changed files with 21 additions and 2 deletions

View File

@ -257,12 +257,13 @@ function REPLServer(prompt,
} }
} catch (e) { } catch (e) {
err = 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. // The stack trace for this case is not very useful anyway.
Object.defineProperty(err, 'stack', { value: '' }); Object.defineProperty(err, 'stack', { value: '' });
} }
if (err && process.domain) { if (process.domain) {
debug('not recoverable, send to domain'); debug('not recoverable, send to domain');
process.domain.emit('error', err); process.domain.emit('error', err);
process.domain.exit(); process.domain.exit();

View File

@ -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');