From 079fbca2c2fbd467b42e9fa3b892ce3e05eb2bef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Mon, 13 Nov 2023 11:49:17 +0100 Subject: [PATCH] src: iterate on import attributes array correctly The array's length is supposed to be a multiple of two. Fixes: https://github.com/nodejs/node/issues/50700 --- src/module_wrap.cc | 3 ++- test/es-module/test-esm-import-attributes-errors.js | 5 +++++ test/es-module/test-esm-import-attributes-errors.mjs | 5 +++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 1d61fe5f0522ba..17d21afa69d032 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -253,7 +253,8 @@ static Local createImportAttributesContainer( Environment* env, Isolate* isolate, Local raw_attributes) { Local attributes = Object::New(isolate, v8::Null(env->isolate()), nullptr, nullptr, 0); - for (int i = 0; i < raw_attributes->Length(); i += 3) { + CHECK_EQ(raw_attributes->Length() % 2, 0); + for (int i = 0; i < raw_attributes->Length(); i += 2) { attributes ->Set(env->context(), raw_attributes->Get(env->context(), i).As(), diff --git a/test/es-module/test-esm-import-attributes-errors.js b/test/es-module/test-esm-import-attributes-errors.js index 243277fdcd7d29..fcf7fa4f797be2 100644 --- a/test/es-module/test-esm-import-attributes-errors.js +++ b/test/es-module/test-esm-import-attributes-errors.js @@ -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_UNSUPPORTED' } + ); + await rejects( import(jsModuleDataUrl, { with: { type: 'unsupported' } }), { code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' } diff --git a/test/es-module/test-esm-import-attributes-errors.mjs b/test/es-module/test-esm-import-attributes-errors.mjs index c29cc459326c4b..887a1b97cc26ee 100644 --- a/test/es-module/test-esm-import-attributes-errors.mjs +++ b/test/es-module/test-esm-import-attributes-errors.mjs @@ -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' }