Skip to content

Commit d9cc325

Browse files
authored
Merge pull request #1218 from pbor/unix-socket-addr
gio: fix UnixSocketAddress constructor with a path
2 parents 2cb10ec + f0eff3f commit d9cc325

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

gio/src/unix_socket_address.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,21 @@ impl UnixSocketAddress {
4646
use self::UnixSocketAddressPath::*;
4747

4848
let type_ = address_type.to_type();
49-
let (path, len) = match address_type {
50-
Path(path) => (path.to_glib_none().0, path.as_os_str().len()),
51-
Abstract(path) | AbstractPadded(path) => {
52-
(path.to_glib_none().0 as *mut libc::c_char, path.len())
53-
}
54-
Anonymous => (ptr::null_mut(), 0),
55-
};
56-
unsafe {
49+
let new = |ptr, len| unsafe {
5750
SocketAddress::from_glib_full(ffi::g_unix_socket_address_new_with_type(
58-
path,
59-
len as i32,
51+
ptr,
52+
len,
6053
type_.into_glib(),
6154
))
6255
.unsafe_cast()
56+
};
57+
match address_type {
58+
Path(path) => new(path.to_glib_none().0, -1),
59+
Abstract(path) | AbstractPadded(path) => new(
60+
path.to_glib_none().0 as *mut libc::c_char,
61+
path.len() as i32,
62+
),
63+
Anonymous => new(ptr::null_mut(), 0),
6364
}
6465
}
6566
}
@@ -97,3 +98,24 @@ pub trait UnixSocketAddressExtManual: sealed::Sealed + IsA<UnixSocketAddress> +
9798
}
9899

99100
impl<O: IsA<UnixSocketAddress>> UnixSocketAddressExtManual for O {}
101+
102+
#[cfg(test)]
103+
mod test {
104+
use super::*;
105+
106+
// Check the actual path and len are correct and are not the underlying OsString
107+
#[test]
108+
fn check_path() {
109+
let mut os_string = std::ffi::OsString::with_capacity(100);
110+
os_string.push("/tmp/foo");
111+
let path = os_string.as_ref();
112+
113+
let addr = UnixSocketAddress::new(path);
114+
assert_eq!(addr.path_len(), 8);
115+
assert_eq!(addr.path_as_array().unwrap().as_ref(), b"/tmp/foo");
116+
117+
let addr = UnixSocketAddress::with_type(UnixSocketAddressPath::Path(path));
118+
assert_eq!(addr.path_len(), 8);
119+
assert_eq!(addr.path_as_array().unwrap().as_ref(), b"/tmp/foo");
120+
}
121+
}

0 commit comments

Comments
 (0)