diff --git a/lib/DB.zig b/lib/DB.zig index d27c97c..53d3413 100644 --- a/lib/DB.zig +++ b/lib/DB.zig @@ -400,14 +400,20 @@ fn additionalGids( try compress.appendUvarint(&blob, 0); var scratch = try allocator.alloc(u32, 256); - defer allocator.free(scratch); + var scratch_allocated: bool = true; + defer if (scratch_allocated) allocator.free(scratch); for (corpus.user2groups) |usergroups, user_idx| { if (usergroups.len == 0) { idx2offset[user_idx] = 0; continue; } idx2offset[user_idx] = blob.items.len; - scratch = try allocator.realloc(scratch, usergroups.len); + if (scratch.len < usergroups.len) { + allocator.free(scratch); + scratch_allocated = false; + scratch = try allocator.alloc(u32, usergroups.len); + scratch_allocated = true; + } scratch.len = usergroups.len; const corpusGids = corpus.groups.items(.gid); for (usergroups) |group_idx, i|