Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1381541

Browse filesBrowse files
mertcanaltinaduh95
authored andcommitted
util: fix Latin1 decoding to return string output
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>
1 parent 1d8cc61 commit 1381541
Copy full SHA for 1381541

File tree

Expand file treeCollapse file tree

3 files changed

+44
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+44
-4
lines changed
Open diff view settings
Collapse file

‎src/encoding_binding.cc‎

Copy file name to clipboardExpand all lines: src/encoding_binding.cc
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,11 @@ void BindingData::DecodeLatin1(const FunctionCallbackInfo<Value>& args) {
286286
env->isolate(), "The encoded data was not valid for encoding latin1");
287287
}
288288

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

294296
} // namespace encoding_binding
Collapse file

‎test/cctest/test_encoding_binding.cc‎

Copy file name to clipboardExpand all lines: test/cctest/test_encoding_binding.cc
+22-1Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ bool RunDecodeLatin1(Environment* env,
2626
return false;
2727
}
2828

29-
*result = try_catch.Exception();
29+
*result = args[0];
3030
return true;
3131
}
3232

@@ -151,5 +151,26 @@ TEST_F(EncodingBindingTest, DecodeLatin1_BOMPresent) {
151151
EXPECT_STREQ(*utf8_result, "Áéó");
152152
}
153153

154+
TEST_F(EncodingBindingTest, DecodeLatin1_ReturnsString) {
155+
Environment* env = CreateEnvironment();
156+
Isolate* isolate = env->isolate();
157+
HandleScope handle_scope(isolate);
158+
159+
const uint8_t latin1_data[] = {0xC1, 0xE9, 0xF3};
160+
Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, sizeof(latin1_data));
161+
memcpy(ab->GetBackingStore()->Data(), latin1_data, sizeof(latin1_data));
162+
163+
Local<Uint8Array> array = Uint8Array::New(ab, 0, sizeof(latin1_data));
164+
Local<Value> args[] = {array};
165+
166+
Local<Value> result;
167+
ASSERT_TRUE(RunDecodeLatin1(env, args, false, false, &result));
168+
169+
ASSERT_TRUE(result->IsString());
170+
171+
String::Utf8Value utf8_result(isolate, result);
172+
EXPECT_STREQ(*utf8_result, "Áéó");
173+
}
174+
154175
} // namespace encoding_binding
155176
} // namespace node
Collapse file
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
const test = require('node:test');
6+
const assert = require('node:assert');
7+
8+
test('TextDecoder correctly decodes windows-1252 encoded data', { skip: !common.hasIntl }, () => {
9+
const latin1Bytes = new Uint8Array([0xc1, 0xe9, 0xf3]);
10+
11+
const expectedString = 'Áéó';
12+
13+
const decoder = new TextDecoder('windows-1252');
14+
const decodedString = decoder.decode(latin1Bytes);
15+
16+
assert.strictEqual(decodedString, expectedString);
17+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.