Browse Source

Fix overflow issues in Redox getdents

Jeremy Soller 6 years ago
parent
commit
de429a8df6
1 changed files with 6 additions and 2 deletions
  1. 6 2
      src/platform/redox/mod.rs

+ 6 - 2
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,