Skip to content

Commit

Permalink
Avoid crashes on ES module resolution when module not found (#1546)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo authored and ry committed Jan 18, 2019
1 parent 315e4ab commit f9b167d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
8 changes: 7 additions & 1 deletion libdeno/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,11 @@ v8::MaybeLocal<v8::Module> ResolveCallback(v8::Local<v8::Context> context,

if (d->resolve_module_.IsEmpty()) {
// Resolution Error.
isolate->ThrowException(v8_str("module resolution error"));
std::stringstream err_ss;
err_ss << "NotFound: Cannot resolve module \"" << specifier_c
<< "\" from \"" << referrer_filename << "\"";
auto resolve_error = v8_str(err_ss.str().c_str());
isolate->ThrowException(resolve_error);
return v8::MaybeLocal<v8::Module>();
} else {
auto module = d->resolve_module_.Get(isolate);
Expand Down Expand Up @@ -612,6 +616,8 @@ bool ExecuteMod(v8::Local<v8::Context> context, const char* js_filename,
auto module = maybe_module.ToLocalChecked();
auto maybe_ok = module->InstantiateModule(context, ResolveCallback);
if (maybe_ok.IsNothing()) {
DCHECK(try_catch.HasCaught());
HandleException(context, try_catch.Exception());
return false;
}

Expand Down
11 changes: 9 additions & 2 deletions src/isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,15 @@ extern "C" fn resolve_cb(
debug!("module_resolve callback {} {}", specifier, referrer);
let isolate = unsafe { Isolate::from_raw_ptr(user_data) };

let out =
code_fetch_and_maybe_compile(&isolate.state, specifier, referrer).unwrap();
let maybe_out =
code_fetch_and_maybe_compile(&isolate.state, specifier, referrer);

if maybe_out.is_err() {
// Resolution failure
return;
}

let out = maybe_out.unwrap();

let filename = CString::new(out.filename.clone()).unwrap();
let filename_ptr = filename.as_ptr() as *const i8;
Expand Down
1 change: 1 addition & 0 deletions tests/error_009_missing_js_module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./bad-module.js";
1 change: 1 addition & 0 deletions tests/error_009_missing_js_module.js.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NotFound: Cannot resolve module "./bad-module.js" from "[WILDCARD]/tests/error_009_missing_js_module.js"
4 changes: 4 additions & 0 deletions tests/error_009_missing_js_module.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
args: tests/error_009_missing_js_module.js
check_stderr: true
exit_code: 1
output: tests/error_009_missing_js_module.js.out

0 comments on commit f9b167d

Please sign in to comment.