From 6fb32ac2552a0a03b8e7d54ef9bda06909823b6b Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 21 Mar 2019 02:01:19 +0100 Subject: src: prevent crash in TTYWrap::Initialize When console.log is called for the first time it initializes TTYWrap object. However, if there is not enough space on the V8 stack, creating function template fails and triggers empty maybe local exception. PR-URL: https://github.com/nodejs/node/pull/26832 Reviewed-By: Ruben Bridgewater Reviewed-By: Joyee Cheung Reviewed-By: Anna Henningsen Reviewed-By: Ben Noordhuis --- src/tty_wrap.cc | 11 ++++++----- test/parallel/test-ttywrap-stack.js | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-ttywrap-stack.js diff --git a/src/tty_wrap.cc b/src/tty_wrap.cc index 405b70343f..33dd4dbdac 100644 --- a/src/tty_wrap.cc +++ b/src/tty_wrap.cc @@ -32,6 +32,7 @@ namespace node { using v8::Array; using v8::Context; +using v8::Function; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Integer; @@ -40,7 +41,6 @@ using v8::Object; using v8::String; using v8::Value; - void TTYWrap::Initialize(Local target, Local unused, Local context, @@ -61,10 +61,11 @@ void TTYWrap::Initialize(Local target, env->SetMethodNoSideEffect(target, "isTTY", IsTTY); env->SetMethodNoSideEffect(target, "guessHandleType", GuessHandleType); - target->Set(env->context(), - ttyString, - t->GetFunction(env->context()).ToLocalChecked()).FromJust(); - env->set_tty_constructor_template(t); + Local func; + if (t->GetFunction(env->context()).ToLocal(&func) && + target->Set(env->context(), ttyString, func).IsJust()) { + env->set_tty_constructor_template(t); + } } diff --git a/test/parallel/test-ttywrap-stack.js b/test/parallel/test-ttywrap-stack.js new file mode 100644 index 0000000000..b2ad69b1b2 --- /dev/null +++ b/test/parallel/test-ttywrap-stack.js @@ -0,0 +1,20 @@ +'use strict'; +const common = require('../common'); + +// This test ensures that console.log +// will not crash the process if there +// is not enough space on the V8 stack + +const done = common.mustCall(() => {}); + +async function test() { + await test(); +} + +(async () => { + try { + await test(); + } catch (err) { + console.log(err); + } +})().then(done, done); -- cgit v1.2.3