Skip to content

Respect caller capacity in refcount IP sockaddr writeback - #659

Merged
nunoplopes merged 1 commit into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-6pgf6jamzw
Nov 19, 2026
Merged

nunoplopes merged 1 commit into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-6pgf6jamzw

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Refcount getsockname now copies at most the caller's advertised buffer capacity for IPv4 and IPv6 addresses instead of writing a full address struct or panicking on short buffers. Previously, calling getsockname with a capacity below 16 or 28 bytes overwrote destination bytes past the caller's limit or panicked reading a full SockaddrIn or SockaddrIn6 when the destination allocation was physically smaller.

Sockaddr::encode in rules/socket/shim.rs now serializes SockaddrIn or SockaddrIn6 into a local buffer sized with ::std::mem::size_of for the corresponding native libc address type, reads the input out_len, and copies only min(out_len, address_bytes.len()) bytes when non-zero through Ptr::<u8>::with_slice_mut, leaving any suffix and zero-capacity null destinations untouched before writing ss.len() to out_len. getpeername and recvfrom inherit the same bounded IP writeback.

tests/unit/socket_ip_name.c checks unbound IPv4 and IPv6 datagram sockets across capacities 0, 1, 2, actual - 1, actual, and actual + 1 on both padded buffers and exact-size heap allocations, comparing prefixes against native output and asserting untouched suffixes. On the base revision with only the new fixture files added, socket_ip_name.c fails in refcount while passing in native C and unsafe.

Returned IP address length reporting (ss.len()), syscall error branches, Sockaddr::decode, and the Unix branch are unchanged. Testing ran on Linux; macOS CI handles macOS validation.

Testing

libcc2rs reinterpreted-pointer unit tests report 5 passed, 0 failed, and 0 ignored (73 filtered out).

Scoped lit socket unit tests (ifaddrs.c, inet_pton_ntop.c, libc_struct_without_default.cpp, netdb.c, socket_ip_name.c, socket_listen.c, socket_open_close.c, socket_transparent_union.c, socket_type_constants.c, socket_unix_name.c, sockopt.c) in Linux Release report 11 passed, 0 failed, and 0 skipped.

clang-format-23, cargo fmt, and cargo clippy on rules and libcc2rs passed with no warnings or formatting changes.

Full tests/unit, tests/std, tests/ub, tests/multi-file, libcc2rs-macros, and macOS builds are left to target CI.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

Comment thread rules/socket/shim.rs Outdated
let l = ::libc::sockaddr_in::from(*ss.as_sockaddr_in().unwrap());
out.reinterpret_cast::<SockaddrIn>()
.write(SockaddrIn::from_libc(&l));
let mut bytes = [0; 16];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using constants, can you use a sizeof expression here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both address buffers now use sizeof expressions for their corresponding native libc address types instead of numeric sizes. The bounded-copy logic and fixtures are unchanged.

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
@perfloop
perfloop Bot force-pushed the perfloop-pr-open-6pgf6jamzw branch from e9a7a41 to 16278a8 Compare November 19, 2026 10:18
@nunoplopes
nunoplopes merged commit 12d0b80 into Cpp2Rust:master Nov 19, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants