about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/lib.rs7
-rw-r--r--src/libstd/sys/sgx/abi/mod.rs8
-rw-r--r--src/libstd/sys/windows/path.rs139
-rw-r--r--src/libstd/sys/windows/path/tests.rs21
-rw-r--r--src/libstd/thread/mod.rs5
5 files changed, 108 insertions, 72 deletions
diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs
index bd585d39c24..5215db7cdb3 100644
--- a/src/libstd/lib.rs
+++ b/src/libstd/lib.rs
@@ -242,8 +242,8 @@
 #![feature(atomic_mut_ptr)]
 #![feature(box_syntax)]
 #![feature(c_variadic)]
-#![feature(cfg_accessible)]
 #![feature(can_vector)]
+#![feature(cfg_accessible)]
 #![feature(cfg_target_has_atomic)]
 #![feature(cfg_target_thread_local)]
 #![feature(char_error_internals)]
@@ -276,8 +276,8 @@
 #![feature(hashmap_internals)]
 #![feature(int_error_internals)]
 #![feature(int_error_matching)]
-#![feature(into_future)]
 #![feature(integer_atomics)]
+#![feature(into_future)]
 #![feature(lang_items)]
 #![feature(libc)]
 #![feature(link_args)]
@@ -286,6 +286,7 @@
 #![feature(log_syntax)]
 #![feature(maybe_uninit_ref)]
 #![feature(maybe_uninit_slice)]
+#![feature(min_specialization)]
 #![feature(needs_panic_runtime)]
 #![feature(negative_impls)]
 #![feature(never_type)]
@@ -305,7 +306,7 @@
 #![feature(shrink_to)]
 #![feature(slice_concat_ext)]
 #![feature(slice_internals)]
-#![feature(min_specialization)]
+#![feature(slice_strip)]
 #![feature(staged_api)]
 #![feature(std_internals)]
 #![feature(stdsimd)]
diff --git a/src/libstd/sys/sgx/abi/mod.rs b/src/libstd/sys/sgx/abi/mod.rs
index 5ef26d4cc4d..b0693b63a48 100644
--- a/src/libstd/sys/sgx/abi/mod.rs
+++ b/src/libstd/sys/sgx/abi/mod.rs
@@ -17,6 +17,9 @@ pub mod usercalls;
 #[cfg(not(test))]
 global_asm!(include_str!("entry.S"));
 
