Merge pull request #576 from Techatrix/fix-memory-leak

Fix memory leaks
This commit is contained in:
Auguste Rame 2022-08-09 00:27:34 +02:00 committed by GitHub
commit 8847ed54f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,6 +28,8 @@ fn loop(server: *Server) !void {
return; return;
}; };
const buffer = try server.allocator.alloc(u8, headers.content_length); const buffer = try server.allocator.alloc(u8, headers.content_length);
defer server.allocator.free(buffer);
try reader.readNoEof(buffer); try reader.readNoEof(buffer);
var writer = std.io.getStdOut().writer(); var writer = std.io.getStdOut().writer();
@ -41,8 +43,8 @@ const ConfigWithPath = struct {
config_path: ?[]const u8, config_path: ?[]const u8,
}; };
fn getConfig(allocator: std.mem.Allocator, config_path: ?[]const u8) !?ConfigWithPath { fn getConfig(allocator: std.mem.Allocator, config: ConfigWithPath) !ConfigWithPath {
if (config_path) |path| { if (config.config_path) |path| {
if (Config.loadFromFile(allocator, path)) |conf| { if (Config.loadFromFile(allocator, path)) |conf| {
return ConfigWithPath{ return ConfigWithPath{
.config = conf, .config = conf,
@ -74,7 +76,10 @@ fn getConfig(allocator: std.mem.Allocator, config_path: ?[]const u8) !?ConfigWit
} }
} }
return null; return ConfigWithPath{
.config = Config{},
.config_path = null,
};
} }
const stack_frames = switch (zig_builtin.mode) { const stack_frames = switch (zig_builtin.mode) {
@ -91,8 +96,11 @@ pub fn main() anyerror!void {
allocator = tracy.tracyAllocator(allocator).allocator(); allocator = tracy.tracyAllocator(allocator).allocator();
} }
var config_path: ?[]const u8 = null; var config = ConfigWithPath{
defer if (config_path) |path| allocator.free(path); .config = undefined,
.config_path = null,
};
defer if (config.config_path) |path| allocator.free(path);
// Check arguments. // Check arguments.
var args_it = try std.process.ArgIterator.initWithAllocator(allocator); var args_it = try std.process.ArgIterator.initWithAllocator(allocator);
@ -109,7 +117,7 @@ pub fn main() anyerror!void {
std.debug.print("Expected configuration file path after --config-path argument\n", .{}); std.debug.print("Expected configuration file path after --config-path argument\n", .{});
std.os.exit(1); std.os.exit(1);
}; };
config_path = try allocator.dupe(u8, path); config.config_path = try allocator.dupe(u8, path);
} else if (std.mem.eql(u8, arg, "config") or std.mem.eql(u8, arg, "configure")) { } else if (std.mem.eql(u8, arg, "config") or std.mem.eql(u8, arg, "configure")) {
try setup.wizard(allocator); try setup.wizard(allocator);
return; return;
@ -119,21 +127,15 @@ pub fn main() anyerror!void {
} }
} }
var new_config = blk: { config = try getConfig(allocator, config);
if (try getConfig(allocator, config_path)) |config| { if (config.config_path == null) {
break :blk config;
}
logger.info("No config file zls.json found.", .{}); logger.info("No config file zls.json found.", .{});
break :blk ConfigWithPath{ }
.config = Config{},
.config_path = null,
};
};
var server = try Server.init( var server = try Server.init(
allocator, allocator,
new_config.config, config.config,
new_config.config_path, config.config_path,
actual_log_level, actual_log_level,
); );
defer server.deinit(); defer server.deinit();