fix memory leak and remove 'add'

This commit is contained in:
2024-08-27 19:45:48 +02:00
parent 8854646cda
commit 044b2e3e8f

View File

@@ -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 {