src: iterate on import attributes array correctly

The array's length is supposed to be a multiple of two for dynamic
import callbacks.

Fixes: https://github.com/nodejs/node/issues/50700
PR-URL: https://github.com/nodejs/node/pull/50703
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Shelley Vohr <shelley.vohr@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Michaël Zasso 2023-11-19 22:22:49 +01:00 committed by GitHub
parent 96890f6f33
commit 4db94c5023
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 4 deletions

View File

@ -255,10 +255,14 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
}
static Local<Object> createImportAttributesContainer(
Realm* realm, Isolate* isolate, Local<FixedArray> raw_attributes) {
Realm* realm,
Isolate* isolate,
Local<FixedArray> raw_attributes,
const int elements_per_attribute) {
CHECK_EQ(raw_attributes->Length() % elements_per_attribute, 0);
Local<Object> attributes =
Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
for (int i = 0; i < raw_attributes->Length(); i += 3) {
for (int i = 0; i < raw_attributes->Length(); i += elements_per_attribute) {
attributes
->Set(realm->context(),
raw_attributes->Get(realm->context(), i).As<String>(),
@ -304,7 +308,7 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
Local<FixedArray> raw_attributes = module_request->GetImportAssertions();
Local<Object> attributes =
createImportAttributesContainer(realm, isolate, raw_attributes);
createImportAttributesContainer(realm, isolate, raw_attributes, 3);
Local<Value> argv[] = {
specifier,
@ -588,7 +592,7 @@ static MaybeLocal<Promise> ImportModuleDynamically(
}
Local<Object> attributes =
createImportAttributesContainer(realm, isolate, import_attributes);
createImportAttributesContainer(realm, isolate, import_attributes, 2);
Local<Value> import_args[] = {
id,

View File

@ -26,6 +26,11 @@ async function test() {
{ code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' }
);
await rejects(
import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }),
{ code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' }
);
await rejects(
import(jsModuleDataUrl, { with: { type: 'unsupported' } }),
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }

View File

@ -21,6 +21,11 @@ await rejects(
{ code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' }
);
await rejects(
import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }),
{ code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' }
);
await rejects(
import(import.meta.url, { with: { type: 'unsupported' } }),
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }