From 13815417c72644f0567ae300af24f67dafe7714c Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Sat, 14 Dec 2024 21:09:49 +0300 Subject: [PATCH] util: fix Latin1 decoding to return string output PR-URL: https://github.com/nodejs/node/pull/56222 Fixes: https://github.com/nodejs/node/issues/56219 Reviewed-By: Antoine du Hamel Reviewed-By: Ruben Bridgewater Reviewed-By: Daniel Lemire Reviewed-By: Yagiz Nizipli Reviewed-By: James M Snell --- src/encoding_binding.cc | 8 +++++--- test/cctest/test_encoding_binding.cc | 23 ++++++++++++++++++++++- test/parallel/test-util-text-decoder.js | 17 +++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 test/parallel/test-util-text-decoder.js diff --git a/src/encoding_binding.cc b/src/encoding_binding.cc index a132eeb62306c6..885a0d072312e9 100644 --- a/src/encoding_binding.cc +++ b/src/encoding_binding.cc @@ -286,9 +286,11 @@ void BindingData::DecodeLatin1(const FunctionCallbackInfo& args) { env->isolate(), "The encoded data was not valid for encoding latin1"); } - Local buffer_result = - node::Buffer::Copy(env, result.c_str(), written).ToLocalChecked(); - args.GetReturnValue().Set(buffer_result); + Local output = + String::NewFromUtf8( + env->isolate(), result.c_str(), v8::NewStringType::kNormal, written) + .ToLocalChecked(); + args.GetReturnValue().Set(output); } } // namespace encoding_binding diff --git a/test/cctest/test_encoding_binding.cc b/test/cctest/test_encoding_binding.cc index 06cc36d8f6ae34..d5d14c60fedf7e 100644 --- a/test/cctest/test_encoding_binding.cc +++ b/test/cctest/test_encoding_binding.cc @@ -26,7 +26,7 @@ bool RunDecodeLatin1(Environment* env, return false; } - *result = try_catch.Exception(); + *result = args[0]; return true; } @@ -151,5 +151,26 @@ TEST_F(EncodingBindingTest, DecodeLatin1_BOMPresent) { EXPECT_STREQ(*utf8_result, "Áéó"); } +TEST_F(EncodingBindingTest, DecodeLatin1_ReturnsString) { + Environment* env = CreateEnvironment(); + Isolate* isolate = env->isolate(); + HandleScope handle_scope(isolate); + + const uint8_t latin1_data[] = {0xC1, 0xE9, 0xF3}; + Local ab = ArrayBuffer::New(isolate, sizeof(latin1_data)); + memcpy(ab->GetBackingStore()->Data(), latin1_data, sizeof(latin1_data)); + + Local array = Uint8Array::New(ab, 0, sizeof(latin1_data)); + Local args[] = {array}; + + Local result; + ASSERT_TRUE(RunDecodeLatin1(env, args, false, false, &result)); + + ASSERT_TRUE(result->IsString()); + + String::Utf8Value utf8_result(isolate, result); + EXPECT_STREQ(*utf8_result, "Áéó"); +} + } // namespace encoding_binding } // namespace node diff --git a/test/parallel/test-util-text-decoder.js b/test/parallel/test-util-text-decoder.js new file mode 100644 index 00000000000000..0f6d0463f9da48 --- /dev/null +++ b/test/parallel/test-util-text-decoder.js @@ -0,0 +1,17 @@ +'use strict'; + +const common = require('../common'); + +const test = require('node:test'); +const assert = require('node:assert'); + +test('TextDecoder correctly decodes windows-1252 encoded data', { skip: !common.hasIntl }, () => { + const latin1Bytes = new Uint8Array([0xc1, 0xe9, 0xf3]); + + const expectedString = 'Áéó'; + + const decoder = new TextDecoder('windows-1252'); + const decodedString = decoder.decode(latin1Bytes); + + assert.strictEqual(decodedString, expectedString); +});