+#[repr(C)]
+struct EntryReturn(u64, u64);
+
 #[cfg(not(test))]
 #[no_mangle]
 unsafe extern "C" fn tcs_init(secondary: bool) {
@@ -56,8 +59,7 @@ unsafe extern "C" fn tcs_init(secondary: bool) {
 // able to specify this
 #[cfg(not(test))]
 #[no_mangle]
-#[allow(improper_ctypes_definitions)]
-extern "C" fn entry(p1: u64, p2: u64, p3: u64, secondary: bool, p4: u64, p5: u64) -> (u64, u64) {
+extern "C" fn entry(p1: u64, p2: u64, p3: u64, secondary: bool, p4: u64, p5: u64) -> EntryReturn {
     // FIXME: how to support TLS in library mode?
     let tls = Box::new(tls::Tls::new());
     let _tls_guard = unsafe { tls.activate() };
@@ -65,7 +67,7 @@ extern "C" fn entry(p1: u64, p2: u64, p3: u64, secondary: bool, p4: u64, p5: u64
     if secondary {
         super::thread::Thread::entry();
 
-        (0, 0)
+        EntryReturn(0, 0)
     } else {
         extern "C" {
             fn main(argc: isize, argv: *const *const u8) -> isize;
diff --git a/src/libstd/sys/windows/path.rs b/src/libstd/sys/windows/path.rs
index 524f21f889b..dda3ed68cfc 100644
--- a/src/libstd/sys/windows/path.rs
+++ b/src/libstd/sys/windows/path.rs
@@ -2,6 +2,16 @@ use crate::ffi::OsStr;
 use crate::mem;
 use crate::path::Prefix;
 
+#[cfg(test)]
+mod tests;
+
+pub const MAIN_SEP_STR: &str = "\\";
+pub const MAIN_SEP: char = '\\';
+
+// The unsafety here stems from converting between `&OsStr` and `&[u8]`
+// and back. This is safe to do because (1) we only look at ASCII
+// contents of the encoding and (2) new &OsStr values are produced
+// only from ASCII-bounded slices of existing &OsStr values.
 fn os_str_as_u8_slice(s: &OsStr) -> &[u8] {
     unsafe { mem::transmute(s) }
 }
@@ -19,76 +29,79 @@ pub fn is_verbatim_sep(b: u8) -> bool {
     b == b'\\'
 }
 
+// In most DOS systems, it is not possible to have more than 26 drive letters.
+// See <https://en.wikipedia.org/wiki/Drive_letter_assignment#Common_assignments>.
+pub fn is_valid_drive_letter(disk: u8) -> bool {
+    disk.is_ascii_alphabetic()
+}
+
 pub fn parse_prefix(path: &OsStr) -> Option<Prefix<'_>> {
-    use crate::path::Prefix::*;
-    unsafe {
-        // The unsafety here stems from converting between &OsStr and &[u8]
-        // and back. This is safe to do because (1) we only look at ASCII
-        // contents of the encoding and (2) new &OsStr values are produced
-        // only from ASCII-bounded slices of existing &OsStr values.
-        let mut path = os_str_as_u8_slice(path);
+    use Prefix::{DeviceNS, Disk, Verbatim, VerbatimDisk, VerbatimUNC, UNC};
+
+    let path = os_str_as_u8_slice(path);
 
-        if path.starts_with(br"\\") {
-            // \\
-            path = &path[2..];
-            if path.starts_with(br"?\") {
-                // \\?\
-                path = &path[2..];
-                if path.starts_with(br"UNC\") {
-                    // \\?\UNC\server\share
-                    path = &path[4..];
-                    let (server, share) = match parse_two_comps(path, is_verbatim_sep) {
-                        Some((server, share)) => {
-                            (u8_slice_as_os_str(server), u8_slice_as_os_str(share))
-                        }
-                        None => (u8_slice_as_os_str(path), u8_slice_as_os_str(&[])),
-                    };
-                    return Some(VerbatimUNC(server, share));
-                } else {
-                    // \\?\path
-                    let idx = path.iter().position(|&b| b == b'\\');
-                    if idx == Some(2) && path[1] == b':' {
-                        let c = path[0];
-                        if c.is_ascii() && (c as char).is_alphabetic() {
-                            // \\?\C:\ path
-                            return Some(VerbatimDisk(c.to_ascii_uppercase()));
-                        }
+    // \\
+    if let Some(path) = path.strip_prefix(br"\\") {
+        // \\?\
+        if let Some(path) = path.strip_prefix(br"?\") {
+            // \\?\UNC\server\share
+            if let Some(path) = path.strip_prefix(br"UNC\") {
+                let (server, share) = match get_first_two_components(path, is_verbatim_sep) {
+                    Some((server, share)) => unsafe {
+                        (u8_slice_as_os_str(server), u8_slice_as_os_str(share))
+                    },
+                    None => (unsafe { u8_slice_as_os_str(path) }, OsStr::new("")),
+                };
+                return Some(VerbatimUNC(server, share));
+            } else {
+                // \\?\path
+                match path {
+                    // \\?\C:\path
+                    [c, b':', b'\\', ..] if is_valid_drive_letter(*c) => {
+                        return Some(VerbatimDisk(c.to_ascii_uppercase()));
+                    }
+                    // \\?\cat_pics
+                    _ => {
+                        let idx = path.iter().position(|&b| b == b'\\').unwrap_or(path.len());
+                        let slice = &path[..idx];
+                        return Some(Verbatim(unsafe { u8_slice_as_os_str(slice) }));
                     }
-                    let slice = &path[..idx.unwrap_or(path.len())];
-                    return Some(Verbatim(u8_slice_as_os_str(slice)));
-                }
-            } else if path.starts_with(b".\\") {
-                // \\.\path
-                path = &path[2..];
-                let pos = path.iter().position(|&b| b == b'\\');
-                let slice = &path[..pos.unwrap_or(path.len())];
-                return Some(DeviceNS(u8_slice_as_os_str(slice)));
-            }
-            match parse_two_comps(path, is_sep_byte) {
-                Some((server, share)) if !server.is_empty() && !share.is_empty() => {
-                    // \\server\share
-                    return Some(UNC(u8_slice_as_os_str(server), u8_slice_as_os_str(share)));
                 }
-                _ => (),
             }
-        } else if path.get(1) == Some(&b':') {
-            // C:
-            let c = path[0];
-            if c.is_ascii() && (c as char).is_alphabetic() {
-                return Some(Disk(c.to_ascii_uppercase()));
+        } else if let Some(path) = path.strip_prefix(b".\\") {
+            // \\.\COM42
+            let idx = path.iter().position(|&b| b == b'\\').unwrap_or(path.len());
+            let slice = &path[..idx];
+            return Some(DeviceNS(unsafe { u8_slice_as_os_str(slice) }));
+        }
+        match get_first_two_components(path, is_sep_byte) {
+            Some((server, share)) if !server.is_empty() && !share.is_empty() => {
+                // \\server\share
+                return Some(unsafe { UNC(u8_slice_as_os_str(server), u8_slice_as_os_str(share)) });
             }
+            _ => {}
+        }
+    } else if let [c, b':', ..] = path {
+        // C:
+        if is_valid_drive_letter(*c) {
+            return Some(Disk(c.to_ascii_uppercase()));
         }
-        return None;
-    }
-
-    fn parse_two_comps(mut path: &[u8], f: fn(u8) -> bool) -> Option<(&[u8], &[u8])> {
-        let first = &path[..path.iter().position(|x| f(*x))?];
-        path = &path[(first.len() + 1)..];
-        let idx = path.iter().position(|x| f(*x));
-        let second = &path[..idx.unwrap_or(path.len())];
-        Some((first, second))
     }
+    None
 }
 
-pub const MAIN_SEP_STR: &str = "\\";
-pub const MAIN_SEP: char = '\\';
+/// Returns the first two path components with predicate `f`.
+///
+/// The two components returned will be use by caller
+/// to construct `VerbatimUNC` or `UNC` Windows path prefix.
+///
+/// Returns [`None`] if there are no separators in path.
+fn get_first_two_components(path: &[u8], f: fn(u8) -> bool) -> Option<(&[u8], &[u8])> {
+    let idx = path.iter().position(|&x| f(x))?;
+    // Panic safe
+    // The max `idx+1` is `path.len()` and `path[path.len()..]` is a valid index.
+    let (first, path) = (&path[..idx], &path[idx + 1..]);
+    let idx = path.iter().position(|&x| f(x)).unwrap_or(path.len());
+    let second = &path[..idx];
+    Some((first, second))
+}
diff --git a/src/libstd/sys/windows/path/tests.rs b/src/libstd/sys/windows/path/tests.rs
new file mode 100644
index 00000000000..fbac1dc1ca1
--- /dev/null
+++ b/src/libstd/sys/windows/path/tests.rs
@@ -0,0 +1,21 @@
+use super::*;
+
+#[test]
+fn test_get_first_two_components() {
+    assert_eq!(
+        get_first_two_components(br"server\share", is_verbatim_sep),
+        Some((&b"server"[..], &b"share"[..])),
+    );
+
+    assert_eq!(
+        get_first_two_components(br"server\", is_verbatim_sep),
+        Some((&b"server"[..], &b""[..]))
+    );
+
+    assert_eq!(
+        get_first_two_components(br"\server\", is_verbatim_sep),
+        Some((&b""[..], &b"server"[..]))
+    );
+
+    assert_eq!(get_first_two_components(br"there are no separators here", is_verbatim_sep), None,);
+}
diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs
index d435ca68425..d354a9b1842 100644
--- a/src/libstd/thread/mod.rs
+++ b/src/libstd/thread/mod.rs
@@ -641,9 +641,8 @@ where
 #[stable(feature = "rust1", since = "1.0.0")]
 pub fn current() -> Thread {
     thread_info::current_thread().expect(
-        "use of std::thread::current() is not \
-                                          possible after the thread's local \
-                                          data has been destroyed",
+        "use of std::thread::current() is not possible \
+         after the thread's local data has been destroyed",
     )
 }