about summary refs log tree commit diff
path: root/library/std/src/os/unix/net/addr.rs
diff options
context:
space:
mode:
authorThomas de Zeeuw <thomasdezeeuw@gmail.com>2022-01-24 18:00:23 +0100
committerThomas de Zeeuw <thomasdezeeuw@gmail.com>2022-01-24 18:02:37 +0100
commitc1cd200922702f2ec5e30d5b5391c0443607d647 (patch)
tree5b5abfbac00bc43efbe1a08ccf750b61347f7b91 /library/std/src/os/unix/net/addr.rs
parentf2cdb57b946c1f7dc07d8e944aad8d4af8cb2f17 (diff)
downloadrust-c1cd200922702f2ec5e30d5b5391c0443607d647.tar.gz
rust-c1cd200922702f2ec5e30d5b5391c0443607d647.zip
Rename SocketAddr::unix to from_path
And change it to disallow NULL bytes.
Diffstat (limited to 'library/std/src/os/unix/net/addr.rs')
-rw-r--r--library/std/src/os/unix/net/addr.rs36
1 files changed, 20 insertions, 16 deletions
diff --git a/library/std/src/os/unix/net/addr.rs b/library/std/src/os/unix/net/addr.rs
index b8e8e9b506d..7f64533cb67 100644
--- a/library/std/src/os/unix/net/addr.rs
+++ b/library/std/src/os/unix/net/addr.rs
@@ -131,7 +131,8 @@ impl SocketAddr {
     ///
     /// # Errors
     ///
-    /// Returns an error if the path is longer than `SUN_LEN`.
+    /// Returns an error if the path is longer than `SUN_LEN` or if it contains
+    /// NULL bytes.
     ///
     /// # Examples
     ///
@@ -141,13 +142,22 @@ impl SocketAddr {
     /// use std::path::Path;
     ///
     /// # fn main() -> std::io::Result<()> {
-    /// let address = SocketAddr::unix("/path/to/socket")?;
+    /// let address = SocketAddr::from_path("/path/to/socket")?;
     /// assert_eq!(address.as_pathname(), Some(Path::new("/path/to/socket")));
     /// # Ok(())
     /// # }
     /// ```
+    ///
+    /// Creating a `SocketAddr` with a NULL byte results in an error.
+    ///
+    /// ```
+    /// #![feature(unix_socket_creation)]
+    /// use std::os::unix::net::SocketAddr;
+    ///
+    /// assert!(SocketAddr::from_path("/path/with/\0/bytes").is_err());
+    /// ```
     #[unstable(feature = "unix_socket_creation", issue = "65275")]
-    pub fn unix<P>(path: P) -> io::Result<SocketAddr>
+    pub fn from_path<P>(path: P) -> io::Result<SocketAddr>
     where
         P: AsRef<Path>,
     {
@@ -155,13 +165,12 @@ impl SocketAddr {
         let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() };
 
         let bytes = path.as_ref().as_os_str().as_bytes();
-        let too_long = match bytes.first() {
-            None => false,
-            // linux abstract namespaces aren't null-terminated.
-            Some(&0) => bytes.len() > storage.sun_path.len(),
-            Some(_) => bytes.len() >= storage.sun_path.len(),
-        };
-        if too_long {
+        if bytes.contains(&b'\0') {
+            return Err(io::Error::new(
+                io::ErrorKind::InvalidInput,
+                "path can't contain null bytes",
+            ));
+        } else if bytes.len() >= storage.sun_path.len() {
             return Err(io::Error::new(
                 io::ErrorKind::InvalidInput,
                 "path must be shorter than SUN_LEN",
@@ -184,12 +193,7 @@ impl SocketAddr {
         let base = &storage as *const _ as usize;
         let path = &storage.sun_path as *const _ as usize;
         let sun_path_offset = path - base;
-        let length = sun_path_offset
-            + bytes.len()
-            + match bytes.first() {
-                Some(&0) | None => 0,
-                Some(_) => 1,
-            };
+        let length = sun_path_offset + bytes.len() + 1;
 
         Ok(SocketAddr { addr: storage, len: length as _ })
     }