From e9b364772df065b292522da0831177a42e60d31a Mon Sep 17 00:00:00 2001 From: Techatrix <19954306+Techatrix@users.noreply.github.com> Date: Sat, 7 Jan 2023 21:33:10 +0000 Subject: [PATCH] fix returning freed memory in formattingHandler (#890) --- src/Server.zig | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Server.zig b/src/Server.zig index 728f4f5..0887253 100644 --- a/src/Server.zig +++ b/src/Server.zig @@ -2222,13 +2222,14 @@ fn formattingHandler(server: *Server, request: types.DocumentFormattingParams) E if (handle.tree.errors.len != 0) return null; - const formatted = try handle.tree.render(server.allocator); - defer server.allocator.free(formatted); + const allocator = server.arena.allocator(); + + const formatted = try handle.tree.render(allocator); if (std.mem.eql(u8, handle.text, formatted)) return null; // avoid computing diffs if the output is small - const maybe_edits = if (formatted.len <= 512) null else diff.edits(server.arena.allocator(), handle.text, formatted) catch null; + const maybe_edits = if (formatted.len <= 512) null else diff.edits(allocator, handle.text, formatted) catch null; const edits = maybe_edits orelse { // if edits have been computed we replace the entire file with the formatted text @@ -2239,7 +2240,7 @@ fn formattingHandler(server: *Server, request: types.DocumentFormattingParams) E }; // Convert from `[]diff.Edit` to `[]types.TextEdit` - var text_edits = try std.ArrayListUnmanaged(types.TextEdit).initCapacity(server.arena.allocator(), edits.items.len); + var text_edits = try std.ArrayListUnmanaged(types.TextEdit).initCapacity(allocator, edits.items.len); for (edits.items) |edit| { text_edits.appendAssumeCapacity(.{ .range = edit.range,