From 385d440694a95bc4bfee25daa602c5c4363b6423 Mon Sep 17 00:00:00 2001 From: Ai Hoshino Date: Thu, 10 Aug 2023 00:45:50 +0800 Subject: Fix constructing buffer from a UTF16 string with the Latin1 encoding. (#4086) Close: #3914 --- src/bun.js/webcore/encoding.zig | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/bun.js/webcore/encoding.zig b/src/bun.js/webcore/encoding.zig index 4fa0f4ca2..9b1de838c 100644 --- a/src/bun.js/webcore/encoding.zig +++ b/src/bun.js/webcore/encoding.zig @@ -808,7 +808,8 @@ pub const Encoder = struct { .utf16le => constructFromU16(input, len, .utf16le), .ucs2 => constructFromU16(input, len, .utf16le), .utf8 => constructFromU16(input, len, .utf8), - .ascii => constructFromU16(input, len, .utf8), + .ascii => constructFromU16(input, len, .ascii), + .latin1 => constructFromU16(input, len, .latin1), else => unreachable, }; return JSC.JSValue.createBuffer(globalObject, slice, globalObject.bunVM().allocator); @@ -1170,12 +1171,7 @@ pub const Encoder = struct { }, .latin1, .buffer, .ascii => { var to = allocator.alloc(u8, len) catch return &[_]u8{}; - var input_bytes = std.mem.sliceAsBytes(input[0..len]); - @memcpy(to[0..input_bytes.len], input_bytes); - for (to[0..len], 0..) |c, i| { - to[i] = @as(u8, @as(u7, @truncate(c))); - } - + strings.copyU16IntoU8(to[0..len], []const u16, input[0..len]); return to; }, // string is already encoded, just need to copy the data -- cgit v1.2.3