From 0a30bf0212ed32a5230ec6fe9ae20fb10e52c4e4 Mon Sep 17 00:00:00 2001 From: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Date: Sat, 3 Dec 2022 02:59:17 -0800 Subject: [PATCH] make sure to not use incomplete characters (#1575) --- src/bun.js/webcore/encoding.zig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/bun.js/webcore/encoding.zig b/src/bun.js/webcore/encoding.zig index a3b350899c..c3444c71f1 100644 --- a/src/bun.js/webcore/encoding.zig +++ b/src/bun.js/webcore/encoding.zig @@ -885,7 +885,10 @@ pub const Encoder = struct { return ZigString.init(input).toValueGC(global); }, .ucs2, .utf16le => { - var output = allocator.alloc(u16, @maximum(len / 2, 1)) catch return ZigString.init("Out of memory").toErrorInstance(global); + // Avoid incomplete characters + if (len / 2 == 0) return ZigString.Empty.toValue(global); + + var output = allocator.alloc(u16, len / 2) catch return ZigString.init("Out of memory").toErrorInstance(global); var output_bytes = std.mem.sliceAsBytes(output); output_bytes[output_bytes.len - 1] = 0;