From 44f877d18bef6c82d727efeb5ff78ea97d3307bf Mon Sep 17 00:00:00 2001 From: Bas van den Berg Date: Tue, 8 Sep 2020 12:24:39 +0200 Subject: [PATCH] change socklen_t to u32 and add appropriate casts when calling WSA --- lib/std/os.zig | 2 +- lib/std/os/windows.zig | 18 ++++-------------- lib/std/os/windows/ws2_32.zig | 2 +- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/lib/std/os.zig b/lib/std/os.zig index 80bdca6bd..cfb7a6261 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -3114,7 +3114,7 @@ pub const ConnectError = error{ /// Initiate a connection on a socket. pub fn connect(sock: socket_t, sock_addr: *const sockaddr, len: socklen_t) ConnectError!void { if (builtin.os.tag == .windows) { - const rc = windows.ws2_32.connect(sock, sock_addr, len); + const rc = windows.ws2_32.connect(sock, sock_addr, @intCast(i32, len)); if (rc == 0) return; switch (windows.ws2_32.WSAGetLastError()) { .WSAEADDRINUSE => return error.AddressInUse, diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index 2da8d05ec..69bfd0fbc 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -1154,7 +1154,7 @@ pub fn WSASocketW( } pub fn bind(s: ws2_32.SOCKET, name: *const ws2_32.sockaddr, namelen: ws2_32.socklen_t) i32 { - return ws2_32.bind(s, name, namelen); + return ws2_32.bind(s, name, @intCast(i32, namelen)); } pub fn listen(s: ws2_32.SOCKET, backlog: u31) i32 { @@ -1173,27 +1173,17 @@ pub fn closesocket(s: ws2_32.SOCKET) !void { pub fn accept(s: ws2_32.SOCKET, name: ?*ws2_32.sockaddr, namelen: ?*ws2_32.socklen_t) ws2_32.SOCKET { assert((name == null) == (namelen == null)); - if (namelen) |name_length| { - var os_namelen: c_int = name_length.*; - const sock = ws2_32.accept(s, name, &os_namelen); - name_length.* = @intCast(ws2_32.socklen_t, os_namelen); - return sock; - } else { - return ws2_32.accept(s, null, null); - } + return ws2_32.accept(s, name, @ptrCast(?*i32, namelen)); } pub fn getsockname(s: ws2_32.SOCKET, name: *ws2_32.sockaddr, namelen: *ws2_32.socklen_t) i32 { - var os_namelen: c_int = namelen.*; - const rc = ws2_32.getsockname(s, name, &os_namelen); - namelen.* = @intCast(ws2_32.socklen_t, os_namelen); - return rc; + return ws2_32.getsockname(s, name, @ptrCast(*i32, namelen)); } pub fn sendto(s: ws2_32.SOCKET, buf: [*]const u8, len: usize, flags: u32, to: ?*const ws2_32.sockaddr, to_len: ws2_32.socklen_t) i32 { var buffer = ws2_32.WSABUF{ .len = @truncate(u31, len), .buf = @intToPtr([*]u8, @ptrToInt(buf)) }; var bytes_send: DWORD = undefined; - if (ws2_32.WSASendTo(s, @ptrCast([*]ws2_32.WSABUF, &buffer), 1, &bytes_send, flags, to, to_len, null, null) == ws2_32.SOCKET_ERROR) { + if (ws2_32.WSASendTo(s, @ptrCast([*]ws2_32.WSABUF, &buffer), 1, &bytes_send, flags, to, @intCast(i32, to_len), null, null) == ws2_32.SOCKET_ERROR) { return ws2_32.SOCKET_ERROR; } else { return @as(i32, @intCast(u31, bytes_send)); diff --git a/lib/std/os/windows/ws2_32.zig b/lib/std/os/windows/ws2_32.zig index 2afe42bb3..a54a29ed5 100644 --- a/lib/std/os/windows/ws2_32.zig +++ b/lib/std/os/windows/ws2_32.zig @@ -116,7 +116,7 @@ pub const WSAOVERLAPPED_COMPLETION_ROUTINE = fn (dwError: DWORD, cbTransferred: pub const ADDRESS_FAMILY = u16; // Microsoft use the signed c_int for this, but it should never be negative -pub const socklen_t = u31; +pub const socklen_t = u32; pub const AF_UNSPEC = 0; pub const AF_UNIX = 1;