about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorJosh Stone <cuviper@gmail.com>2016-07-11 21:43:53 -0700
committerJosh Stone <cuviper@gmail.com>2016-07-11 21:43:53 -0700
commit79fb5522bd5bf79c7dad7adfd516ad364dff3e9e (patch)
treed130a67fc71d00d8d6b0bb39755556727fb00a8c /src/libstd
parent726fa9f09f467cd445758ac8b1bbe8a786aadc9b (diff)
downloadrust-79fb5522bd5bf79c7dad7adfd516ad364dff3e9e.tar.gz
rust-79fb5522bd5bf79c7dad7adfd516ad364dff3e9e.zip
std: clear errno before readdir, then check it (solaris)
A `NULL` from `readdir` could be the end of stream or an error.  The
only way to know is to check `errno`, so it must be set to a known value
first, like a 0 that POSIX will never use.

This patch adds `set_errno`, uses it to clear the value before calling
`readdir`, then checks it again after to see the reason for a `NULL`.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/sys/unix/fs.rs8
-rw-r--r--src/libstd/sys/unix/os.rs41
2 files changed, 32 insertions, 17 deletions
diff --git a/src/libstd/sys/unix/fs.rs b/src/libstd/sys/unix/fs.rs
index 0524851df91..fe946ae0ba4 100644
--- a/src/libstd/sys/unix/fs.rs
+++ b/src/libstd/sys/unix/fs.rs
@@ -205,9 +205,15 @@ impl Iterator for ReadDir {
                 // of the thread safety, on Illumos the readdir(3C) function is safe to use
                 // in threaded applications and it is generally preferred over the
                 // readdir_r(3C) function.
+                super::os::set_errno(0);
                 let entry_ptr = libc::readdir(self.dirp.0);
                 if entry_ptr.is_null() {
-                    return None
+                    // NULL can mean either the end is reached or an error occurred.
+                    // So we had to clear errno beforehand to check for an error now.
+                    return match super::os::errno() {
+                        0 => None,
+                        e => Some(Err(Error::from_raw_os_error(e))),
+                    }
                 }
 
                 let name = (*entry_ptr).d_name.as_ptr();
diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs
index 21ce6b19ceb..34b36b854f4 100644
--- a/src/libstd/sys/unix/os.rs
+++ b/src/libstd/sys/unix/os.rs
@@ -35,28 +35,37 @@ use vec;
 const TMPBUF_SZ: usize = 128;
 static ENV_LOCK: Mutex = Mutex::new();
 
+
+extern {
+    #[cfg_attr(any(target_os = "linux", target_os = "emscripten"),
+               link_name = "__errno_location")]
+    #[cfg_attr(any(target_os = "bitrig",
+                   target_os = "netbsd",
+                   target_os = "openbsd",
+                   target_os = "android",
+                   target_env = "newlib"),
+               link_name = "__errno")]
+    #[cfg_attr(target_os = "solaris", link_name = "___errno")]
+    #[cfg_attr(any(target_os = "macos",
+                   target_os = "ios",
+                   target_os = "freebsd"),
+               link_name = "__error")]
+    fn errno_location() -> *mut c_int;
+}
+
 /// Returns the platform-specific value of errno
 #[cfg(not(target_os = "dragonfly"))]
 pub fn errno() -> i32 {
-    extern {
-        #[cfg_attr(any(target_os = "linux", target_os = "emscripten"),
-                   link_name = "__errno_location")]
-        #[cfg_attr(any(target_os = "bitrig",
-                       target_os = "netbsd",
-                       target_os = "openbsd",
-                       target_os = "android",
-                       target_env = "newlib"),
-                   link_name = "__errno")]
-        #[cfg_attr(target_os = "solaris", link_name = "___errno")]
-        #[cfg_attr(any(target_os = "macos",
-                       target_os = "ios",
-                       target_os = "freebsd"),
-                   link_name = "__error")]
-        fn errno_location() -> *const c_int;
+    unsafe {
+        (*errno_location()) as i32
     }
+}
 
+/// Sets the platform-specific value of errno
+#[cfg(target_os = "solaris")] // only needed for readdir so far
+pub fn set_errno(e: i32) {
     unsafe {
-        (*errno_location()) as i32
+        *errno_location() = e as c_int
     }
 }