Skip to content

index: AnyTrigramIndex.candidates mmap_overlay ArrayList may leak on alloc failure #251

@justrach

Description

@justrach

Problem

In the mmap_overlay branch of AnyTrigramIndex.candidates, a result ArrayList is created with ensureTotalCapacity. If toOwnedSlice fails (OOM), the ArrayList's backing buffer is not freed — there is no defer result.deinit(allocator) before the break :blk null fallback:

// src/index.zig — AnyTrigramIndex.candidates, mmap_overlay branch
var result: std.ArrayList([]const u8) = .{};
result.ensureTotalCapacity(allocator, merged.count()) catch break :blk null;
while (it.next()) |k| result.appendAssumeCapacity(k.*);
break :blk result.toOwnedSlice(allocator) catch null; // ← leaks result.items on OOM

The leak is proportional to merged.count() (can be large for common trigrams) and accumulates on every search under memory pressure.

Fix

Add defer result.deinit(allocator); before the ensureTotalCapacity line, and replace catch null with a proper errdefer or restructure to only call toOwnedSlice when allocation is guaranteed to succeed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions