authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2023-01-23 12:17:06+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2023-01-23 14:49:06+01:00
log186e8058381747b589190898560dcfa5622facc7
treed4345cc1fe099c358508f722b17e39c82f53ded2
parentfaf2fd18d33de246c5bb03905f25290108e1dd30
signaturelock-open Commit is signed but in an unrecognized format.

std.net.Address: clarify unix socket getOsSockLen


1 files changed, 13 insertions(+), 5 deletions(-)

lib/std/net.zig+13-5
...@@ -103,10 +103,10 @@ pub const Address = extern union {...@@ -103,10 +103,10 @@ pub const Address = extern union {
103 .path = undefined,103 .path = undefined,
104 };104 };
105105
106 // this enables us to have the proper length of the socket in getOsSockLen106 // Add 1 to ensure a terminating 0 is present in the path array for maximum portability.
107 mem.set(u8, &sock_addr.path, 0);107 if (path.len + 1 > sock_addr.path.len) return error.NameTooLong;
108108
109 if (path.len > sock_addr.path.len) return error.NameTooLong;109 mem.set(u8, &sock_addr.path, 0);
110 mem.copy(u8, &sock_addr.path, path);110 mem.copy(u8, &sock_addr.path, path);
111111
112 return Address{ .un = sock_addr };112 return Address{ .un = sock_addr };
...@@ -179,9 +179,17 @@ pub const Address = extern union {...@@ -179,9 +179,17 @@ pub const Address = extern union {
179 unreachable;179 unreachable;
180 }180 }
181181
182 const path_len = std.mem.len(std.meta.assumeSentinel(&self.un.path, 0));182 // Using the full length of the structure here is more portable than returning
183 return @intCast(os.socklen_t, @sizeOf(os.sockaddr.un) - self.un.path.len + path_len);183 // the number of bytes actually used by the currently stored path.
184 // This also is correct regardless if we are passing a socket address to the kernel
185 // (e.g. in bind, connect, sendto) since we ensure the path is 0 terminated in
186 // initUnix() or if we are receiving a socket address from the kernel and must
187 // provide the full buffer size (e.g. getsockname, getpeername, recvfrom, accept).
188 //
189 // To access the path, std.mem.sliceTo(&address.un.path, 0) should be used.
190 return @intCast(os.socklen_t, @sizeOf(os.sockaddr.un));
184 },191 },
192
185 else => unreachable,193 else => unreachable,
186 }194 }
187 }195 }