Skip to content

Commit 39d158a

Browse files
committed
Fix length of abstract socket address
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 <[email protected]>
1 parent a4a465d commit 39d158a

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4040
([#1107](https://github.com/nix-rust/nix/pull/1107))
4141

4242
### Fixed
43+
- Fix length of abstract socket addresses
44+
([#1120](https://github.com/nix-rust/nix/pull/1120))
45+
4346
### Removed
4447

4548
## [0.15.0] - 10 August 2019

src/sys/socket/addr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ impl UnixAddr {
562562
ret.sun_path.as_mut_ptr().offset(1) as *mut u8,
563563
path.len());
564564

565-
Ok(UnixAddr(ret, ret.sun_path.len()))
565+
Ok(UnixAddr(ret, path.len() + 1))
566566
}
567567
}
568568

@@ -1300,7 +1300,7 @@ mod tests {
13001300
let addr = UnixAddr::new_abstract(name.as_bytes()).unwrap();
13011301

13021302
let sun_path1 = addr.sun_path();
1303-
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];
1303+
let sun_path2 = [0u8, 110, 105, 120, 0, 97, 98, 115, 116, 114, 97, 99, 116, 0, 116, 101, 115, 116];
13041304
assert_eq!(sun_path1.len(), sun_path2.len());
13051305
for i in 0..sun_path1.len() {
13061306
assert_eq!(sun_path1[i], sun_path2[i]);

test/sys/test_socket.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub fn test_addr_equality_abstract() {
106106
assert_eq!(addr1, addr2);
107107
assert_eq!(calculate_hash(&addr1), calculate_hash(&addr2));
108108

109-
addr2.0.sun_path[18] = 127;
109+
addr2.0.sun_path[17] = 127;
110110
assert_ne!(addr1, addr2);
111111
assert_ne!(calculate_hash(&addr1), calculate_hash(&addr2));
112112
}
@@ -117,16 +117,13 @@ pub fn test_addr_equality_abstract() {
117117
pub fn test_abstract_uds_addr() {
118118
let empty = String::new();
119119
let addr = UnixAddr::new_abstract(empty.as_bytes()).unwrap();
120-
let sun_path = [0u8; 107];
120+
let sun_path: [u8; 0] = [];
121121
assert_eq!(addr.as_abstract(), Some(&sun_path[..]));
122122

123123
let name = String::from("nix\0abstract\0test");
124124
let addr = UnixAddr::new_abstract(name.as_bytes()).unwrap();
125125
let sun_path = [
126-
110u8, 105, 120, 0, 97, 98, 115, 116, 114, 97, 99, 116, 0, 116, 101, 115, 116, 0, 0, 0, 0,
127-
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,
128-
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,
129-
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,
126+
110u8, 105, 120, 0, 97, 98, 115, 116, 114, 97, 99, 116, 0, 116, 101, 115, 116
130127
];
131128
assert_eq!(addr.as_abstract(), Some(&sun_path[..]));
132129
assert_eq!(addr.path(), None);

0 commit comments

Comments
 (0)