# fix msghdr and cmsghdr on non-Linux targets, document musl behaviour · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/0a2f6a048b1e9c9eee97b463c18aaedad3e31f60)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 0a2f6a048b1e9c9eee97b463c18aaedad3e31f60

Requested commit: 0a2f6a048b1e9c9eee97b463c18aaedad3e31f60

Commit: 0a2f6a048b1e9c9eee97b463c18aaedad3e31f60

Tree: dbdfef9a9f266e1c8468abe06dd5ddcac2b9e536

Author: invlpg

Committer: Andrew Kelley

## Message

````
fix msghdr and cmsghdr on non-Linux targets, document musl behaviour

The following assertions fail on non-Linux platforms after c0c20105354
which inserted padding based on musl definitions. This padding only
exists on musl to workaround a discrepancy betweeen the POSIX API and
Linux ABI, and is incorrect on other POSIX operating systems.

This change makes the padding musl-only, and documents the reason it
exists. With this change, the assertions pass on Linux and FreeBSD
targets. The corresponding definitions on other targets line up with the
POSIX and FreeBSD ones, so they should work there too.

```zig
const std = @import("std");
const assert = std.debug.assert;
const msghdr = std.c.msghdr;
const cmsghdr = std.c.cmsghdr;

const c = @cImport({
    @cInclude("sys/socket.h");
});

comptime {
    assert(@offsetOf(msghdr, "iovlen") == @offsetOf(c.msghdr, "msg_iovlen"));
    assert(@offsetOf(msghdr, "controllen") == @offsetOf(c.msghdr, "msg_controllen"));
    assert(@offsetOf(msghdr, "control") == @offsetOf(c.msghdr, "msg_control"));
    assert(@offsetOf(msghdr, "flags") == @offsetOf(c.msghdr, "msg_flags"));

    assert(@sizeOf(msghdr) == @sizeOf(c.msghdr));

    assert(@offsetOf(cmsghdr, "len") == @offsetOf(c.cmsghdr, "cmsg_len"));
    assert(@offsetOf(cmsghdr, "level") == @offsetOf(c.cmsghdr, "cmsg_level"));

    assert(@sizeOf(cmsghdr) == @sizeOf(c.cmsghdr));
}
```

````

## Parents

- [442b292a3712829578c1b93d406a87f1abcf45b8](https://git.cafe/gitcafe/zig/commit/442b292a3712829578c1b93d406a87f1abcf45b8?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/0a2f6a048b1e9c9eee97b463c18aaedad3e31f60?format=markdown)
