From d437fe4ce9871a37a49dde10b97a71bedb1fa456 Mon Sep 17 00:00:00 2001 From: mattsu <35655889+mattsu2020@users.noreply.github.com> Date: Thu, 26 Feb 2026 18:34:46 +0900 Subject: [PATCH] fix: correct mkdirat implementation to use nix crate's mkdirat function (#11126) The commit fixes an issue where the `mkdir_at` method was incorrectly using the raw libc `mkdirat` function instead of the nix crate's `mkdirat` function. This change ensures proper error handling and type safety by using the nix crate's wrapper, which provides better integration with Rust's type system and error handling patterns. --- src/uucore/src/lib/features/safe_traversal.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/uucore/src/lib/features/safe_traversal.rs b/src/uucore/src/lib/features/safe_traversal.rs index d58d4d9fa..00e2e94a0 100644 --- a/src/uucore/src/lib/features/safe_traversal.rs +++ b/src/uucore/src/lib/features/safe_traversal.rs @@ -24,7 +24,7 @@ use std::path::{Path, PathBuf}; use nix::dir::Dir; use nix::fcntl::{OFlag, openat}; use nix::libc; -use nix::sys::stat::{FchmodatFlags, FileStat, Mode, fchmodat, fstatat}; +use nix::sys::stat::{FchmodatFlags, FileStat, Mode, fchmodat, fstatat, mkdirat}; use nix::unistd::{Gid, Uid, UnlinkatFlags, fchown, fchownat, unlinkat}; use os_display::Quotable; @@ -323,12 +323,10 @@ impl DirFd { pub fn mkdir_at(&self, name: &OsStr, mode: u32) -> io::Result<()> { let name_cstr = CString::new(name.as_bytes()).map_err(|_| SafeTraversalError::PathContainsNull)?; - let mode = mode as libc::mode_t; - let fd = self.fd.as_raw_fd(); + let mode = Mode::from_bits_truncate(mode as libc::mode_t); - let result = unsafe { libc::mkdirat(fd, name_cstr.as_ptr(), mode) }; - if result == -1 { - let err = io::Error::last_os_error(); + if let Err(e) = mkdirat(self.fd.as_fd(), name_cstr.as_c_str(), mode) { + let err = io::Error::from_raw_os_error(e as i32); return Err(SafeTraversalError::OpenFailed { path: name.into(), source: err,