rust-lang / rust-lang/libc

Types of `CMSG_LEN` and `CMSG_SPACE` don't match libc on many platforms

Open
#3,240 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

breakage-candidate C-bug
Dominant language
Rust
Stars
2.6k
Forks
1.3k
Avg merge
1d 22h
Merged PRs (30d)
69

Description

The CMSG_LEN and CMSG_SPACE macros defined in <sys/socket.h> on most UNIX platforms do not have well-defined types, but they generally (not always[0]) operate on size_t:

/* Linux (GNU libc, musl) */
#define CMSG_LEN(len)   (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
#define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr)))

/* macOS */
#define CMSG_SPACE(l)           (__DARWIN_ALIGN32(sizeof(struct cmsghdr)) + __DARWIN_ALIGN32(l))
#define CMSG_LEN(l)             (__DARWIN_ALIGN32(sizeof(struct cmsghdr)) + (l))

/* FreeBSD */
#define	CMSG_SPACE(l)		(_ALIGN(sizeof(struct cmsghdr)) + _ALIGN(l))
#define	CMSG_LEN(l)		(_ALIGN(sizeof(struct cmsghdr)) + (l))

/* OpenBSD */
#define	CMSG_LEN(len)	(_ALIGN(sizeof(struct cmsghdr)) + (len))
#define	CMSG_SPACE(len)	(_ALIGN(sizeof(struct cmsghdr)) + _ALIGN(len))

However, the libc crate uses c_uint for the functions that mimic those macros, so the values end up getting bounced back and forth between usize and c_uint with failable as conversions.

The libc crate would ideally use c_size when appropriate to match the platform libc. This would be a breaking change, though most users of those functions follow a CMSG_LEN(some_slice.len() as _) pattern that might reduce the ecosystem impact.

[0] An example platform that does not use size_t is Solaris and its descendants, which use unsigned int: https://github.com/illumos/illumos-gate/blob/118b2dbf1f4a745a7e35a5054a777c09bd90fff7/usr/src/uts/common/sys/socket.h#L499-L505

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the libc crate bindings for CMSG_LEN and CMSG_SPACE and compare their signatures with the platform definitions in <sys/socket.h>, including the Solaris example linked in the issue. Done means the bindings use the platform-appropriate types without unnecessary conversions while preserving the unsigned-int platforms and documenting or testing the breaking change.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.