From 25776f3ea14fb0aadae38ce91c2fbcfa95746255 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 17 Dec 2015 12:13:11 +0100 Subject: [PATCH] debugger: guard against call from non-node context Fix a segmentation fault when the debug message handler was called from a context without an associated `node::Environment`. Fixes: https://github.com/nodejs/node/issues/4261 Fixes: https://github.com/nodejs/node/issues/4322 PR-URL: https://github.com/nodejs/node/pull/4328 Reviewed-By: Evan Lucas Reviewed-By: James M Snell --- src/debug-agent.cc | 2 ++ test/parallel/test-debug-no-context.js | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 test/parallel/test-debug-no-context.js diff --git a/src/debug-agent.cc b/src/debug-agent.cc index 3409fb2603ae9b..06ede7fdef4aa9 100644 --- a/src/debug-agent.cc +++ b/src/debug-agent.cc @@ -321,6 +321,8 @@ void Agent::EnqueueMessage(AgentMessage* message) { void Agent::MessageHandler(const v8::Debug::Message& message) { Isolate* isolate = message.GetIsolate(); Environment* env = Environment::GetCurrent(isolate); + if (env == nullptr) + return; // Called from a non-node context. Agent* a = env->debugger_agent(); CHECK_NE(a, nullptr); CHECK_EQ(isolate, a->parent_env()->isolate()); diff --git a/test/parallel/test-debug-no-context.js b/test/parallel/test-debug-no-context.js new file mode 100644 index 00000000000000..a143e67ac66cd5 --- /dev/null +++ b/test/parallel/test-debug-no-context.js @@ -0,0 +1,24 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const spawn = require('child_process').spawn; + +const args = [`--debug`, `--debug-port=${common.PORT}`, `--interactive`]; +const proc = spawn(process.execPath, args, { stdio: 'pipe' }); +proc.stdin.write(` + util.inspect(Promise.resolve(42)); + util.inspect(Promise.resolve(1337)); + .exit +`); +proc.on('exit', common.mustCall((exitCode, signalCode) => { + assert.strictEqual(exitCode, 0); + assert.strictEqual(signalCode, null); +})); +let stdout = ''; +proc.stdout.setEncoding('utf8'); +proc.stdout.on('data', data => stdout += data); +process.on('exit', () => { + assert(stdout.includes('Promise { 42 }')); + assert(stdout.includes('Promise { 1337 }')); +});