From de429a8df69b5b588ba7527f20d21548c797d14a Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Sun, 2 Dec 2018 21:04:07 -0700
Subject: [PATCH] Fix overflow issues in Redox getdents

---
 src/platform/redox/mod.rs | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/platform/redox/mod.rs b/src/platform/redox/mod.rs
index eaf0de9ac..106cdd974 100644
--- a/src/platform/redox/mod.rs
+++ b/src/platform/redox/mod.rs
@@ -411,6 +411,9 @@ impl Pal for Sys {
     }
 
     fn getdents(fd: c_int, mut dirents: *mut dirent, max_bytes: usize) -> c_int {
+        //TODO: rewrite this code. Originally the *dirents = dirent { ... } stuff below caused
+        // massive issues. This has been hacked around, but it still isn't perfect
+
         // Get initial reading position
         let mut read = match syscall::lseek(fd as usize, 0, syscall::SEEK_CUR) {
             Ok(pos) => pos as isize,
@@ -429,15 +432,16 @@ impl Pal for Sys {
                 name[*i] = 0;
             }
             // Get size: full size - unused bytes
-            let size = mem::size_of::<dirent>() - name.len().saturating_sub(*i + 1);
-            if *written + size > max_bytes {
+            if *written + mem::size_of::<dirent>() > max_bytes {
                 // Seek back to after last read entry and return
                 match syscall::lseek(fd as usize, read, syscall::SEEK_SET) {
                     Ok(_) => return Some(*written as c_int),
                     Err(err) => return Some(-err.errno),
                 }
             }
+            let size = mem::size_of::<dirent>() - name.len().saturating_sub(*i + 1);
             unsafe {
+                //This is the offending code mentioned above
                 *dirents = dirent {
                     d_ino: 0,
                     d_off: read as off_t,
-- 
GitLab