kjloveless / zish

simple shell in zig
1 stars 1 forks source link

zish_split_line returns an OwnedSlice that never gets freed #2

Closed kjloveless closed 3 months ago

kjloveless commented 3 months ago
fn zish_split_line(allocator: Allocator, line: []const u8) ![][]const u8 {
    var tokens = std.ArrayList([]const u8).init(allocator);
    defer tokens.deinit();

    var it = std.mem.splitAny(u8, line, ZISH_TOKEN_DELIMITER);
    while (it.next()) |token| {
      if (token.len > 0) {
        try tokens.append(token);
      }
    }
    return tokens.toOwnedSlice();
}

zish_split_line returns memory to the caller, in this case in:

fn zish_loop() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();

    var args: [][]const u8 = undefined;
    const status: bool = true;

    while (status) {
        _ = try io.getStdOut().writer().write("> ");
        const line = try zish_read_line(allocator);
        args = try zish_split_line(allocator, line);
        _ = try zish_execute(allocator, args);
    }
    return;
}

but is never being freed by the allocator.