Skip to content

Fix a panic when opening a directory with truncate(true) #388

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

Merged
merged 2 commits into from
Apr 21, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
build: [stable, windows-latest, windows-2019, macos-latest, macos-13, beta, ubuntu-20.04, aarch64-ubuntu]
build: [stable, windows-latest, windows-2019, macos-latest, macos-13, beta, ubuntu-22.04, aarch64-ubuntu]
include:
- build: stable
os: ubuntu-latest
Expand All @@ -221,8 +221,8 @@ jobs:
- build: beta
os: ubuntu-latest
rust: beta
- build: ubuntu-20.04
os: ubuntu-20.04
- build: ubuntu-22.04
os: ubuntu-22.04
rust: stable
- build: aarch64-ubuntu
os: ubuntu-latest
Expand Down Expand Up @@ -332,13 +332,13 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
build: [ubuntu, ubuntu-20.04]
build: [ubuntu, ubuntu-22.04]
include:
- build: ubuntu
os: ubuntu-latest
rust: nightly
- build: ubuntu-20.04
os: ubuntu-20.04
- build: ubuntu-22.04
os: ubuntu-22.04
rust: nightly

env:
Expand Down
12 changes: 7 additions & 5 deletions cap-primitives/src/windows/fs/open_unchecked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,14 @@ fn handle_open_result(
}

// Windows truncates symlinks into normal files, so truncation
// may be disabled above; do it manually if needed.
// may be disabled above; do it manually if needed. Note that this
// is expected to always succeed for normal files, but this will
// fail if a directory was opened as directories don't support
// truncation.
if manually_trunc {
// Unwrap is ok because 0 never overflows, and we'll only
// have `manually_trunc` set when the file is opened for
// writing.
f.set_len(0).unwrap();
if let Err(e) = f.set_len(0) {
return Err(OpenUncheckedError::Other(e));
}
}
Ok(f)
}
Expand Down
16 changes: 16 additions & 0 deletions tests/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1645,3 +1645,19 @@ fn create_dir_long_paths() {
std::io::ErrorKind::NotFound
);
}

/// Ensure that opening a directory with the truncation flag set is an error,
/// not a panic inside of cap-std.
#[test]
fn open_directory_with_truncate_is_error() {
use cap_fs_ext::OpenOptionsMaybeDirExt;
let tmpdir = tmpdir();
let mut options = OpenOptions::new();
options
.truncate(true)
.maybe_dir(true)
.read(true)
.write(true);
tmpdir.create_dir("test").unwrap();
assert!(tmpdir.open_with("test", &options).is_err());
}