Skip to content

Commit 342585c

Browse files
bors[bot]asomers
andauthored
Merge #1675 #1676
1675: Fix a panic in Linkaddr::addr r=rtzoeller a=asomers The function assumed something about the values of the sockaddr_dl's fields. But because the inner type is public, we musn't do that. The only solution is to change the function's signature to return an Option. 1676: Fix the build on DragonflyBSD with -Zminimal-versions r=asomers a=asomers Co-authored-by: Alan Somers <[email protected]>
3 parents bf59a6c + f0f6795 + e08c47c commit 342585c

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
7676

7777
- `InetAddr::from_std` now sets the `sin_len`/`sin6_len` fields on the BSDs.
7878
(#[1642](https://github.com/nix-rust/nix/pull/1642))
79+
- Fixed a panic in `LinkAddr::addr`. That function now returns an `Option`.
80+
(#[1675](https://github.com/nix-rust/nix/pull/1675))
7981

8082
### Removed
8183

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ targets = [
2727
]
2828

2929
[dependencies]
30-
libc = { version = "0.2.114", features = [ "extra_traits" ] }
30+
libc = { version = "0.2.115", features = [ "extra_traits" ] }
3131
bitflags = "1.1"
3232
cfg-if = "1.0"
3333

src/sys/socket/addr.rs

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,33 +1446,38 @@ mod datalink {
14461446
}
14471447

14481448
/// Physical-layer address (MAC)
1449-
pub fn addr(&self) -> [u8; 6] {
1449+
pub fn addr(&self) -> Option<[u8; 6]> {
14501450
let nlen = self.nlen();
14511451
let data = self.0.sdl_data;
14521452

1453-
assert!(!self.is_empty());
1454-
1455-
[
1456-
data[nlen] as u8,
1457-
data[nlen + 1] as u8,
1458-
data[nlen + 2] as u8,
1459-
data[nlen + 3] as u8,
1460-
data[nlen + 4] as u8,
1461-
data[nlen + 5] as u8,
1462-
]
1453+
if self.is_empty() {
1454+
None
1455+
} else {
1456+
Some([
1457+
data[nlen] as u8,
1458+
data[nlen + 1] as u8,
1459+
data[nlen + 2] as u8,
1460+
data[nlen + 3] as u8,
1461+
data[nlen + 4] as u8,
1462+
data[nlen + 5] as u8,
1463+
])
1464+
}
14631465
}
14641466
}
14651467

14661468
impl fmt::Display for LinkAddr {
14671469
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1468-
let addr = self.addr();
1469-
write!(f, "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
1470-
addr[0],
1471-
addr[1],
1472-
addr[2],
1473-
addr[3],
1474-
addr[4],
1475-
addr[5])
1470+
if let Some(addr) = self.addr() {
1471+
write!(f, "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
1472+
addr[0],
1473+
addr[1],
1474+
addr[2],
1475+
addr[3],
1476+
addr[4],
1477+
addr[5])
1478+
} else {
1479+
Ok(())
1480+
}
14761481
}
14771482
}
14781483
}
@@ -1558,6 +1563,28 @@ mod tests {
15581563
target_os = "openbsd"))]
15591564
use super::*;
15601565

1566+
/// Don't panic when trying to display an empty datalink address
1567+
#[cfg(any(target_os = "dragonfly",
1568+
target_os = "freebsd",
1569+
target_os = "ios",
1570+
target_os = "macos",
1571+
target_os = "netbsd",
1572+
target_os = "openbsd"))]
1573+
#[test]
1574+
fn test_datalink_display() {
1575+
let la = LinkAddr(libc::sockaddr_dl{
1576+
sdl_len: 56,
1577+
sdl_family: 18,
1578+
sdl_index: 5,
1579+
sdl_type: 24,
1580+
sdl_nlen: 3,
1581+
sdl_alen: 0,
1582+
sdl_slen: 0,
1583+
.. unsafe{mem::zeroed()}
1584+
});
1585+
format!("{}", la);
1586+
}
1587+
15611588
#[cfg(any(target_os = "dragonfly",
15621589
target_os = "freebsd",
15631590
target_os = "ios",
@@ -1593,7 +1620,8 @@ mod tests {
15931620

15941621
match sock_addr {
15951622
SockAddr::Link(ether_addr) => {
1596-
assert_eq!(ether_addr.addr(), [24u8, 101, 144, 221, 76, 176]);
1623+
assert_eq!(ether_addr.addr(),
1624+
Some([24u8, 101, 144, 221, 76, 176]));
15971625
},
15981626
_ => { unreachable!() }
15991627
};
@@ -1615,7 +1643,8 @@ mod tests {
16151643

16161644
match sock_addr {
16171645
SockAddr::Link(ether_addr) => {
1618-
assert_eq!(ether_addr.addr(), [24u8, 101, 144, 221, 76, 176]);
1646+
assert_eq!(ether_addr.addr(),
1647+
Some([24u8, 101, 144, 221, 76, 176]));
16191648
},
16201649
_ => { unreachable!() }
16211650
};

0 commit comments

Comments
 (0)