Skip to content

Commit 4bd410b

Browse files
committed
vm: don't abort process when stack space runs out
Make less assumptions about what objects will be available when vm context creation or error message printing fail because V8 runs out of JS stack space. Ref: #6899 PR-URL: #6907 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent cc6a78e commit 4bd410b

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/node.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1521,8 +1521,8 @@ void AppendExceptionLine(Environment* env,
15211521
// sourceline to 78 characters, and we end up not providing very much
15221522
// useful debugging info to the user if we remove 62 characters.
15231523

1524-
int start = message->GetStartColumn(env->context()).FromJust();
1525-
int end = message->GetEndColumn(env->context()).FromJust();
1524+
int start = message->GetStartColumn(env->context()).FromMaybe(0);
1525+
int end = message->GetEndColumn(env->context()).FromMaybe(0);
15261526

15271527
char arrow[1024];
15281528
int max_off = sizeof(arrow) - 2;

src/node_contextify.cc

+10-4
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ class ContextifyContext {
205205

206206
Local<Context> ctx = Context::New(env->isolate(), nullptr, object_template);
207207

208-
CHECK(!ctx.IsEmpty());
208+
if (ctx.IsEmpty()) {
209+
env->ThrowError("Could not instantiate context");
210+
return Local<Context>();
211+
}
212+
209213
ctx->SetSecurityToken(env->context()->GetSecurityToken());
210214

211215
// We need to tie the lifetime of the sandbox object with the lifetime of
@@ -632,9 +636,11 @@ class ContextifyScript : public BaseObject {
632636
env->arrow_message_private_symbol());
633637

634638
Local<Value> arrow;
635-
if (!(maybe_value.ToLocal(&arrow) &&
636-
arrow->IsString() &&
637-
stack->IsString())) {
639+
if (!(maybe_value.ToLocal(&arrow) && arrow->IsString())) {
640+
return;
641+
}
642+
643+
if (stack.IsEmpty() || !stack->IsString()) {
638644
return;
639645
}
640646

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const vm = require('vm');
5+
6+
function a() {
7+
try {
8+
return a();
9+
} catch (e) {
10+
// Throw an exception as near to the recursion-based RangeError as possible.
11+
return vm.runInThisContext('() => 42')();
12+
}
13+
}
14+
15+
assert.strictEqual(a(), 42);
16+
17+
function b() {
18+
try {
19+
return b();
20+
} catch (e) {
21+
// This writes a lot of noise to stderr, but it still works.
22+
return vm.runInNewContext('() => 42')();
23+
}
24+
}
25+
26+
assert.strictEqual(b(), 42);

0 commit comments

Comments
 (0)