From 54fc5aa9e025c284d644f92ee72082412c91e48c Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Tue, 3 Jan 2023 18:18:16 +0100 Subject: [PATCH] Fix mem leaks in ipcd. --- zig-impl/src/context.zig | 16 +++++++++++++--- zig-impl/src/ipcd.zig | 11 ++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/zig-impl/src/context.zig b/zig-impl/src/context.zig index a6b8df2..727b369 100644 --- a/zig-impl/src/context.zig +++ b/zig-impl/src/context.zig @@ -119,7 +119,10 @@ pub const Context = struct { // IPC_NETWORK="audio https://example.com/audio ;pong tls://pong.example.com/pong" var network_envvar = std.process.getEnvVarOwned(allocator, "IPC_NETWORK") catch |err| switch(err) { // error{ OutOfMemory, EnvironmentVariableNotFound, InvalidUtf8 } (ErrorSet) - error.EnvironmentVariableNotFound => { return null; }, // no need to contact IPCd + error.EnvironmentVariableNotFound => { + print("no IPC_NETWORK envvar: IPCd won't be contacted\n", .{}); + return null; + }, // no need to contact IPCd else => { return err; }, }; @@ -232,8 +235,15 @@ pub const Context = struct { // Create a unix socket. // Store std lib structures in the context. // TODO: find better error name - pub fn server_init(self: *Self, path: [] const u8) !net.StreamServer { - // print("context server init {s}\n", .{path}); + pub fn server_init(self: *Self, service_name: [] const u8) !net.StreamServer { + var buffer: [1000]u8 = undefined; + var fbs = std.io.fixedBufferStream(&buffer); + var writer = fbs.writer(); + + try self.server_path(service_name, writer); + var path = fbs.getWritten(); + + print("context server init {s}\n", .{path}); var server = net.StreamServer.init(.{}); var socket_addr = try net.Address.initUnix(path); try server.listen(socket_addr); diff --git a/zig-impl/src/ipcd.zig b/zig-impl/src/ipcd.zig index fc05b82..9d0df1d 100644 --- a/zig-impl/src/ipcd.zig +++ b/zig-impl/src/ipcd.zig @@ -37,10 +37,8 @@ fn create_service() !void { var ctx = try ipc.Context.init(allocator); defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful? - const path = "/tmp/.TEST_USOCK"; - // SERVER SIDE: creating a service. - _ = try ctx.server_init(path); + _ = try ctx.server_init("ipcd"); // signal handler, to quit when asked const S = struct { @@ -132,16 +130,19 @@ fn create_service() !void { , allocator , "currently not implemented"); try ctx.write(response); + response.deinit(); + m.deinit(); } else { // There is a problem: ipcd was contacted without providing // a message, meaning there is nothing to do. This should be // explicitely warned about. - var m = try Message.init(some_event.origin + var response = try Message.init(some_event.origin , Message.Type.ERROR , allocator , "lookup message without data"); - try ctx.write(m); + try ctx.write(response); + response.deinit(); } },