fix leak in openDocument

This commit is contained in:
Techatrix 2023-03-08 21:52:26 +01:00
parent 42a8d1adb7
commit ed91fd3823

View File

@ -141,7 +141,7 @@ fn getOrLoadHandleInternal(self: *DocumentStore, uri: Uri) !?*const Handle {
return gop.value_ptr.*; return gop.value_ptr.*;
} }
/// Takes ownership of `new_text` which has to be allocated /// Takes ownership of `text` which has to be allocated
/// with this DocumentStore's allocator /// with this DocumentStore's allocator
pub fn openDocument(self: *DocumentStore, uri: Uri, text: [:0]const u8) error{OutOfMemory}!Handle { pub fn openDocument(self: *DocumentStore, uri: Uri, text: [:0]const u8) error{OutOfMemory}!Handle {
const tracy_zone = tracy.trace(@src()); const tracy_zone = tracy.trace(@src());
@ -153,10 +153,14 @@ pub fn openDocument(self: *DocumentStore, uri: Uri, text: [:0]const u8) error{Ou
} else { } else {
handle.open = true; handle.open = true;
} }
self.allocator.free(text);
return handle.*; return handle.*;
} }
var handle = try self.allocator.create(Handle); var handle = self.allocator.create(Handle) catch |err| {
self.allocator.free(text);
return err;
};
errdefer self.allocator.destroy(handle); errdefer self.allocator.destroy(handle);
handle.* = try self.createDocument(uri, text, true); handle.* = try self.createDocument(uri, text, true);