From 0069e28cc6f681797f1424317f46d52da9d9e635 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 20 Apr 2026 08:56:40 -0700 Subject: [PATCH] libghostty: expose the APC max byte limits --- include/ghostty/vt/terminal.h | 19 +++++++++++++++++++ src/terminal/c/terminal.zig | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/ghostty/vt/terminal.h b/include/ghostty/vt/terminal.h index 433308c84..0e6d048e1 100644 --- a/include/ghostty/vt/terminal.h +++ b/include/ghostty/vt/terminal.h @@ -573,6 +573,25 @@ typedef enum GHOSTTY_ENUM_TYPED { * Input type: bool* */ GHOSTTY_TERMINAL_OPT_KITTY_IMAGE_MEDIUM_SHARED_MEM = 18, + + /** + * Set the maximum bytes the APC handler will buffer for all protocols. + * This prevents malicious input from causing unbounded memory allocation. + * A NULL value pointer removes all overrides, reverting to the built-in + * defaults. + * + * Input type: size_t* + */ + GHOSTTY_TERMINAL_OPT_APC_MAX_BYTES = 19, + + /** + * Set the maximum bytes the APC handler will buffer for Kitty graphics + * protocol data. A NULL value pointer removes the override, reverting + * to the built-in default. + * + * Input type: size_t* + */ + GHOSTTY_TERMINAL_OPT_APC_MAX_BYTES_KITTY = 20, GHOSTTY_TERMINAL_OPT_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE, } GhosttyTerminalOption; diff --git a/src/terminal/c/terminal.zig b/src/terminal/c/terminal.zig index 0931b1685..670c2aea3 100644 --- a/src/terminal/c/terminal.zig +++ b/src/terminal/c/terminal.zig @@ -7,6 +7,7 @@ const ZigTerminal = @import("../Terminal.zig"); const Stream = @import("../stream_terminal.zig").Stream; const ScreenSet = @import("../ScreenSet.zig"); const PageList = @import("../PageList.zig"); +const apc = @import("../apc.zig"); const kitty = @import("../kitty/key.zig"); const kitty_gfx_c = @import("kitty_graphics.zig"); const modes = @import("../modes.zig"); @@ -310,6 +311,8 @@ pub const Option = enum(c_int) { kitty_image_medium_file = 16, kitty_image_medium_temp_file = 17, kitty_image_medium_shared_mem = 18, + apc_max_bytes = 19, + apc_max_bytes_kitty = 20, /// Input type expected for setting the option. pub fn InType(comptime self: Option) type { @@ -331,6 +334,7 @@ pub const Option = enum(c_int) { .kitty_image_medium_temp_file, .kitty_image_medium_shared_mem, => ?*const bool, + .apc_max_bytes, .apc_max_bytes_kitty => ?*const usize, }; } }; @@ -425,6 +429,19 @@ fn setTyped( } } }, + .apc_max_bytes => { + wrapper.stream.handler.apc_handler.max_bytes = if (value) |ptr| + .initFull(ptr.*) + else + .{}; + }, + .apc_max_bytes_kitty => { + if (value) |ptr| { + wrapper.stream.handler.apc_handler.max_bytes.put(.kitty, ptr.*); + } else { + wrapper.stream.handler.apc_handler.max_bytes.remove(.kitty); + } + }, } return .success; }