Merge pull request #595 from nullptrdevs/master

fix: fix memory leaks related to updating config variables
This commit is contained in:
Lee Cannon 2022-08-23 15:04:30 +01:00 committed by GitHub
commit 7c245f7dd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 17 deletions

View File

@ -200,22 +200,18 @@ pub fn configChanged(config: *Config, allocator: std.mem.Allocator, builtin_crea
config.builtin_path = try std.fs.path.join(allocator, &.{ builtin_creation_dir.?, "builtin.zig" }); config.builtin_path = try std.fs.path.join(allocator, &.{ builtin_creation_dir.?, "builtin.zig" });
} }
config.build_runner_path = if (config.build_runner_path) |p| if (null == config.build_runner_path) {
try allocator.dupe(u8, p)
else blk: {
var exe_dir_bytes: [std.fs.MAX_PATH_BYTES]u8 = undefined; var exe_dir_bytes: [std.fs.MAX_PATH_BYTES]u8 = undefined;
const exe_dir_path = try std.fs.selfExeDirPath(&exe_dir_bytes); const exe_dir_path = try std.fs.selfExeDirPath(&exe_dir_bytes);
break :blk try std.fs.path.resolve(allocator, &[_][]const u8{ exe_dir_path, "build_runner.zig" }); config.build_runner_path = try std.fs.path.resolve(allocator, &[_][]const u8{ exe_dir_path, "build_runner.zig" });
}; }
config.global_cache_path = if (config.global_cache_path) |p| if (null == config.global_cache_path) {
try allocator.dupe(u8, p)
else blk: {
const cache_dir_path = (try known_folders.getPath(allocator, .cache)) orelse { const cache_dir_path = (try known_folders.getPath(allocator, .cache)) orelse {
logger.warn("Known-folders could not fetch the cache path", .{}); logger.warn("Known-folders could not fetch the cache path", .{});
return; return;
}; };
defer allocator.free(cache_dir_path); defer allocator.free(cache_dir_path);
break :blk try std.fs.path.resolve(allocator, &[_][]const u8{ cache_dir_path, "zls" }); config.global_cache_path = try std.fs.path.resolve(allocator, &[_][]const u8{ cache_dir_path, "zls" });
}; }
} }

View File

@ -2440,7 +2440,11 @@ pub fn processJsonRpc(server: *Server, writer: anytype, json: []const u8) !void
if (value != .Null) { if (value != .Null) {
const new_value: field.field_type = switch (ft) { const new_value: field.field_type = switch (ft) {
[]const u8 => switch (value) { []const u8 => switch (value) {
.String => |s| try server.allocator.dupe(u8, s), // TODO: Allocation model? (same with didChangeConfiguration); imo this isn't *that* bad but still .String => |s| blk: {
var nv = try server.allocator.dupe(u8, s);
if (@field(server.config, field.name)) |prev_val| server.allocator.free(prev_val);
break :blk nv;
}, // TODO: Allocation model? (same with didChangeConfiguration); imo this isn't *that* bad but still
else => @panic("Invalid configuration value"), // TODO: Handle this else => @panic("Invalid configuration value"), // TODO: Handle this
}, },
else => switch (ti) { else => switch (ti) {

View File

@ -2543,16 +2543,20 @@ fn makeInnerScope(allocator: std.mem.Allocator, context: ScopeContext, node_idx:
if (container_field) |_| { if (container_field) |_| {
if (!std.mem.eql(u8, name, "_")) { if (!std.mem.eql(u8, name, "_")) {
try context.enums.put(allocator, .{ var doc = if (try getDocComments(allocator, tree, decl, .Markdown)) |docs|
types.MarkupContent{ .kind = .Markdown, .value = docs }
else
null;
var gop_res = try context.enums.getOrPut(allocator, .{
.label = name, .label = name,
.kind = .Constant, .kind = .Constant,
.insertText = name, .insertText = name,
.insertTextFormat = .PlainText, .insertTextFormat = .PlainText,
.documentation = if (try getDocComments(allocator, tree, decl, .Markdown)) |docs| .documentation = doc
types.MarkupContent{ .kind = .Markdown, .value = docs } });
else if (gop_res.found_existing) {
null, if (doc) |d| allocator.free(d.value);
}, {}); }
} }
} }
} }