From cde6c0ebeb3839aa376e17c770e18ee00f15402c 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 1/3] repl: fix issue `throw null` and `throw undefined` crash
 in repl

The issue was that when it tries to check if `err.message == 'Script execution interrupted.'`
line 269 repl.js it throws error as e would be `null` or `undefined`. Now before it checks
the err.message it checks if err was `null|undefined` and id so returns err before checking
and exits,

`
// sample output repl
> throw null
Thrown null
> throw undefined
Thrown undefined
`

Fixes: https://github.com/nodejs/node/issues/16545
---
 lib/repl.js | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/repl.js b/lib/repl.js
index d4b95bf3a67f45..5e55f513871d63 100644
--- a/lib/repl.js
+++ b/lib/repl.js
@@ -257,7 +257,8 @@ 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: '' });
         }

From ca62b2acbbb616e45ab4b195e47da5efe0cd4708 Mon Sep 17 00:00:00 2001
From: cPhost <23620441+cPhost@users.noreply.github.com>
Date: Sat, 28 Oct 2017 21:27:29 +0000
Subject: [PATCH 2/3] test: add a regression test for testing throw null or
 throw undefined

---
 .../test-repl-throw-null-or-undefined.js      | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)
 create mode 100644 test/parallel/test-repl-throw-null-or-undefined.js

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..b06af02513b70e
--- /dev/null
+++ b/test/parallel/test-repl-throw-null-or-undefined.js
@@ -0,0 +1,24 @@
+'use strict';
+require('../common');
+
+// This test makes sures 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();
+
+r.write('throw null\n');
+r.write('throw undefined\n');
+r.write('.exit\n');
+
+let i = 0;
+r.on('line', function replOutput(output) {
+  const testStatement = i === 0 ? 'null' : 'undefined';
+  const expectedOutput = 'Thrown: ' + testStatement;
+  const msg = 'repl did not throw ' + testStatement;
+  assert.deepStrictEqual(output, expectedOutput, msg);
+  i++;
+});

From bd1769445c90ddcf4a72c81d2170d2810164aa66 Mon Sep 17 00:00:00 2001
From: cPhost <23620441+cPhost@users.noreply.github.com>
Date: Sun, 29 Oct 2017 00:21:32 +0000
Subject: [PATCH 3/3] nit: fixed code as requested, fixed tests

---
 lib/repl.js                                    |  2 +-
 .../test-repl-throw-null-or-undefined.js       | 18 ++++++------------
 2 files changed, 7 insertions(+), 13 deletions(-)

diff --git a/lib/repl.js b/lib/repl.js
index 5e55f513871d63..40c3a68714cd9f 100644
--- a/lib/repl.js
+++ b/lib/repl.js
@@ -263,7 +263,7 @@ function REPLServer(prompt,
           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
index b06af02513b70e..7379cd4b22aec4 100644
--- a/test/parallel/test-repl-throw-null-or-undefined.js
+++ b/test/parallel/test-repl-throw-null-or-undefined.js
@@ -1,7 +1,7 @@
 'use strict';
 require('../common');
 
-// This test makes sures that the repl does not 
+// This test ensures that the repl does not 
 // crash or emit error when throwing `null|undefined`
 // ie `throw null` or `throw undefined`
 
@@ -10,15 +10,9 @@ const repl = require('repl');
 
 const r = repl.start();
 
-r.write('throw null\n');
-r.write('throw undefined\n');
-r.write('.exit\n');
+assert.doesNotThrow(() => {
+  r.write('throw null\n');
+  r.write('throw undefined\n');
+}, TypeError, 'repl crashes/throw error on `throw null|undefined`');
 
-let i = 0;
-r.on('line', function replOutput(output) {
-  const testStatement = i === 0 ? 'null' : 'undefined';
-  const expectedOutput = 'Thrown: ' + testStatement;
-  const msg = 'repl did not throw ' + testStatement;
-  assert.deepStrictEqual(output, expectedOutput, msg);
-  i++;
-});
+r.write('.exit\n');