Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: fix Latin1 decoding to return string output #56222

Merged
merged 7 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/encoding_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,11 @@ void BindingData::DecodeLatin1(const FunctionCallbackInfo<Value>& args) {
env->isolate(), "The encoded data was not valid for encoding latin1");
}

Local<Object> buffer_result =
node::Buffer::Copy(env, result.c_str(), written).ToLocalChecked();
args.GetReturnValue().Set(buffer_result);
Local<String> output =
String::NewFromUtf8(
env->isolate(), result.c_str(), v8::NewStringType::kNormal, written)
.ToLocalChecked();
args.GetReturnValue().Set(output);
}

} // namespace encoding_binding
Expand Down
23 changes: 22 additions & 1 deletion test/cctest/test_encoding_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bool RunDecodeLatin1(Environment* env,
return false;
}

*result = try_catch.Exception();
*result = args[0];
return true;
}

Expand Down Expand Up @@ -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<ArrayBuffer> ab = ArrayBuffer::New(isolate, sizeof(latin1_data));
memcpy(ab->GetBackingStore()->Data(), latin1_data, sizeof(latin1_data));

Local<Uint8Array> array = Uint8Array::New(ab, 0, sizeof(latin1_data));
Local<Value> args[] = {array};

Local<Value> result;
ASSERT_TRUE(RunDecodeLatin1(env, args, false, false, &result));

ASSERT_TRUE(result->IsString());

String::Utf8Value utf8_result(isolate, result);
EXPECT_STREQ(*utf8_result, "Áéó");
}
aduh95 marked this conversation as resolved.
Show resolved Hide resolved

} // namespace encoding_binding
} // namespace node
17 changes: 17 additions & 0 deletions test/parallel/test-util-text-decoder.js
Original file line number Diff line number Diff line change
@@ -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);
});
Loading