@@ -2,7 +2,7 @@ use crate::ffi::OsStr;
2
2
use crate :: os:: unix:: ffi:: OsStrExt ;
3
3
use crate :: path:: Path ;
4
4
use crate :: sys:: cvt;
5
- use crate :: { ascii, fmt, io, iter , mem, ptr} ;
5
+ use crate :: { ascii, fmt, io, mem, ptr} ;
6
6
7
7
// FIXME(#43348): Make libc adapt #[doc(cfg(...))] so we don't need these fake definitions here?
8
8
#[ cfg( not( unix) ) ]
@@ -22,8 +22,9 @@ fn sun_path_offset(addr: &libc::sockaddr_un) -> usize {
22
22
path - base
23
23
}
24
24
25
- pub ( super ) unsafe fn sockaddr_un ( path : & Path ) -> io:: Result < ( libc:: sockaddr_un , libc:: socklen_t ) > {
26
- let mut addr: libc:: sockaddr_un = mem:: zeroed ( ) ;
25
+ pub ( super ) fn sockaddr_un ( path : & Path ) -> io:: Result < ( libc:: sockaddr_un , libc:: socklen_t ) > {
26
+ // SAFETY: All zeros is a valid representation for `sockaddr_un`.
27
+ let mut addr: libc:: sockaddr_un = unsafe { mem:: zeroed ( ) } ;
27
28
addr. sun_family = libc:: AF_UNIX as libc:: sa_family_t ;
28
29
29
30
let bytes = path. as_os_str ( ) . as_bytes ( ) ;
@@ -41,11 +42,13 @@ pub(super) unsafe fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un,
41
42
& "path must be shorter than SUN_LEN" ,
42
43
) ) ;
43
44
}
44
- for ( dst, src) in iter:: zip ( & mut addr. sun_path , bytes) {
45
- * dst = * src as libc:: c_char ;
46
- }
47
- // null byte for pathname addresses is already there because we zeroed the
48
- // struct
45
+ // SAFETY: `bytes` and `addr.sun_path` are not overlapping and
46
+ // both point to valid memory.
47
+ // NOTE: We zeroed the memory above, so the path is already null
48
+ // terminated.
49
+ unsafe {
50
+ ptr:: copy_nonoverlapping ( bytes. as_ptr ( ) , addr. sun_path . as_mut_ptr ( ) . cast ( ) , bytes. len ( ) )
51
+ } ;
49
52
50
53
let mut len = sun_path_offset ( & addr) + bytes. len ( ) ;
51
54
match bytes. get ( 0 ) {
0 commit comments