Skip to content

Commit 98f3aac

Browse files
committed
mkdir: Collect OsString, instead of String.
collect_lossy incorrectly converts invalid UTF-8 strings, let's keep the OsString everywhere. This also means we need to modify strip_minus_from_mode to make it work with OsString.
1 parent f6f9ca7 commit 98f3aac

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/uu/mkdir/src/mkdir.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use clap::parser::ValuesRef;
1010
use clap::{Arg, ArgAction, ArgMatches, Command};
1111
use std::collections::HashMap;
1212
use std::ffi::OsString;
13+
use std::os::unix::ffi::OsStrExt;
1314
use std::path::{Path, PathBuf};
1415
#[cfg(not(windows))]
1516
use uucore::error::FromIo;
@@ -81,24 +82,26 @@ fn get_mode(matches: &ArgMatches, mode_had_minus_prefix: bool) -> Result<u32, St
8182
}
8283

8384
#[cfg(windows)]
84-
fn strip_minus_from_mode(_args: &mut [String]) -> bool {
85+
fn strip_minus_from_mode(_args: &mut [OsString]) -> bool {
8586
false
8687
}
8788

8889
// Iterate 'args' and delete the first occurrence
8990
// of a prefix '-' if it's associated with MODE
9091
// e.g. "chmod -v -xw -R FILE" -> "chmod -v xw -R FILE"
9192
#[cfg(not(windows))]
92-
fn strip_minus_from_mode(args: &mut Vec<String>) -> bool {
93+
fn strip_minus_from_mode(args: &mut Vec<OsString>) -> bool {
9394
for arg in args {
9495
if arg == "--" {
9596
break;
9697
}
97-
if let Some(arg_stripped) = arg.strip_prefix('-') {
98-
if let Some('r' | 'w' | 'x' | 'X' | 's' | 't' | 'u' | 'g' | 'o' | '0'..='7') =
99-
arg.chars().nth(1)
98+
let bytes = arg.as_bytes();
99+
if let Some(b'-') = bytes.first() {
100+
if let Some(
101+
b'r' | b'w' | b'x' | b'X' | b's' | b't' | b'u' | b'g' | b'o' | b'0'..=b'7',
102+
) = bytes.get(1)
100103
{
101-
*arg = arg_stripped.to_string();
104+
*arg = std::ffi::OsStr::from_bytes(&bytes[1..]).to_owned();
102105
return true;
103106
}
104107
}
@@ -108,7 +111,7 @@ fn strip_minus_from_mode(args: &mut Vec<String>) -> bool {
108111

109112
#[uucore::main]
110113
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
111-
let mut args = args.collect_lossy();
114+
let mut args: Vec<OsString> = args.collect();
112115

113116
// Before we can parse 'args' with clap (and previously getopts),
114117
// a possible MODE prefix '-' needs to be removed (e.g. "chmod -x FILE").

0 commit comments

Comments
 (0)