From 044b2e3e8fc7a21e77afd44da76395c835601e4d Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Tue, 27 Aug 2024 19:45:48 +0200 Subject: [PATCH] fix memory leak and remove 'add' --- src/main.zig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.zig b/src/main.zig index 422c6ca..3171aa4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -120,6 +120,8 @@ fn configLocation(allocator: Allocator) ![]const u8 { return config_location; } +/// Reads the config file into a list of OtpAuthUrl. +/// The caller is responsible to free the memory. fn read_config(allocator: Allocator, config_location: []const u8) !ArrayList(OtpAuthUrl) { const file = try std.fs.cwd().openFile(config_location, .{}); defer file.close(); @@ -142,6 +144,7 @@ fn executeGetList(allocator: Allocator, config_location: []const u8) !std.ArrayL var names = std.ArrayList([]const u8).init(allocator); const authenticators = try read_config(allocator, config_location); + defer authenticators.deinit(); for (0..authenticators.items.len) |i| { try names.append(authenticators.items[i].name); @@ -162,6 +165,7 @@ test "read list of entries" { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); + std.fs.cwd().deleteTree("test-tmp") catch unreachable; try std.fs.cwd().makeDir("test-tmp"); const file = try std.fs.cwd().createFile("test-tmp/zig-totp", .{ .read = true }); defer {