Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix another UB in raw dir #474

Merged
merged 1 commit into from Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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