Skip to content

Commit

Permalink
Prevent segfault when eval throws an error (denoland#1411)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo authored and ry committed Jan 9, 2019
1 parent 3634488 commit d835c84
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 7 additions & 4 deletions libdeno/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ std::string EncodeExceptionAsJSON(v8::Local<v8::Context> context,
CHECK(frame_obj
->Set(context, v8_str("functionName"), frame->GetFunctionName())
.FromJust());
CHECK(frame_obj
->Set(context, v8_str("scriptName"),
frame->GetScriptNameOrSourceURL())
.FromJust());
// scriptName can be empty in special conditions e.g. eval
auto scriptName = frame->GetScriptNameOrSourceURL();
if (scriptName.IsEmpty()) {
scriptName = v8_str("<unknown>");
}
CHECK(
frame_obj->Set(context, v8_str("scriptName"), scriptName).FromJust());
CHECK(frame_obj
->Set(context, v8_str("isEval"),
v8::Boolean::New(isolate, frame->IsEval()))
Expand Down
15 changes: 15 additions & 0 deletions libdeno/libdeno_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,21 @@ TEST(LibDenoTest, LastException) {
deno_delete(d);
}

TEST(LibDenoTest, EncodeErrorBug) {
Deno* d = deno_new(deno_config{0, empty, empty, nullptr, nullptr});
EXPECT_EQ(deno_last_exception(d), nullptr);
EXPECT_FALSE(deno_execute(d, nullptr, "a.js", "eval('a')"));
EXPECT_STREQ(deno_last_exception(d),
"{\"message\":\"ReferenceError: a is not defined\","
"\"frames\":[{\"line\":1,\"column\":1,"
"\"functionName\":\"\",\"scriptName\":\"<unknown>\","
"\"isEval\":true,"
"\"isConstructor\":false,\"isWasm\":false},{\"line\":1,"
"\"column\":1,\"functionName\":\"\",\"scriptName\":\"a.js\","
"\"isEval\":false,\"isConstructor\":false,\"isWasm\":false}]}");
deno_delete(d);
}

TEST(LibDenoTest, Shared) {
uint8_t s[] = {0, 1, 2};
deno_buf shared = {nullptr, 0, s, 3};
Expand Down

0 comments on commit d835c84

Please sign in to comment.