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; if (handle.tree.errors.len != 0) return null;
const formatted = try handle.tree.render(server.allocator); const allocator = server.arena.allocator();
defer server.allocator.free(formatted);
const formatted = try handle.tree.render(allocator);
if (std.mem.eql(u8, handle.text, formatted)) return null; if (std.mem.eql(u8, handle.text, formatted)) return null;
// avoid computing diffs if the output is small // 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 { const edits = maybe_edits orelse {
// if edits have been computed we replace the entire file with the formatted text // 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` // 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| { for (edits.items) |edit| {
text_edits.appendAssumeCapacity(.{ text_edits.appendAssumeCapacity(.{
.range = edit.range, .range = edit.range,