fix returning freed memory in formattingHandler (#890)

This commit is contained in:
Techatrix 2023-01-07 21:33:10 +00:00 committed by GitHub
parent 54e7d1da8b
commit e9b364772d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,