zig icon indicating copy to clipboard operation
zig copied to clipboard

Compilation failure using std.os.sendto for x86_64-windows target

Open TheMarcsOv opened this issue 2 years ago • 1 comments

Zig Version

0.11.0-dev.2680+a1aa55ebe

Steps to Reproduce and Observed Behavior

Sample code:

const std = @import("std");
const os = std.os;

pub fn main() !void {
    
    const sock = try os.socket(os.AF.INET, os.SOCK.DGRAM, 0);
    defer os.closeSocket(sock);

    var address = try std.net.Address.parseIp("127.0.0.1", 1111);
    const addr_len = address.getOsSockLen();
    const buffer = [_]u8{0} ** 16;
    _ = try std.os.sendto(sock, &buffer, 0, &address.any, addr_len);

}

Build command: zig build-exe src/main.zig -target x86_64-windows

Output:

/home/marcos/.local/bin/zig_fork/lib/std/os.zig:5930:59: error: expected type 'i32', found 'usize'
        switch (windows.ws2_32.sendto(sockfd, buf.ptr, buf.len, flags, dest_addr, addrlen)) {
                                                       ~~~^~~~
/home/marcos/.local/bin/zig_fork/lib/std/os.zig:5930:59: note: signed 32-bit int cannot represent all possible unsigned 64-bit values
/home/marcos/.local/bin/zig_fork/lib/std/os/windows/ws2_32.zig:1820:10: note: parameter type declared here
    len: i32,
         ^~~
referenced by:
    main: main.zig:12:19
    callMain: /home/marcos/.local/bin/zig_fork/lib/std/start.zig:609:32
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

Expected Behavior

Successful compilation, os.sendto using WS2's implementation.

TheMarcsOv avatar Apr 25 '23 22:04 TheMarcsOv

Potential fix:

Use os.windows.sendto instead of os.windows.ws2_sendto inside of os.sendto:

--- a/lib/std/os.zig
+++ b/lib/std/os.zig
@@ -5927,7 +5927,7 @@ pub fn sendto(
     addrlen: socklen_t,
 ) SendToError!usize {
     if (builtin.os.tag == .windows) {
-        switch (windows.ws2_32.sendto(sockfd, buf.ptr, buf.len, flags, dest_addr, addrlen)) {
+        switch (windows.sendto(sockfd, buf.ptr, buf.len, flags, dest_addr, addrlen)) {
             windows.ws2_32.SOCKET_ERROR => switch (windows.ws2_32.WSAGetLastError()) {
                 .WSAEACCES => return error.AccessDenied,
                 .WSAEADDRNOTAVAIL => return error.AddressNotAvailable,

TheMarcsOv avatar Apr 25 '23 22:04 TheMarcsOv