szn

Bug 59 — key.format — no bounds check on output buffer before writing

59. key.format — no bounds check on output buffer before writing

File: src/key.zig:215–301 Severity: LOW Status: ✅ FIXED — Added bounds and remaining capacity checks in format and prependModifiers to prevent buffer overflow.

pub fn format(key: Key, buf: []u8) []const u8 {
    ...
    // Modifier prefixes (up to 8 bytes: "C-M-S-M-")
    if (mod.ctrl) { buf[pos] = 'C'; buf[pos + 1] = '-'; pos += 2; }
    if (mod.alt)  { buf[pos] = 'M'; buf[pos + 1] = '-'; pos += 2; }
    if (mod.shift) { buf[pos] = 'S'; buf[pos + 1] = '-'; pos += 2; }
    if (mod.meta) { buf[pos] = 'M'; buf[pos + 1] = '-'; pos += 2; }
    ...
    @memcpy(buf[pos..][0..n.len], n);
    ...
    std.fmt.bufPrint(buf[pos..], "{u}", .{...});

Multiple @memcpy and bufPrint calls write into the buffer without checking remaining capacity against buf.len. If the caller passes a buffer too small (e.g. 2 bytes for a modifier-heavy key name), it’s a buffer overflow. All current call sites pass [64]u8 or [128]u8 which are safely oversized, but the function itself provides no protection.