Skip to content

Commit eacf4ba

Browse files
targosRafaelGSS
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: #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 c8d4cd6 commit eacf4ba

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎src/module_wrap.cc‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,14 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
255255
}
256256

257257
static Local<Object> createImportAttributesContainer(
258-
Realm* realm, Isolate* isolate, Local<FixedArray> raw_attributes) {
258+
Realm* realm,
259+
Isolate* isolate,
260+
Local<FixedArray> raw_attributes,
261+
constint elements_per_attribute) {
262+
CHECK_EQ(raw_attributes->Length() % elements_per_attribute, 0);
259263
Local<Object> attributes =
260264
Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
261-
for (int i = 0; i < raw_attributes->Length(); i += 3) {
265+
for (int i = 0; i < raw_attributes->Length(); i += elements_per_attribute) {
262266
attributes
263267
->Set(realm->context(),
264268
raw_attributes->Get(realm->context(), i).As<String>(),
@@ -304,7 +308,7 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
304308

305309
Local<FixedArray> raw_attributes = module_request->GetImportAssertions();
306310
Local<Object> attributes =
307-
createImportAttributesContainer(realm, isolate, raw_attributes);
311+
createImportAttributesContainer(realm, isolate, raw_attributes, 3);
308312

309313
Local<Value> argv[] = {
310314
specifier,
@@ -588,7 +592,7 @@ static MaybeLocal<Promise> ImportModuleDynamically(
588592
}
589593

590594
Local<Object> attributes =
591-
createImportAttributesContainer(realm, isolate, import_attributes);
595+
createImportAttributesContainer(realm, isolate, import_attributes, 2);
592596

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ async function test() {
2626
{code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE'}
2727
);
2828

29+
awaitrejects(
30+
import(jsModuleDataUrl,{with: {type: 'json',other: 'unsupported'}}),
31+
{code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE'}
32+
);
33+
2934
awaitrejects(
3035
import(jsModuleDataUrl,{with: {type: 'unsupported'}}),
3136
{code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED'}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ await rejects(
2121
{code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE'}
2222
);
2323

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

0 commit comments

Comments
 (0)