fix(node:buffer): fix Buffer.write stuck (#6651)

This commit is contained in:
Ai Hoshino
2023-10-23 04:42:05 +08:00
committed by Jarred Sumner
parent d92f3b6610
commit 8e1eae7dae
2 changed files with 32 additions and 10 deletions

View File

@@ -2079,12 +2079,16 @@ pub fn copyLatin1IntoUTF8StopOnNonASCII(buf_: []u8, comptime Type: type, latin1_
}
}
if (latin1.len > 0 and buf.len >= 2) {
if (comptime stop) return .{ .written = std.math.maxInt(u32), .read = std.math.maxInt(u32) };
if (latin1.len > 0) {
if (buf.len >= 2) {
if (comptime stop) return .{ .written = std.math.maxInt(u32), .read = std.math.maxInt(u32) };
buf[0..2].* = latin1ToCodepointBytesAssumeNotASCII(latin1[0]);
latin1 = latin1[1..];
buf = buf[2..];
buf[0..2].* = latin1ToCodepointBytesAssumeNotASCII(latin1[0]);
latin1 = latin1[1..];
buf = buf[2..];
} else {
break;
}
}
}

View File

@@ -211,11 +211,29 @@ it("only top level parent propagates from a non-pooled instance", () => {
});
it("UTF-8 write() & slice()", () => {
const testValue = "\u00F6\u65E5\u672C\u8A9E"; // ö日本語
const buffer = Buffer.allocUnsafe(32);
const size = buffer.write(testValue, 0, "utf8");
const slice = buffer.toString("utf8", 0, size);
expect(slice).toBe(testValue);
{
const testValue = "\u00F6\u65E5\u672C\u8A9E"; // ö日本語
const buffer = Buffer.allocUnsafe(32);
const size = buffer.write(testValue, 0, "utf8");
const slice = buffer.toString("utf8", 0, size);
expect(slice).toBe(testValue);
}
{
const buffer = Buffer.allocUnsafe(1);
buffer.write("\x61");
buffer.write("\xFF");
expect(buffer).toStrictEqual(Buffer.from([0x61]));
}
{
const buffer = Buffer.alloc(5);
buffer.write("\x61\xFF\x62\xFF\x63", "utf8");
expect(buffer).toStrictEqual(Buffer.from([0x61, 0xc3, 0xbf, 0x62, 0x00]));
}
{
const buffer = Buffer.alloc(5);
buffer.write("\xFF\x61\xFF\x62\xFF", "utf8");
expect(buffer).toStrictEqual(Buffer.from([0xc3, 0xbf, 0x61, 0xc3, 0xbf]));
}
});
it("triple slice", () => {