Skip to content

Commit

Permalink
Fix another UB in raw dir (#474)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
  • Loading branch information
SUPERCILEX committed Dec 13, 2022
1 parent a493c37 commit 811e5ea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
19 changes: 12 additions & 7 deletions src/fs/raw_dir.rs
Expand Up @@ -45,7 +45,8 @@ impl<'buf, Fd: AsFd> RawDir<'buf, Fd> {
/// let fd = openat(cwd(), ".", OFlags::RDONLY | OFlags::DIRECTORY, Mode::empty()).unwrap();
///
/// let mut buf = Vec::with_capacity(8192);
/// for entry in RawDir::new(fd, buf.spare_capacity_mut()) {
/// let mut iter = RawDir::new(fd, buf.spare_capacity_mut());
/// while let Some(entry) = iter.next() {
/// let entry = entry.unwrap();
/// dbg!(&entry);
/// }
Expand All @@ -60,7 +61,8 @@ impl<'buf, Fd: AsFd> RawDir<'buf, Fd> {
/// let fd = openat(cwd(), ".", OFlags::RDONLY | OFlags::DIRECTORY, Mode::empty()).unwrap();
///
/// let mut buf = [MaybeUninit::uninit(); 2048];
/// for entry in RawDir::new(fd, &mut buf) {
/// let mut iter = RawDir::new(fd, &mut buf);
/// while let Some(entry) = iter.next() {
/// let entry = entry.unwrap();
/// dbg!(&entry);
/// }
Expand All @@ -82,7 +84,8 @@ impl<'buf, Fd: AsFd> RawDir<'buf, Fd> {
/// let mut buf = Vec::with_capacity(8192);
/// 'read: loop {
/// 'resize: {
/// for entry in RawDir::new(&fd, buf.spare_capacity_mut()) {
/// let mut iter = RawDir::new(&fd, buf.spare_capacity_mut());
/// while let Some(entry) = iter.next() {
/// let entry = match entry {
/// Err(Errno::INVAL) => break 'resize,
/// r => r.unwrap(),
Expand Down Expand Up @@ -162,11 +165,13 @@ impl<'a> RawDirEntry<'a> {
}
}

impl<'buf, Fd: AsFd> Iterator for RawDir<'buf, Fd> {
type Item = io::Result<RawDirEntry<'buf>>;

impl<'buf, Fd: AsFd> RawDir<'buf, Fd> {
/// Identical to [Iterator::next] except that [Iterator::Item] borrows from self.
///
/// Note: this interface will be broken to implement a stdlib iterator API with
/// GAT support once one becomes available.
#[allow(unsafe_code)]
fn next(&mut self) -> Option<Self::Item> {
pub fn next(&mut self) -> Option<io::Result<RawDirEntry>> {
loop {
if self.offset < self.initialized {
let dirent_ptr = self.buf[self.offset..].as_ptr();
Expand Down
13 changes: 6 additions & 7 deletions tests/fs/readdir.rs
Expand Up @@ -56,24 +56,23 @@ fn read_entries(dir: &mut Dir) -> HashMap<String, DirEntry> {

#[cfg(any(target_os = "android", target_os = "linux"))]
fn test_raw_dir(buf: &mut [MaybeUninit<u8>]) {
use std::collections::HashSet;
use std::io::{Seek, SeekFrom};

use rustix::fd::AsFd;
use rustix::fs::{RawDir, RawDirEntry};
use rustix::fs::RawDir;

fn read_raw_entries<'a, Fd: AsFd>(
dir: &'a mut RawDir<'_, Fd>,
) -> HashMap<String, RawDirEntry<'a>> {
let mut out = HashMap::new();
for entry in dir {
fn read_raw_entries<Fd: AsFd>(dir: &mut RawDir<Fd>) -> HashSet<String> {
let mut out = HashSet::new();
while let Some(entry) = dir.next() {
let entry = entry.expect("non-error entry");
let name = entry
.file_name()
.to_str()
.expect("utf8 filename")
.to_owned();
if name != "." && name != ".." {
out.insert(name, entry);
out.insert(name);
}
}
out
Expand Down

0 comments on commit 811e5ea

Please sign in to comment.