aboutsummaryrefslogtreecommitdiff
path: root/src/proto/authentication_request.zig
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-09-28 10:54:58 +0100
committerMartin Ashby <martin@ashbysoft.com>2023-09-28 10:54:58 +0100
commit35494bc81b59165ee9264cd1004bb05a120279a3 (patch)
tree8d11946f8fc20e1f254301100e860f28bade5ee0 /src/proto/authentication_request.zig
parent0c063d42430077881e563120ebfcf92c2cecf463 (diff)
downloadpgz-35494bc81b59165ee9264cd1004bb05a120279a3.tar.gz
pgz-35494bc81b59165ee9264cd1004bb05a120279a3.tar.bz2
pgz-35494bc81b59165ee9264cd1004bb05a120279a3.tar.xz
pgz-35494bc81b59165ee9264cd1004bb05a120279a3.zip
Reduce allocations, the message takes ownership of the bytes read from
the stream and is responsible for deallocating, rather than copying them to their own storage.
Diffstat (limited to 'src/proto/authentication_request.zig')
-rw-r--r--src/proto/authentication_request.zig11
1 files changed, 6 insertions, 5 deletions
diff --git a/src/proto/authentication_request.zig b/src/proto/authentication_request.zig
index 3ea5cd1..c4bddb5 100644
--- a/src/proto/authentication_request.zig
+++ b/src/proto/authentication_request.zig
@@ -25,12 +25,14 @@ pub const AuthRequestCleartextPassword = struct {};
inner_type: InnerAuthRequestType,
inner: InnerAuthRequest,
-pub fn read(_: std.mem.Allocator, b: []const u8) !AuthenticationRequest {
- if (b.len != 4) {
- log.err("invalid message length, expected 4 got {}", .{b.len});
+// takes ownership of b
+pub fn read(a: std.mem.Allocator, buf: []const u8) !AuthenticationRequest {
+ defer a.free(buf); // No need to retain it we copy all the interesting data out
+ if (buf.len != 4) {
+ log.err("invalid message length, expected 4 got {}", .{buf.len});
return ProtocolError.InvalidMessageLength;
}
- const auth_type_int = std.mem.readIntBig(u32, b[0..4]);
+ const auth_type_int = std.mem.readIntBig(u32, buf[0..4]);
const inner_type = enum_from_int(InnerAuthRequestType, auth_type_int) orelse {
log.err("Unsupported auth type {}", .{auth_type_int});
return ClientError.UnsupportedAuthType;
@@ -71,7 +73,6 @@ test "round trip" {
try std.testing.expectEqual(Tag, tag);
const len = try reader.readIntBig(u32);
const buf = try allocator.alloc(u8, len - 4);
- defer allocator.free(buf);
try reader.readNoEof(buf);
var sm2 = try AuthenticationRequest.read(allocator, buf);
defer sm2.deinit(allocator);