diff --git a/src/User.zig b/src/User.zig index 910e236..a873599 100644 --- a/src/User.zig +++ b/src/User.zig @@ -24,22 +24,13 @@ pub fn clone( self: *const User, allocator: Allocator, ) error{OutOfMemory}!User { - const stringdata = try allocator.alloc(u8, self.strlen()); - const gecos_start = self.name.len; - const home_start = gecos_start + self.gecos.len; - const shell_start = home_start + self.home.len; - mem.copy(u8, stringdata[0..self.name.len], self.name); - mem.copy(u8, stringdata[gecos_start..], self.gecos); - mem.copy(u8, stringdata[home_start..], self.home); - mem.copy(u8, stringdata[shell_start..], self.shell); - return User{ .uid = self.uid, .gid = self.gid, - .name = stringdata[0..self.name.len], - .gecos = stringdata[gecos_start .. gecos_start + self.gecos.len], - .home = stringdata[home_start .. home_start + self.home.len], - .shell = stringdata[shell_start .. shell_start + self.shell.len], + .name = try allocator.dupe(u8, self.name), + .gecos = try allocator.dupe(u8, self.gecos), + .home = try allocator.dupe(u8, self.home), + .shell = try allocator.dupe(u8, self.shell), }; } @@ -115,8 +106,10 @@ pub fn strlenZ(self: *const User) usize { } pub fn deinit(self: *User, allocator: Allocator) void { - const slice = self.home.ptr[0..self.strlen()]; - allocator.free(slice); + allocator.free(self.name); + allocator.free(self.gecos); + allocator.free(self.home); + allocator.free(self.shell); self.* = undefined; } diff --git a/src/unix2db/main.zig b/src/unix2db/main.zig index 8c3a921..5547d15 100644 --- a/src/unix2db/main.zig +++ b/src/unix2db/main.zig @@ -73,8 +73,10 @@ fn execute( var users = try User.fromReader(allocator, passwdFile.reader()); defer for (users) |*user| user.deinit(allocator); + defer allocator.free(users); var groups = try Group.fromReader(allocator, groupFile.reader()); defer for (groups) |*group| group.deinit(allocator); + defer allocator.free(groups); var corpus = try Corpus.init(allocator, users, groups); defer corpus.deinit();