Skip to content

Commit 2f6d741

Browse files
targosUlisesGascon
authored andcommittedDec 20, 2023
src: iterate on import attributes array correctly
The array's length is supposed to be a multiple of two for dynamic import callbacks. Fixes: #50700 PR-URL: #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 b3041a0 commit 2f6d741

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
@@ -254,10 +254,14 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
254254
}
255255

256256
static Local<Object> createImportAttributesContainer(
257-
Realm* realm, Isolate* isolate, Local<FixedArray> raw_attributes) {
257+
Realm* realm,
258+
Isolate* isolate,
259+
Local<FixedArray> raw_attributes,
260+
const int elements_per_attribute) {
261+
CHECK_EQ(raw_attributes->Length() % elements_per_attribute, 0);
258262
Local<Object> attributes =
259263
Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
260-
for (int i = 0; i < raw_attributes->Length(); i += 3) {
264+
for (int i = 0; i < raw_attributes->Length(); i += elements_per_attribute) {
261265
attributes
262266
->Set(realm->context(),
263267
raw_attributes->Get(realm->context(), i).As<String>(),
@@ -303,7 +307,7 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
303307

304308
Local<FixedArray> raw_attributes = module_request->GetImportAssertions();
305309
Local<Object> attributes =
306-
createImportAttributesContainer(realm, isolate, raw_attributes);
310+
createImportAttributesContainer(realm, isolate, raw_attributes, 3);
307311

308312
Local<Value> argv[] = {
309313
specifier,
@@ -587,7 +591,7 @@ static MaybeLocal<Promise> ImportModuleDynamically(
587591
}
588592

589593
Local<Object> attributes =
590-
createImportAttributesContainer(realm, isolate, import_attributes);
594+
createImportAttributesContainer(realm, isolate, import_attributes, 2);
591595

592596
Local<Value> import_args[] = {
593597
id,

‎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)