Fix crash when napi_register_module_v1 returns nullptr (#16816)

This commit is contained in:
190n
2025-01-27 20:13:22 -08:00
committed by GitHub
parent f8cbb32343
commit 71eb1476db
8 changed files with 326 additions and 9 deletions

View File

@@ -438,6 +438,13 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionDlopen, (JSC::JSGlobalObject * globalOb
EncodedJSValue exportsValue = JSC::JSValue::encode(exports);
JSC::JSValue resultValue = JSValue::decode(napi_register_module_v1(globalObject, exportsValue));
RETURN_IF_EXCEPTION(scope, {});
// If a module returns `nullptr` (cast to a napi_value) from its register function, we should
// use the `exports` value (which may have had properties added to it) as the return value of
// `require()`.
if (resultValue.isEmpty()) {
resultValue = exports;
}
if (auto resultObject = resultValue.getObject()) {
#if OS(DARWIN) || OS(LINUX)
@@ -454,11 +461,10 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionDlopen, (JSC::JSGlobalObject * globalOb
Bun::NapiExternal* napi_external = Bun::NapiExternal::create(vm, globalObject->NapiExternalStructure(), meta, nullptr, nullptr);
bool success = resultObject->putDirect(vm, WebCore::builtinNames(vm).napiDlopenHandlePrivateName(), napi_external, JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly);
ASSERT(success);
RETURN_IF_EXCEPTION(scope, {});
}
}
RETURN_IF_EXCEPTION(scope, {});
globalObject->m_pendingNapiModuleAndExports[0].clear();
globalObject->m_pendingNapiModuleAndExports[1].clear();
globalObject->m_pendingNapiModuleDlopenHandle = nullptr;