From cd7da9eb4b56053096ec211373f6f57497876261 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 3 Jun 2024 18:40:52 -0700 Subject: [PATCH] inspector: fix encoding of pty-encoded key event --- src/Surface.zig | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index fb267b50c..39c9b460e 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -1432,17 +1432,25 @@ pub fn keyCallback( break :req try termio.Message.WriteReq.init(self.alloc, seq); }; + // Copy the encoded data into the inspector event if we have one. + // We do this before the mailbox because the IO thread could + // release the memory before we get a chance to copy it. + if (insp_ev) |*ev| pty: { + const slice = write_req.slice(); + const copy = self.alloc.alloc(u8, slice.len) catch |err| { + log.warn("error allocating pty data for inspector err={}", .{err}); + break :pty; + }; + errdefer self.alloc.free(copy); + @memcpy(copy, slice); + ev.pty = copy; + } + _ = self.io_thread.mailbox.push(switch (write_req) { .small => |v| .{ .write_small = v }, .stable => |v| .{ .write_stable = v }, .alloc => |v| .{ .write_alloc = v }, }, .{ .forever = {} }); - if (insp_ev) |*ev| { - ev.pty = self.alloc.dupe(u8, write_req.slice()) catch |err| err: { - log.warn("error copying pty data for inspector err={}", .{err}); - break :err ""; - }; - } try self.io_thread.wakeup.notify(); // If our event is any keypress that isn't a modifier and we generated