Skip to content

Commit

Permalink
util: fix Latin1 decoding to return string output
Browse files Browse the repository at this point in the history
PR-URL: #56222
Fixes: #56219
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
mertcanaltin authored and aduh95 committed Dec 18, 2024
1 parent 1d8cc61 commit 1381541
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
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, "Áéó");
}

} // 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);
});

0 comments on commit 1381541

Please sign in to comment.