Fixed memory leak caused by DocumentStore.resolveImports

This commit is contained in:
Alexandros Naskos 2021-03-30 21:18:46 +03:00
parent 2415e7ca6d
commit 40edfadc26
No known key found for this signature in database
GPG Key ID: 02BF2E72B0EA32D2

View File

@ -532,15 +532,13 @@ pub fn uriFromImportStr(
} }
pub fn resolveImport(self: *DocumentStore, handle: *Handle, import_str: []const u8) !?*Handle { pub fn resolveImport(self: *DocumentStore, handle: *Handle, import_str: []const u8) !?*Handle {
std.debug.print("RESOLVING IMPORT STR: {s}\n", .{import_str});
const allocator = self.allocator; const allocator = self.allocator;
const final_uri = (try self.uriFromImportStr( const final_uri = (try self.uriFromImportStr(
self.allocator, self.allocator,
handle.*, handle.*,
import_str, import_str,
)) orelse return null; )) orelse return null;
var consumed_final_uri = false; defer allocator.free(final_uri);
defer if (!consumed_final_uri) allocator.free(final_uri);
for (handle.imports_used.items) |uri| { for (handle.imports_used.items) |uri| {
if (std.mem.eql(u8, uri, final_uri)) { if (std.mem.eql(u8, uri, final_uri)) {
@ -548,14 +546,19 @@ pub fn resolveImport(self: *DocumentStore, handle: *Handle, import_str: []const
} }
} }
// The URI must be somewhere in the import_uris
const handle_uri = for (handle.import_uris) |uri| {
if (std.mem.eql(u8, uri, final_uri)) {
break uri;
}
} else return null;
// New import. // New import.
// Check if the import is already opened by others. // Check if the import is already opened by others.
if (self.getHandle(final_uri)) |new_handle| { if (self.getHandle(final_uri)) |new_handle| {
// If it is, append it to our imports, increment the count, set our new handle // If it is, append it to our imports, increment the count, set our new handle
// and return the parsed tree root node. // and return the parsed tree root node.
try handle.imports_used.append(self.allocator, final_uri); try handle.imports_used.append(self.allocator, handle_uri);
consumed_final_uri = true;
new_handle.count += 1; new_handle.count += 1;
return new_handle; return new_handle;
} }
@ -582,9 +585,7 @@ pub fn resolveImport(self: *DocumentStore, handle: *Handle, import_str: []const
}; };
// Add to import table of current handle. // Add to import table of current handle.
try handle.imports_used.append(self.allocator, final_uri); try handle.imports_used.append(self.allocator, handle_uri);
consumed_final_uri = true;
// Swap handles. // Swap handles.
// This takes ownership of the passed uri and text. // This takes ownership of the passed uri and text.
const duped_final_uri = try std.mem.dupe(allocator, u8, final_uri); const duped_final_uri = try std.mem.dupe(allocator, u8, final_uri);