src: handle empty MaybeLocal in cjs_lexer::Parse

CreateString() and Parse() in node_cjs_lexer.cc unconditionally called
ToLocalChecked() on the results of String::NewFromOneByte(),
String::NewFromUtf8() and Set::Add(). If string or handle allocation
fails or an exception is pending on the isolate, these return an empty
MaybeLocal and ToLocalChecked() aborts the process with
"FATAL ERROR: v8::ToLocalChecked Empty MaybeLocal".

Since Parse() is on the hot path of every ESM import of a CJS module
(cjsPreparseModuleExports), propagate the failure as a regular pending
JavaScript exception instead so callers can recover.

Fixes: https://github.com/nodejs/node/issues/63323
Refs: https://github.com/nodejs/node/pull/61456
Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
PR-URL: https://github.com/nodejs/node/pull/63885
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Yagiz Nizipli authored and GitHub committed 2026-06-15 01:30:52 +00:00
1 parent 120c2a4558
commit aa25a0a30c
1 file changed
+14 -7
+14 -7
View File
@@ -15,6 +15,7 @@ using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::LocalVector;
using v8::MaybeLocal;
using v8::NewStringType;
using v8::Object;
using v8::Set;
@@ -23,22 +24,20 @@ using v8::Value;
// Create a V8 string from an export_string variant, using fast path for ASCII
template <typename T>
inline Local<String> CreateString(Isolate* isolate, const T& str) {
inline MaybeLocal<String> CreateString(Isolate* isolate, const T& str) {
std::string_view sv = lexer::get_string_view(str);
if (simdutf::validate_ascii(sv.data(), sv.size())) {
return String::NewFromOneByte(isolate,
reinterpret_cast<const uint8_t*>(sv.data()),
NewStringType::kInternalized,
static_cast<int>(sv.size()))
.ToLocalChecked();
static_cast<int>(sv.size()));
}
return String::NewFromUtf8(isolate,
sv.data(),
NewStringType::kInternalized,
static_cast<int>(sv.size()))
.ToLocalChecked();
static_cast<int>(sv.size()));
}
void Parse(const FunctionCallbackInfo<Value>& args) {
@@ -71,14 +70,22 @@ void Parse(const FunctionCallbackInfo<Value>& args) {
// Convert exports to JS Set
Local<Set> exports_set = Set::New(isolate);
for (const auto& exp : analysis.exports) {
exports_set->Add(context, CreateString(isolate, exp)).ToLocalChecked();
Local<String> exp_str;
if (!CreateString(isolate, exp).ToLocal(&exp_str) ||
exports_set->Add(context, exp_str).IsEmpty()) {
return;
}
}
// Convert reexports to JS array using batch creation
LocalVector<Value> reexports_vec(isolate);
reexports_vec.reserve(analysis.re_exports.size());
for (const auto& reexp : analysis.re_exports) {
reexports_vec.push_back(CreateString(isolate, reexp));
Local<String> reexp_str;
if (!CreateString(isolate, reexp).ToLocal(&reexp_str)) {
return;
}
reexports_vec.push_back(reexp_str);
}
// Create result array [exports (Set), reexports (Array)]