Skip to content

Commit

Permalink
Merge #1120
Browse files Browse the repository at this point in the history
1120: Fix length of abstract socket address r=asomers a=yshui

NULL bytes have no special significance in an abstrace address, and the
length of the address is solely decided by the length member. If the
length is set to sun_path.len(), all the NULL bytes will be considered
part of the address.

Tests are updated accordingly.

Closes #1119

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>

Co-authored-by: Yuxuan Shui <yshuiv7@gmail.com>
  • Loading branch information
bors[bot] and yshui committed Sep 4, 2019
2 parents 01f4d57 + 39d158a commit 1662466
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#1107](https://github.com/nix-rust/nix/pull/1107))

### Fixed
- Fix length of abstract socket addresses
([#1120](https://github.com/nix-rust/nix/pull/1120))

### Removed

## [0.15.0] - 10 August 2019
Expand Down
4 changes: 2 additions & 2 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ impl UnixAddr {
ret.sun_path.as_mut_ptr().offset(1) as *mut u8,
path.len());

Ok(UnixAddr(ret, ret.sun_path.len()))
Ok(UnixAddr(ret, path.len() + 1))
}
}

Expand Down Expand Up @@ -1298,7 +1298,7 @@ mod tests {
let addr = UnixAddr::new_abstract(name.as_bytes()).unwrap();

let sun_path1 = addr.sun_path();
let sun_path2 = [0u8, 110, 105, 120, 0, 97, 98, 115, 116, 114, 97, 99, 116, 0, 116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let sun_path2 = [0u8, 110, 105, 120, 0, 97, 98, 115, 116, 114, 97, 99, 116, 0, 116, 101, 115, 116];
assert_eq!(sun_path1.len(), sun_path2.len());
for i in 0..sun_path1.len() {
assert_eq!(sun_path1[i], sun_path2[i]);
Expand Down
9 changes: 3 additions & 6 deletions test/sys/test_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn test_addr_equality_abstract() {
assert_eq!(addr1, addr2);
assert_eq!(calculate_hash(&addr1), calculate_hash(&addr2));

addr2.0.sun_path[18] = 127;
addr2.0.sun_path[17] = 127;
assert_ne!(addr1, addr2);
assert_ne!(calculate_hash(&addr1), calculate_hash(&addr2));
}
Expand All @@ -117,16 +117,13 @@ pub fn test_addr_equality_abstract() {
pub fn test_abstract_uds_addr() {
let empty = String::new();
let addr = UnixAddr::new_abstract(empty.as_bytes()).unwrap();
let sun_path = [0u8; 107];
let sun_path: [u8; 0] = [];
assert_eq!(addr.as_abstract(), Some(&sun_path[..]));

let name = String::from("nix\0abstract\0test");
let addr = UnixAddr::new_abstract(name.as_bytes()).unwrap();
let sun_path = [
110u8, 105, 120, 0, 97, 98, 115, 116, 114, 97, 99, 116, 0, 116, 101, 115, 116, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110u8, 105, 120, 0, 97, 98, 115, 116, 114, 97, 99, 116, 0, 116, 101, 115, 116
];
assert_eq!(addr.as_abstract(), Some(&sun_path[..]));
assert_eq!(addr.path(), None);
Expand Down

0 comments on commit 1662466

Please sign in to comment.