szn

Bug 545 — ChooseMode retained buffers on exit and unbounded allocation in Server.loadConfigFile

545. Retained ChooseMode copies after exit and uncapped config file allocation

Severity: MEDIUM
Status: ✅ RESOLVED
Files: src/choose.zig:34-63, src/server/server.zig:1613-1676, src/server/server.zig:3695-3705

Problem

  1. Retained buffer copies in ChooseMode (src/choose.zig:34-63, src/server/server.zig:1613-1676): ChooseMode.enter deep-copies every buffer’s name, preview, and full data into pane.choose_mode.items. When the user selects an item or cancels (Enter, Escape, q) in Server.handleChooseModeKey, choose.zig only sets self.active = false without freeing self.items. Up to 50 paste buffers (~800 KiB) remain allocated per pane until pane.deinit() or the next choose_mode.enter().

  2. Unbounded ftell allocation in Server.loadConfigFile (src/server/server.zig:3695-3705):

    if (c.fseek(f, 0, c.SEEK_END) != 0) return;
    const size = c.ftell(f);
    if (size <= 0) return;
    if (c.fseek(f, 0, c.SEEK_SET) != 0) return;
    
    const buf = self.allocator.alloc(u8, @intCast(size)) catch return;
    
    • There is no upper bound on size (e.g., source-file /dev/zero, a huge file, or a directory where ftell returns INT64_MAX on some platforms attempts a multi-gigabyte allocation).
    • Non-seekable inputs such as process substitution (szn source-file <(echo "set -g mouse on")) fail fseek/ftell or report size <= 0.

Proposed Fix

  1. Add a ChooseMode.clear(self: *ChooseMode) method that frees self.items and call it after handleChooseModeKey finishes handling .select or .cancel.
  2. In Server.loadConfigFile, read in bounded chunks up to a reasonable maximum (e.g. MAX_CONFIG_FILE_SIZE = 4 * 1024 * 1024) rather than relying on fseek/ftell without a cap.