Skip to content

Commit cc239fe

Browse files
targosnicolo-ribaudo
authored andcommitted
src: iterate on import attributes array correctly
The array's length is supposed to be a multiple of two for dynamic import callbacks. Fixes: nodejs#50700 PR-URL: nodejs#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>
1 parent 9cb5138 commit cc239fe

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/module_wrap.cc

+8-4
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,14 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
251251
}
252252

253253
static Local<Object> createImportAttributesContainer(
254-
Environment* env, Isolate* isolate, Local<FixedArray> raw_attributes) {
254+
Environment* env,
255+
Isolate* isolate,
256+
Local<FixedArray> raw_attributes,
257+
const int elements_per_attribute) {
258+
CHECK_EQ(raw_attributes->Length() % elements_per_attribute, 0);
255259
Local<Object> attributes =
256260
Object::New(isolate, v8::Null(env->isolate()), nullptr, nullptr, 0);
257-
for (int i = 0; i < raw_attributes->Length(); i += 3) {
261+
for (int i = 0; i < raw_attributes->Length(); i += elements_per_attribute) {
258262
attributes
259263
->Set(env->context(),
260264
raw_attributes->Get(env->context(), i).As<String>(),
@@ -300,7 +304,7 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
300304

301305
Local<FixedArray> raw_attributes = module_request->GetImportAssertions();
302306
Local<Object> attributes =
303-
createImportAttributesContainer(env, isolate, raw_attributes);
307+
createImportAttributesContainer(env, isolate, raw_attributes, 3);
304308

305309
Local<Value> argv[] = {
306310
specifier,
@@ -603,7 +607,7 @@ static MaybeLocal<Promise> ImportModuleDynamically(
603607
}
604608

605609
Local<Object> attributes =
606-
createImportAttributesContainer(env, isolate, import_attributes);
610+
createImportAttributesContainer(env, isolate, import_attributes, 2);
607611

608612
Local<Value> import_args[] = {
609613
object,

test/es-module/test-esm-import-attributes-errors.js

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ async function test() {
2626
{ code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' }
2727
);
2828

29+
await rejects(
30+
import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }),
31+
{ code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' }
32+
);
33+
2934
await rejects(
3035
import(jsModuleDataUrl, { with: { type: 'unsupported' } }),
3136
{ code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' }

test/es-module/test-esm-import-attributes-errors.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ await rejects(
2121
{ code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' }
2222
);
2323

24+
await rejects(
25+
import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }),
26+
{ code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' }
27+
);
28+
2429
await rejects(
2530
import(import.meta.url, { with: { type: 'unsupported' } }),
2631
{ code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' }

0 commit comments

Comments
 (0)