Skip to content

Commit

Permalink
Preserve trailing null bytes in abstract paths with local_addr().
Browse files Browse the repository at this point in the history
  • Loading branch information
de-vri-es committed Nov 10, 2024
1 parent c177d8d commit f7d7108
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,13 @@ fn path_to_sockaddr(path: &Path) -> std::io::Result<(libc::sockaddr_un, usize)>
core::ptr::copy_nonoverlapping(path.as_ptr(), sockaddr.sun_path.as_mut_ptr() as *mut u8, path.len());
sockaddr.sun_path[path.len()] = 0;
let path_offset = sockaddr.sun_path.as_ptr() as usize - (&sockaddr as *const _ as usize);
let length_addendum = if path.get(0) == Some(&0) {
// do not add trailing zero byte to abstract UNIX socket paths on Linux
0

// Do not add trailing zero byte to abstract UNIX socket paths on Linux and Android.
if cfg!(any(target_os = "linux", target_os = "android")) && path.first() == Some(&0) {
Ok((sockaddr, path_offset + path.len()))
} else {
1
};
Ok((sockaddr, path_offset + path.len() + length_addendum))
Ok((sockaddr, path_offset + path.len() + 1))
}
}
}

Expand All @@ -290,13 +290,13 @@ fn sockaddr_to_path(address: &libc::sockaddr_un, len: libc::socklen_t) -> std::i
let offset = sun_path.offset_from(address as *const _ as *const u8);
let path = core::slice::from_raw_parts(sun_path, len as usize - offset as usize);

// Some platforms include a trailing null byte in the path length.
let path = if path.last() == Some(&0) {
&path[..path.len() - 1]
// Do not strip trailing zero byte from abstract UNIX socket paths on Linux and Android.
if cfg!(any(target_os = "linux", target_os = "android")) && path.first() == Some(&0) {
Ok(Path::new(OsStr::from_bytes(path)))
} else {
path
};
Ok(Path::new(OsStr::from_bytes(path)))
let path = path.strip_suffix(&[0]).unwrap_or(path);
Ok(Path::new(OsStr::from_bytes(path)))
}
}
}
}
Expand Down

0 comments on commit f7d7108

Please sign in to comment.