From d27ec13cd3b4da00fb22a627f341ab0d7aca7883 Mon Sep 17 00:00:00 2001 From: cPhost <23620441+cPhost@users.noreply.github.com> Date: Sat, 28 Oct 2017 16:19:53 +0000 Subject: [PATCH] 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 Reviewed-By: Lance Ball Reviewed-By: James M Snell Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Anna Henningsen Reviewed-By: Jeremiah Senkpiel Reviewed-By: Colin Ihrig Reviewed-By: Gireesh Punathil --- lib/repl.js | 5 +++-- .../test-repl-throw-null-or-undefined.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-repl-throw-null-or-undefined.js diff --git a/lib/repl.js b/lib/repl.js index 111dcc0ced8eb3..bf93ce8a3fbc6c 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -255,12 +255,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(); diff --git a/test/parallel/test-repl-throw-null-or-undefined.js b/test/parallel/test-repl-throw-null-or-undefined.js new file mode 100644 index 00000000000000..fd2fd202b5bcb6 --- /dev/null +++ b/test/parallel/test-repl-throw-null-or-undefined.js @@ -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');