From 613215efec1efa2bc8b6eba4445f4be5979f7630 Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Sun, 18 Dec 2022 06:21:04 -0800 Subject: [PATCH] Fix memory leaks in integration tests --- src/api/lib.zig | 8 ++++++-- src/api/services/auth.zig | 3 ++- tests/api_integration/lib.zig | 16 +++++++++++++--- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/api/lib.zig b/src/api/lib.zig index f153140..0b37618 100644 --- a/src/api/lib.zig +++ b/src/api/lib.zig @@ -322,6 +322,7 @@ fn ApiConn(comptime DbConn: type) type { pub fn close(self: *Self) void { util.deepFree(self.allocator, self.community); + if (self.token_info) |info| util.deepFree(self.allocator, info); self.db.releaseConnection(); } @@ -353,11 +354,14 @@ fn ApiConn(comptime DbConn: type) type { const user = try services.actors.get(self.db, info.user_id, self.allocator); defer util.deepFree(self.allocator, user); + const username = try util.deepClone(self.allocator, user.username); + errdefer util.deepFree(self.allocator, username); + return AuthorizationInfo{ .id = user.id, - .username = try util.deepClone(self.allocator, user.username), + .username = username, .community_id = self.community.id, - .host = self.community.host, + .host = try util.deepClone(self.allocator, self.community.host), .issued_at = info.issued_at, }; diff --git a/src/api/services/auth.zig b/src/api/services/auth.zig index 88e0f20..4307cab 100644 --- a/src/api/services/auth.zig +++ b/src/api/services/auth.zig @@ -104,7 +104,7 @@ pub fn login( error.NoRows => error.InvalidLogin, else => error.DatabaseFailure, }; - errdefer util.deepFree(alloc, info); + defer alloc.free(info.hash); try verifyPassword(info.hash, password, alloc); @@ -162,6 +162,7 @@ pub fn verifyToken( alloc: std.mem.Allocator, ) VerifyTokenError!TokenInfo { const hash = try hashToken(token, alloc); + defer alloc.free(hash); return db.queryRow( TokenInfo, diff --git a/tests/api_integration/lib.zig b/tests/api_integration/lib.zig index 5f2184a..b06813b 100644 --- a/tests/api_integration/lib.zig +++ b/tests/api_integration/lib.zig @@ -39,7 +39,9 @@ fn connectAndLogin( var conn = try api_source.connectUnauthorized(host, alloc); defer conn.close(); - return try util.deepClone(alloc, try conn.login(username, password)); + const result = try conn.login(username, password); + defer util.deepFree(conn.allocator, result); + return try util.deepClone(alloc, result); } test "login as root" { @@ -54,6 +56,7 @@ test "login as root" { var conn = try src.connectToken(admin_host, login.token, alloc); defer conn.close(); const auth = try conn.verifyAuthorization(); + defer util.deepFree(conn.allocator, auth); try std.testing.expectEqual(login.user_id, auth.id); try std.testing.expectEqualStrings(root_user, auth.username); @@ -74,6 +77,7 @@ test "create community" { const host = "fedi.example.com"; const community = try conn.createCommunity("https://" ++ host, null); + defer util.deepFree(alloc, community); try std.testing.expectEqual(api.Community.Scheme.https, community.scheme); try std.testing.expectEqual(api.Community.Kind.local, community.kind); @@ -83,7 +87,10 @@ test "create community" { } test "create community and transfer to new owner" { - const alloc = std.testing.allocator; + //const alloc = std.testing.allocator; + var gpa = std.heap.GeneralPurposeAllocator(.{ .stack_trace_frames = 16 }){}; + defer _ = gpa.deinit(); + const alloc = gpa.allocator(); var db = try makeDb(alloc); defer db.deinit(); var src = try ApiSource.init(&db); @@ -97,7 +104,9 @@ test "create community and transfer to new owner" { defer conn.close(); const community = try conn.createCommunity("https://" ++ host, null); + defer util.deepFree(conn.allocator, community); const invite = try conn.createInvite(.{ .to_community = community.id, .kind = .community_owner }); + defer util.deepFree(conn.allocator, invite); break :blk try util.deepClone(alloc, invite); }; defer util.deepFree(alloc, invite); @@ -108,7 +117,7 @@ test "create community and transfer to new owner" { var conn = try src.connectUnauthorized(host, alloc); defer conn.close(); - _ = try conn.register(username, password, .{ .invite_code = invite.code }); + util.deepFree(conn.allocator, try conn.register(username, password, .{ .invite_code = invite.code })); } const login = try connectAndLogin(&src, host, username, password, alloc); @@ -118,6 +127,7 @@ test "create community and transfer to new owner" { defer conn.close(); const auth = try conn.verifyAuthorization(); + defer util.deepFree(conn.allocator, auth); try std.testing.expectEqual(login.user_id, auth.id); try std.testing.expectEqualStrings(username, auth.username);