fix assert() crash (#1941)

This commit is contained in:
Alex Lam S.L
2023-01-30 22:20:29 +02:00
committed by GitHub
parent c11bb93552
commit ec2c16fefa
2 changed files with 9 additions and 1 deletions

View File

@@ -1419,7 +1419,7 @@ pub fn convertUTF8BytesIntoUTF16(sequence: *const [4]u8) UTF16Replacement {
switch (len) {
2 => {
if (Environment.allow_assert)
assert(sequence[0] >= 0xC2);
assert(sequence[0] >= 0xC0);
if (Environment.allow_assert)
assert(sequence[0] <= 0xDF);
if (sequence[1] < 0x80 or sequence[1] > 0xBF) {

View File

@@ -2813,3 +2813,11 @@ test("Buffer.byteLength", () => {
);
}
});
it("should not crash on invalid UTF-8 byte sequence", () => {
const buf = Buffer.from([0xC0, 0xFD]).toString();
expect(buf.length).toBe(2);
const str = buf.toString();
expect(str.length).toBe(2);
expect(str).toBe("\uFFFD\uFFFD");
});