Skip to content

Commit

Permalink
The ResponseIterator now has a deinit function for freeing the respon…
Browse files Browse the repository at this point in the history
…se message
  • Loading branch information
r4gus committed Aug 9, 2023
1 parent 8988b4f commit dcc14fd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cks/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub const CKS = struct {
rand: std.rand.Random,
time: *const fn () i64,
) !@This() {
if (!std.mem.eql(u8, "SECRET", raw[0..6])) {
if (raw.len < 10 or !std.mem.eql(u8, "SECRET", raw[0..6])) {
std.log.err("Unexpected magic number", .{});
return error.UnknownFileFormat;
}
Expand Down
11 changes: 5 additions & 6 deletions lib/ctap/transports/ctaphid/authenticator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,9 @@ pub fn handle(packet: []const u8, auth: anytype) ?CtapHidMessageIterator {
var response = resp.CtapHidMessageIterator.new(S.busy.?, S.cmd.?);

if (S.data[1] == 3) {
var d = auth.allocator.alloc(u8, 10) catch unreachable;
@memcpy(d, "FIDO_2_0\x69\x86");
response.data = d;
return resp.iterator(S.busy.?, S.cmd.?, "CTAP2/U2F_V2\x90\x00");
} else {
var d = auth.allocator.alloc(u8, 2) catch unreachable;
@memcpy(d, "\x69\x86");
response.data = d;
return resp.iterator(S.busy.?, S.cmd.?, "\x69\x86");
}

return response;
Expand All @@ -293,11 +289,14 @@ pub fn handle(packet: []const u8, auth: anytype) ?CtapHidMessageIterator {
switch (data) {
.ok => |d| {
response.data = d;
response.allocator = auth.allocator;
},
.err => |e| {
// `data` is freed within handle()
var d = auth.allocator.alloc(u8, 1) catch unreachable;
d[0] = e;
response.data = d;
response.allocator = auth.allocator;
},
}

Expand Down
7 changes: 7 additions & 0 deletions lib/ctap/transports/ctaphid/message.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub const CtapHidMessageIterator = struct {
seq: misc.Seq = 0,
buffer: [PACKET_SIZE]u8 = undefined,
data: []const u8 = &.{},
allocator: ?std.mem.Allocator = null,
cid: misc.Cid,
cmd: command.Cmd,

Expand All @@ -51,6 +52,12 @@ pub const CtapHidMessageIterator = struct {
};
}

pub fn deinit(self: *const @This()) void {
if (self.allocator) |a| {
a.free(self.data);
}
}

/// Get the next data packet.
///
/// Returns `null` if all data bytes have been processed.
Expand Down
10 changes: 9 additions & 1 deletion platform-auth/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ var uv: ?bool = null;

var notification: [*c]notify.NotifyNotification = undefined;

//const la = std.heap.LoggingAllocator(.debug, .debug);
//var gpa = std.heap.GeneralPurposeAllocator(.{}){};
//var lagpa = la.init(gpa.allocator());
//var allocator = lagpa.allocator();

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var allocator = gpa.allocator();
const allocator = gpa.allocator();

fn accept_callback(
n: [*c]notify.NotifyNotification,
Expand Down Expand Up @@ -85,6 +90,9 @@ fn packet_callback(user_data: notify.gpointer) callconv(.C) notify.gboolean {
);

if (response) |*resp| {
// Free the response data at the end
defer resp.deinit();

while (resp.next()) |packet| {
var rev = std.mem.zeroes(uhid.uhid_event);
rev.type = uhid.UHID_INPUT;
Expand Down

0 comments on commit dcc14fd

Please sign in to comment.