Skip to content

Commit 7be98a4

Browse files
bors[bot]asomers
andauthored
1614: Improve the sockaddr interface: r=asomers a=asomers * All sockaddr newtypes should be repr(transparent) * All sockaddr newtypes should be opaque, so the user can't do something like change the sa_family field in a way that violates invariants. This is a prerequisite for nix-rust#1544. Co-authored-by: Alan Somers <[email protected]>
2 parents 6c6c576 + 867ac51 commit 7be98a4

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
2424
### Fixed
2525
### Removed
2626

27+
- Removed public access to the inner fields of `NetlinkAddr`, `AlgAddr`,
28+
`SysControlAddr`, `LinkAddr`, and `VsockAddr`.
29+
(#[1614](https://github.com/nix-rust/nix/pull/1614))
30+
2731
## [0.23.1] - 2021-12-16
2832

2933
### Added

src/sys/socket/addr.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ impl fmt::Display for IpAddr {
473473
*/
474474

475475
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
476+
#[repr(transparent)]
476477
pub struct Ipv4Addr(pub libc::in_addr);
477478

478479
impl Ipv4Addr {
@@ -522,6 +523,7 @@ impl fmt::Display for Ipv4Addr {
522523
*/
523524

524525
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
526+
#[repr(transparent)]
525527
pub struct Ipv6Addr(pub libc::in6_addr);
526528

527529
// Note that IPv6 addresses are stored in big endian order on all architectures.
@@ -1062,7 +1064,8 @@ pub mod netlink {
10621064
use std::{fmt, mem};
10631065

10641066
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
1065-
pub struct NetlinkAddr(pub sockaddr_nl);
1067+
#[repr(transparent)]
1068+
pub struct NetlinkAddr(pub(in super::super) sockaddr_nl);
10661069

10671070
impl NetlinkAddr {
10681071
pub fn new(pid: u32, groups: u32) -> NetlinkAddr {
@@ -1099,7 +1102,8 @@ pub mod alg {
10991102
use std::ffi::CStr;
11001103

11011104
#[derive(Copy, Clone)]
1102-
pub struct AlgAddr(pub sockaddr_alg);
1105+
#[repr(transparent)]
1106+
pub struct AlgAddr(pub(in super::super) sockaddr_alg);
11031107

11041108
// , PartialEq, Eq, Debug, Hash
11051109
impl PartialEq for AlgAddr {
@@ -1179,9 +1183,9 @@ pub mod sys_control {
11791183

11801184
ioctl_readwrite!(ctl_info, CTL_IOC_MAGIC, CTL_IOC_INFO, ctl_ioc_info);
11811185

1182-
#[repr(C)]
11831186
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
1184-
pub struct SysControlAddr(pub libc::sockaddr_ctl);
1187+
#[repr(transparent)]
1188+
pub struct SysControlAddr(pub(in super::super) libc::sockaddr_ctl);
11851189

11861190
impl SysControlAddr {
11871191
pub const fn new(id: u32, unit: u32) -> SysControlAddr {
@@ -1238,7 +1242,8 @@ mod datalink {
12381242

12391243
/// Hardware Address
12401244
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
1241-
pub struct LinkAddr(pub libc::sockaddr_ll);
1245+
#[repr(transparent)]
1246+
pub struct LinkAddr(pub(in super::super) libc::sockaddr_ll);
12421247

12431248
impl LinkAddr {
12441249
/// Always AF_PACKET
@@ -1315,7 +1320,8 @@ mod datalink {
13151320

13161321
/// Hardware Address
13171322
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
1318-
pub struct LinkAddr(pub libc::sockaddr_dl);
1323+
#[repr(transparent)]
1324+
pub struct LinkAddr(pub(in super::super) libc::sockaddr_dl);
13191325

13201326
impl LinkAddr {
13211327
/// Total length of sockaddr
@@ -1408,7 +1414,8 @@ pub mod vsock {
14081414
use std::hash::{Hash, Hasher};
14091415

14101416
#[derive(Copy, Clone)]
1411-
pub struct VsockAddr(pub sockaddr_vm);
1417+
#[repr(transparent)]
1418+
pub struct VsockAddr(pub(in super::super) sockaddr_vm);
14121419

14131420
impl PartialEq for VsockAddr {
14141421
fn eq(&self, other: &Self) -> bool {

0 commit comments

Comments
 (0)