mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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 <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>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<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
test/parallel/test-util-text-decoder.js
Normal file
17
test/parallel/test-util-text-decoder.js
Normal file
@@ -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);
|
||||
});
|
||||
Reference in New Issue
Block a user