fix memory leak and remove 'add'
This commit is contained in:
@@ -120,6 +120,8 @@ fn configLocation(allocator: Allocator) ![]const u8 {
|
|||||||
return config_location;
|
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) {
|
fn read_config(allocator: Allocator, config_location: []const u8) !ArrayList(OtpAuthUrl) {
|
||||||
const file = try std.fs.cwd().openFile(config_location, .{});
|
const file = try std.fs.cwd().openFile(config_location, .{});
|
||||||
defer file.close();
|
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);
|
var names = std.ArrayList([]const u8).init(allocator);
|
||||||
|
|
||||||
const authenticators = try read_config(allocator, config_location);
|
const authenticators = try read_config(allocator, config_location);
|
||||||
|
defer authenticators.deinit();
|
||||||
|
|
||||||
for (0..authenticators.items.len) |i| {
|
for (0..authenticators.items.len) |i| {
|
||||||
try names.append(authenticators.items[i].name);
|
try names.append(authenticators.items[i].name);
|
||||||
@@ -162,6 +165,7 @@ test "read list of entries" {
|
|||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
|
std.fs.cwd().deleteTree("test-tmp") catch unreachable;
|
||||||
try std.fs.cwd().makeDir("test-tmp");
|
try std.fs.cwd().makeDir("test-tmp");
|
||||||
const file = try std.fs.cwd().createFile("test-tmp/zig-totp", .{ .read = true });
|
const file = try std.fs.cwd().createFile("test-tmp/zig-totp", .{ .read = true });
|
||||||
defer {
|
defer {
|
||||||
|
|||||||
Reference in New Issue
Block a user