Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix memory leaks related to updating config variables #595

Merged
merged 3 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/Config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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.build_runner_path = if (config.build_runner_path) |p|
try allocator.dupe(u8, p)
else blk: {
if (null == config.build_runner_path) {
var exe_dir_bytes: [std.fs.MAX_PATH_BYTES]u8 = undefined;
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|
try allocator.dupe(u8, p)
else blk: {
if (null == config.global_cache_path) {
const cache_dir_path = (try known_folders.getPath(allocator, .cache)) orelse {
logger.warn("Known-folders could not fetch the cache path", .{});
return;
};
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" });
}
}
6 changes: 5 additions & 1 deletion src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2440,7 +2440,11 @@ pub fn processJsonRpc(server: *Server, writer: anytype, json: []const u8) !void
if (value != .Null) {
const new_value: field.field_type = switch (ft) {
[]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);
server.allocator.free(@field(server.config, field.name).?);
llogick marked this conversation as resolved.
Show resolved Hide resolved
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 => switch (ti) {
Expand Down
2 changes: 1 addition & 1 deletion src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2542,7 +2542,7 @@ fn makeInnerScope(allocator: std.mem.Allocator, context: ScopeContext, node_idx:
};

if (container_field) |_| {
if (!std.mem.eql(u8, name, "_")) {
if (!std.mem.eql(u8, name, "_") and !std.mem.eql(u8, name, "other")) {
leecannon marked this conversation as resolved.
Show resolved Hide resolved
try context.enums.put(allocator, .{
.label = name,
.kind = .Constant,
Expand Down