1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

uucore/mode: handle mode 0

Trimming all '0' characters results in an invalid string if the string
contained only '0's.
This commit is contained in:
Michael Debertol 2021-08-16 00:30:25 +02:00
parent 74958794c6
commit 9697c89d17

View file

@ -11,19 +11,21 @@ use libc::{mode_t, S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR};
pub fn parse_numeric(fperm: u32, mut mode: &str) -> Result<u32, String> { pub fn parse_numeric(fperm: u32, mut mode: &str) -> Result<u32, String> {
let (op, pos) = parse_op(mode, Some('='))?; let (op, pos) = parse_op(mode, Some('='))?;
mode = mode[pos..].trim().trim_start_matches('0'); mode = mode[pos..].trim();
if mode.len() > 4 { match u32::from_str_radix(mode, 8) {
Err(format!("mode is too large ({} > 7777)", mode)) Ok(change) => {
} else { if change > 0o7777 {
match u32::from_str_radix(mode, 8) { Err(format!("mode is too large ({} > 7777", mode))
Ok(change) => Ok(match op { } else {
'+' => fperm | change, Ok(match op {
'-' => fperm & !change, '+' => fperm | change,
'=' => change, '-' => fperm & !change,
_ => unreachable!(), '=' => change,
}), _ => unreachable!(),
Err(err) => Err(err.to_string()), })
}
} }
Err(err) => Err(err.to_string()),
} }
} }