layla 2026-06-03 14:35:18 +08:00 committed by GitHub
commit aea1ae08eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 22 additions and 5 deletions

View File

@ -68,11 +68,6 @@ pub const Parser = struct {
// we're in a broken state then we'd have already deinited the buffer.
if (self.state == .broken) return null;
if (self.buffer.written().len >= self.max_bytes) {
self.broken();
return error.OutOfMemory;
}
switch (self.state) {
// Drop because we're in a broken state.
.broken => return null,
@ -139,6 +134,14 @@ pub const Parser = struct {
},
}
// Check the limit only here, where the byte is actually stored. A
// line-ending byte that completes a line returns early above without
// being buffered, so it must not be rejected at exactly max_bytes.
if (self.buffer.written().len >= self.max_bytes) {
self.broken();
return error.OutOfMemory;
}
self.buffer.writer.writeByte(byte) catch |err| switch (err) {
error.WriteFailed => return error.OutOfMemory,
};
@ -748,6 +751,20 @@ test "tmux sessions-changed" {
try testing.expect(n == .sessions_changed);
}
test "tmux exact max_bytes notification still parses" {
const testing = std.testing;
const alloc = testing.allocator;
const line = "%sessions-changed";
var c: Parser = .{ .buffer = .init(alloc), .max_bytes = line.len };
defer c.deinit();
for (line) |byte| try testing.expect(try c.put(byte) == null);
const n = (try c.put('\n')).?;
try testing.expect(n == .sessions_changed);
}
test "tmux sessions-changed carriage return" {
const testing = std.testing;
const alloc = testing.allocator;