From 8f5fd2f06c92fe080817e249a3c313212b753c46 Mon Sep 17 00:00:00 2001 From: Kohei Ueno Date: Fri, 29 Jul 2022 14:47:56 +0900 Subject: [PATCH] src: fix to use replacement character PR-URL: https://github.com/nodejs/node/pull/43999 Fixes: https://github.com/nodejs/node/issues/43962 Reviewed-By: Antoine du Hamel Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Darshan Sen Reviewed-By: LiviaMedeiros Reviewed-By: Feng Yu --- src/node_i18n.cc | 6 +++++- test/parallel/test-whatwg-encoding-custom-textdecoder.js | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/node_i18n.cc b/src/node_i18n.cc index c537a247f55ff0..d96efaa9df7b2f 100644 --- a/src/node_i18n.cc +++ b/src/node_i18n.cc @@ -425,7 +425,11 @@ void ConverterObject::Create(const FunctionCallbackInfo& args) { nullptr, nullptr, nullptr, &status); } - new ConverterObject(env, obj, conv, flags); + auto converter = new ConverterObject(env, obj, conv, flags); + size_t sublen = ucnv_getMinCharSize(conv); + std::string sub(sublen, '?'); + converter->set_subst_chars(sub.c_str()); + args.GetReturnValue().Set(obj); } diff --git a/test/parallel/test-whatwg-encoding-custom-textdecoder.js b/test/parallel/test-whatwg-encoding-custom-textdecoder.js index 1fa65164c70678..fe08edc597d3f4 100644 --- a/test/parallel/test-whatwg-encoding-custom-textdecoder.js +++ b/test/parallel/test-whatwg-encoding-custom-textdecoder.js @@ -199,3 +199,10 @@ if (common.hasIntl) { const str = decoder.decode(chunk); assert.strictEqual(str, 'foo\ufffd'); } + +if (common.hasIntl) { + const decoder = new TextDecoder('Shift_JIS'); + const chunk = new Uint8Array([-1]); + const str = decoder.decode(chunk); + assert.strictEqual(str, '\ufffd'); +}