From 2c5ae262f7b98aaf9652dca6b554e204845e9c9e Mon Sep 17 00:00:00 2001 From: nullptrdevs <16590917+nullptrdevs@users.noreply.github.com> Date: Sun, 21 Aug 2022 17:11:50 -0700 Subject: [PATCH 1/3] fix: fix memory leaks related to updating config variables adds an edge case in analysis.makeScopeInternal to prevent leaking memory when adding duplicate container fields w/ name "other" --- src/Config.zig | 16 ++++++---------- src/Server.zig | 6 +++++- src/analysis.zig | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Config.zig b/src/Config.zig index 440a4b3..e54c479 100644 --- a/src/Config.zig +++ b/src/Config.zig @@ -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" }); + } } diff --git a/src/Server.zig b/src/Server.zig index 7923767..e42348b 100644 --- a/src/Server.zig +++ b/src/Server.zig @@ -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).?); + 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) { diff --git a/src/analysis.zig b/src/analysis.zig index ea81bc0..cad359a 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -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")) { try context.enums.put(allocator, .{ .label = name, .kind = .Constant, From 4d0ab8b1aa4c66fd8a945a96d4cb6ded0c2b09b0 Mon Sep 17 00:00:00 2001 From: nullptrdevs <16590917+nullptrdevs@users.noreply.github.com> Date: Mon, 22 Aug 2022 10:49:15 -0700 Subject: [PATCH 2/3] Update Server.zig --- src/Server.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Server.zig b/src/Server.zig index e42348b..c38fe89 100644 --- a/src/Server.zig +++ b/src/Server.zig @@ -2442,7 +2442,7 @@ pub fn processJsonRpc(server: *Server, writer: anytype, json: []const u8) !void []const u8 => switch (value) { .String => |s| blk: { var nv = try server.allocator.dupe(u8, s); - server.allocator.free(@field(server.config, field.name).?); + 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 From 015332b69e2cc6a073c33ca85acff2f587665c86 Mon Sep 17 00:00:00 2001 From: nullptrdevs <16590917+nullptrdevs@users.noreply.github.com> Date: Mon, 22 Aug 2022 10:54:29 -0700 Subject: [PATCH 3/3] Update analysis.zig --- src/analysis.zig | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/analysis.zig b/src/analysis.zig index cad359a..540f52c 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -2542,17 +2542,21 @@ fn makeInnerScope(allocator: std.mem.Allocator, context: ScopeContext, node_idx: }; if (container_field) |_| { - if (!std.mem.eql(u8, name, "_") and !std.mem.eql(u8, name, "other")) { - try context.enums.put(allocator, .{ + if (!std.mem.eql(u8, name, "_")) { + 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, .kind = .Constant, .insertText = name, .insertTextFormat = .PlainText, - .documentation = if (try getDocComments(allocator, tree, decl, .Markdown)) |docs| - types.MarkupContent{ .kind = .Markdown, .value = docs } - else - null, - }, {}); + .documentation = doc + }); + if (gop_res.found_existing) { + if (doc) |d| allocator.free(d.value); + } } } }