@@ -164,41 +164,7 @@ impl SocketAddr {
164
164
where
165
165
P : AsRef < Path > ,
166
166
{
167
- // SAFETY: All zeros is a valid representation for `sockaddr_un`.
168
- let mut storage: libc:: sockaddr_un = unsafe { mem:: zeroed ( ) } ;
169
-
170
- let bytes = path. as_ref ( ) . as_os_str ( ) . as_bytes ( ) ;
171
- if bytes. contains ( & b'\0' ) {
172
- return Err ( io:: Error :: new (
173
- io:: ErrorKind :: InvalidInput ,
174
- "path can't contain null bytes" ,
175
- ) ) ;
176
- } else if bytes. len ( ) >= storage. sun_path . len ( ) {
177
- return Err ( io:: Error :: new (
178
- io:: ErrorKind :: InvalidInput ,
179
- "path must be shorter than SUN_LEN" ,
180
- ) ) ;
181
- }
182
-
183
- storage. sun_family = libc:: AF_UNIX as _ ;
184
- // SAFETY: `bytes` and `addr.sun_path` are not overlapping and
185
- // both point to valid memory.
186
- // NOTE: We zeroed the memory above, so the path is already null
187
- // terminated.
188
- unsafe {
189
- ptr:: copy_nonoverlapping (
190
- bytes. as_ptr ( ) ,
191
- storage. sun_path . as_mut_ptr ( ) . cast ( ) ,
192
- bytes. len ( ) ,
193
- )
194
- } ;
195
-
196
- let base = & storage as * const _ as usize ;
197
- let path = & storage. sun_path as * const _ as usize ;
198
- let sun_path_offset = path - base;
199
- let length = sun_path_offset + bytes. len ( ) + 1 ;
200
-
201
- Ok ( SocketAddr { addr : storage, len : length as _ } )
167
+ sockaddr_un ( path. as_ref ( ) ) . map ( |( addr, len) | SocketAddr { addr, len } )
202
168
}
203
169
204
170
/// Returns `true` if the address is unnamed.
0 commit comments