Merge pull request #1045 from Techatrix/memory-lifetime

fix leak in `openDocument`
This commit is contained in:
Techatrix 2023-03-08 21:31:11 +00:00 committed by GitHub
commit b8eb6aab7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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);