Skip to content

std.os.setsockopt broken with libc #4797

@dermetfan

Description

@dermetfan

std.os.setsockopt appears to be broken when compiling with libc.

Example:

usingnamespace @import("std");

pub fn main() !void {
    const fd = try os.socket(os.AF_INET, os.SOCK_STREAM, 0);
    _ = try os.setsockopt(fd, os.SOL_SOCKET, os.SO_REUSEADDR, &mem.toBytes(@as(c_int, 1)));

    // The same bug breaks StreamServer:

    var server = net.StreamServer.init(.{});
    try server.listen(try net.Address.parseIp4("127.0.0.1", 12345));
}

This compiles fine with zig build-exe main.zig but gives the following error when compiling with --library c:

/nix/store/syj90d418f63szyinbklp69zvv10v8qy-zig-5acc8afb5f9674b9b7b290635e9c2837872b8a93/lib/zig/std/os.zig:4228:60: error: cast discards const qualifier
    switch (errno(system.setsockopt(fd, level, optname, opt.ptr, @intCast(socklen_t, opt.len)))) {
                                                           ^

I got around this issue by removing the const qualifier in my copy of the standard library:

- pub fn setsockopt(fd: fd_t, level: u32, optname: u32, opt: []const u8) SetSockOptError!void {
+ pub fn setsockopt(fd: fd_t, level: u32, optname: u32, opt: []u8) SetSockOptError!void {

I'm not sure if that's an ok fix though so I did not open a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions