Skip to content

Commit 755609c

Browse files
Hakerh400BethGriggs
authored andcommitted
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: #26832 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Signed-off-by: Beth Griggs <Bethany.Griggs@uk.ibm.com>
1 parent 6c40f7f commit 755609c

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/tty_wrap.cc

+6-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace node {
3232

3333
using v8::Array;
3434
using v8::Context;
35+
using v8::Function;
3536
using v8::FunctionCallbackInfo;
3637
using v8::FunctionTemplate;
3738
using v8::Integer;
@@ -40,7 +41,6 @@ using v8::Object;
4041
using v8::String;
4142
using v8::Value;
4243

43-
4444
void TTYWrap::Initialize(Local<Object> target,
4545
Local<Value> unused,
4646
Local<Context> context,
@@ -61,10 +61,11 @@ void TTYWrap::Initialize(Local<Object> target,
6161
env->SetMethodNoSideEffect(target, "isTTY", IsTTY);
6262
env->SetMethodNoSideEffect(target, "guessHandleType", GuessHandleType);
6363

64-
target->Set(env->context(),
65-
ttyString,
66-
t->GetFunction(env->context()).ToLocalChecked()).FromJust();
67-
env->set_tty_constructor_template(t);
64+
Local<Value> func;
65+
if (t->GetFunction(env->context()).ToLocal(&func) &&
66+
target->Set(env->context(), ttyString, func).IsJust()) {
67+
env->set_tty_constructor_template(t);
68+
}
6869
}
6970

7071

test/parallel/test-ttywrap-stack.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// This test ensures that console.log
5+
// will not crash the process if there
6+
// is not enough space on the V8 stack
7+
8+
const done = common.mustCall(() => {});
9+
10+
async function test() {
11+
await test();
12+
}
13+
14+
(async () => {
15+
try {
16+
await test();
17+
} catch (err) {
18+
console.log(err);
19+
}
20+
})().then(done, done);

0 commit comments

Comments
 (0)