szn

Bug 458 — Cmd.parse leaks current token when arg_list.append fails

458. Owned token orphaned between toOwnedSlice and append

Severity: LOW Status: ✅ FIXED Files: src/cmd/cmd.zig


Root Cause Analysis

In src/cmd/cmd.zig:1831-1832:

const token = try token_buf.toOwnedSlice(allocator);
try arg_list.append(allocator, token);

Outer errdefer:1765 frees arg_list.items only. If append OOMs, token is owned but not in the list and token_buf was consumed — nothing frees it. Also lookup failure paths (UnknownCommand/MissingArgs) rely on that same errdefer, which is correct for listed tokens.

Proposed Fix

const token = try token_buf.toOwnedSlice(allocator);
arg_list.append(allocator, token) catch |e| { allocator.free(token); return e; };