From 30edb5aee9c21c9696afbd61c28b51be461c3bc2 Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Sun, 12 Jul 2015 00:58:20 +0000 Subject: [PATCH] repl: preventing REPL crash with inherited properties MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When an inherited property is used as a REPL keyword, the REPL crashes. ➜ Desktop iojs > process.version 'v2.3.4' > .toString readline.js:913 stream[ESCAPE_DECODER].next(r[i]); ^ TypeError: Cannot read property 'call' of undefined at REPLServer.parseREPLKeyword (repl.js:746:15) at REPLServer. (repl.js:284:16) at emitOne (events.js:77:13) at REPLServer.emit (events.js:169:7) at REPLServer.Interface._onLine (readline.js:210:10) at REPLServer.Interface._line (readline.js:549:8) at REPLServer.Interface._ttyWrite (readline.js:826:14) at ReadStream.onkeypress (readline.js:105:10) at emitTwo (events.js:87:13) at ReadStream.emit (events.js:172:7) ➜ Desktop This patch makes the internal `commands` object inherit from `null` so that there will be no inherited properties. > process.version 'v2.3.5-pre' > .toString Invalid REPL keyword > PR-URL: https://github.com/nodejs/io.js/pull/2163 Reviewed-By: Jeremiah Senkpiel --- lib/repl.js | 2 +- test/parallel/test-repl.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 11ee6f58216ff6..0f93bfdc40a734 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -217,7 +217,7 @@ function REPLServer(prompt, self.setPrompt(prompt !== undefined ? prompt : '> '); - this.commands = {}; + this.commands = Object.create(null); defineDefaultCommands(this); // figure out which "writer" function to use diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 94d6bca283cb59..65affa9b9480e8 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -194,6 +194,10 @@ function error_test() { // the error message { client: client_unix, send: '.invalid_repl_command', expect: 'Invalid REPL keyword\n' + prompt_unix }, + // this makes sure that we don't crash when we use an inherited property as + // a REPL command + { client: client_unix, send: '.toString', + expect: 'Invalid REPL keyword\n' + prompt_unix }, ]); }